|
| 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 resp.StatusCode == http.StatusNotFound { |
| 127 | + d.SetId("") |
| 128 | + return nil |
| 129 | + } |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err) |
| 132 | + } |
| 133 | + peer := *resp.Properties |
| 134 | + |
| 135 | + // update appropriate values |
| 136 | + d.Set("name", resp.Name) |
| 137 | + d.Set("virtual_network_name", vnetName) |
| 138 | + d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess) |
| 139 | + d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic) |
| 140 | + d.Set("allow_gateway_transit", peer.AllowGatewayTransit) |
| 141 | + d.Set("use_remote_gateways", peer.UseRemoteGateways) |
| 142 | + d.Set("remote_virtual_network_id", peer.RemoteVirtualNetwork.ID) |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func resourceArmVirtualNetworkPeeringDelete(d *schema.ResourceData, meta interface{}) error { |
| 148 | + client := meta.(*ArmClient).vnetPeeringsClient |
| 149 | + |
| 150 | + id, err := parseAzureResourceID(d.Id()) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + resGroup := id.ResourceGroup |
| 155 | + vnetName := id.Path["virtualNetworks"] |
| 156 | + name := id.Path["virtualNetworkPeerings"] |
| 157 | + |
| 158 | + peerMutex.Lock() |
| 159 | + defer peerMutex.Unlock() |
| 160 | + |
| 161 | + _, err = client.Delete(resGroup, vnetName, name, make(chan struct{})) |
| 162 | + |
| 163 | + return err |
| 164 | +} |
| 165 | + |
| 166 | +func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.VirtualNetworkPeeringPropertiesFormat { |
| 167 | + allowVirtualNetworkAccess := d.Get("allow_virtual_network_access").(bool) |
| 168 | + allowForwardedTraffic := d.Get("allow_forwarded_traffic").(bool) |
| 169 | + allowGatewayTransit := d.Get("allow_gateway_transit").(bool) |
| 170 | + useRemoteGateways := d.Get("use_remote_gateways").(bool) |
| 171 | + remoteVirtualNetworkID := d.Get("remote_virtual_network_id").(string) |
| 172 | + |
| 173 | + return &network.VirtualNetworkPeeringPropertiesFormat{ |
| 174 | + AllowVirtualNetworkAccess: &allowVirtualNetworkAccess, |
| 175 | + AllowForwardedTraffic: &allowForwardedTraffic, |
| 176 | + AllowGatewayTransit: &allowGatewayTransit, |
| 177 | + UseRemoteGateways: &useRemoteGateways, |
| 178 | + RemoteVirtualNetwork: &network.SubResource{ |
| 179 | + ID: &remoteVirtualNetworkID, |
| 180 | + }, |
| 181 | + } |
| 182 | +} |
0 commit comments