github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_virtual_network_peering.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "sync" 8 9 "github.com/Azure/azure-sdk-for-go/arm/network" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 // peerMutex is used to prevet multiple Peering resources being creaed, updated 14 // or deleted at the same time 15 var peerMutex = &sync.Mutex{} 16 17 func resourceArmVirtualNetworkPeering() *schema.Resource { 18 return &schema.Resource{ 19 Create: resourceArmVirtualNetworkPeeringCreate, 20 Read: resourceArmVirtualNetworkPeeringRead, 21 Update: resourceArmVirtualNetworkPeeringCreate, 22 Delete: resourceArmVirtualNetworkPeeringDelete, 23 Importer: &schema.ResourceImporter{ 24 State: schema.ImportStatePassthrough, 25 }, 26 27 Schema: map[string]*schema.Schema{ 28 "name": { 29 Type: schema.TypeString, 30 Required: true, 31 ForceNew: true, 32 }, 33 34 "resource_group_name": { 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 }, 39 40 "virtual_network_name": { 41 Type: schema.TypeString, 42 Required: true, 43 ForceNew: true, 44 }, 45 46 "remote_virtual_network_id": { 47 Type: schema.TypeString, 48 Required: true, 49 ForceNew: true, 50 }, 51 52 "allow_virtual_network_access": { 53 Type: schema.TypeBool, 54 Optional: true, 55 Computed: true, 56 }, 57 58 "allow_forwarded_traffic": { 59 Type: schema.TypeBool, 60 Optional: true, 61 Computed: true, 62 }, 63 64 "allow_gateway_transit": { 65 Type: schema.TypeBool, 66 Optional: true, 67 Computed: true, 68 }, 69 70 "use_remote_gateways": { 71 Type: schema.TypeBool, 72 Optional: true, 73 Computed: true, 74 }, 75 }, 76 } 77 } 78 79 func resourceArmVirtualNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error { 80 client := meta.(*ArmClient).vnetPeeringsClient 81 82 log.Printf("[INFO] preparing arguments for Azure ARM virtual network peering creation.") 83 84 name := d.Get("name").(string) 85 vnetName := d.Get("virtual_network_name").(string) 86 resGroup := d.Get("resource_group_name").(string) 87 88 peer := network.VirtualNetworkPeering{ 89 Name: &name, 90 Properties: getVirtualNetworkPeeringProperties(d), 91 } 92 93 peerMutex.Lock() 94 defer peerMutex.Unlock() 95 96 _, err := client.CreateOrUpdate(resGroup, vnetName, name, peer, make(chan struct{})) 97 if err != nil { 98 return err 99 } 100 101 read, err := client.Get(resGroup, vnetName, name) 102 if err != nil { 103 return err 104 } 105 if read.ID == nil { 106 return fmt.Errorf("Cannot read Virtual Network Peering %s (resource group %s) ID", name, resGroup) 107 } 108 109 d.SetId(*read.ID) 110 111 return resourceArmVirtualNetworkPeeringRead(d, meta) 112 } 113 114 func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error { 115 client := meta.(*ArmClient).vnetPeeringsClient 116 117 id, err := parseAzureResourceID(d.Id()) 118 if err != nil { 119 return err 120 } 121 resGroup := id.ResourceGroup 122 vnetName := id.Path["virtualNetworks"] 123 name := id.Path["virtualNetworkPeerings"] 124 125 resp, err := client.Get(resGroup, vnetName, name) 126 if err != nil { 127 return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err) 128 } 129 if resp.StatusCode == http.StatusNotFound { 130 d.SetId("") 131 return nil 132 } 133 134 peer := *resp.Properties 135 136 // update appropriate values 137 d.Set("name", resp.Name) 138 d.Set("virtual_network_name", vnetName) 139 d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess) 140 d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic) 141 d.Set("allow_gateway_transit", peer.AllowGatewayTransit) 142 d.Set("use_remote_gateways", peer.UseRemoteGateways) 143 d.Set("remote_virtual_network_id", peer.RemoteVirtualNetwork.ID) 144 145 return nil 146 } 147 148 func resourceArmVirtualNetworkPeeringDelete(d *schema.ResourceData, meta interface{}) error { 149 client := meta.(*ArmClient).vnetPeeringsClient 150 151 id, err := parseAzureResourceID(d.Id()) 152 if err != nil { 153 return err 154 } 155 resGroup := id.ResourceGroup 156 vnetName := id.Path["virtualNetworks"] 157 name := id.Path["virtualNetworkPeerings"] 158 159 peerMutex.Lock() 160 defer peerMutex.Unlock() 161 162 _, err = client.Delete(resGroup, vnetName, name, make(chan struct{})) 163 164 return err 165 } 166 167 func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.VirtualNetworkPeeringPropertiesFormat { 168 allowVirtualNetworkAccess := d.Get("allow_virtual_network_access").(bool) 169 allowForwardedTraffic := d.Get("allow_forwarded_traffic").(bool) 170 allowGatewayTransit := d.Get("allow_gateway_transit").(bool) 171 useRemoteGateways := d.Get("use_remote_gateways").(bool) 172 remoteVirtualNetworkID := d.Get("remote_virtual_network_id").(string) 173 174 return &network.VirtualNetworkPeeringPropertiesFormat{ 175 AllowVirtualNetworkAccess: &allowVirtualNetworkAccess, 176 AllowForwardedTraffic: &allowForwardedTraffic, 177 AllowGatewayTransit: &allowGatewayTransit, 178 UseRemoteGateways: &useRemoteGateways, 179 RemoteVirtualNetwork: &network.SubResource{ 180 ID: &remoteVirtualNetworkID, 181 }, 182 } 183 }