2016-08-13 20:37:46 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// peerMutex is used to prevet multiple Peering resources being creaed, updated
|
|
|
|
// or deleted at the same time
|
|
|
|
var peerMutex = &sync.Mutex{}
|
|
|
|
|
|
|
|
func resourceArmVirtualNetworkPeering() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmVirtualNetworkPeeringCreate,
|
|
|
|
Read: resourceArmVirtualNetworkPeeringRead,
|
|
|
|
Update: resourceArmVirtualNetworkPeeringCreate,
|
|
|
|
Delete: resourceArmVirtualNetworkPeeringDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"virtual_network_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"remote_virtual_network_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"allow_virtual_network_access": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"allow_forwarded_traffic": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"allow_gateway_transit": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"use_remote_gateways": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmVirtualNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).vnetPeeringsClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM virtual network peering creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
vnetName := d.Get("virtual_network_name").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
|
|
|
|
peer := network.VirtualNetworkPeering{
|
|
|
|
Name: &name,
|
|
|
|
Properties: getVirtualNetworkPeeringProperties(d),
|
|
|
|
}
|
|
|
|
|
|
|
|
peerMutex.Lock()
|
|
|
|
defer peerMutex.Unlock()
|
|
|
|
|
|
|
|
_, err := client.CreateOrUpdate(resGroup, vnetName, name, peer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := client.Get(resGroup, vnetName, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read Virtual Network Peering %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmVirtualNetworkPeeringRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).vnetPeeringsClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
vnetName := id.Path["virtualNetworks"]
|
|
|
|
name := id.Path["virtualNetworkPeerings"]
|
|
|
|
|
|
|
|
resp, err := client.Get(resGroup, vnetName, name)
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
if err != nil {
|
2016-09-30 18:57:59 +02:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-08-13 20:37:46 +02:00
|
|
|
peer := *resp.Properties
|
|
|
|
|
|
|
|
// update appropriate values
|
2016-09-27 18:49:14 +02:00
|
|
|
d.Set("resource_group_name", resGroup)
|
2016-08-13 20:37:46 +02:00
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("virtual_network_name", vnetName)
|
|
|
|
d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess)
|
|
|
|
d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic)
|
|
|
|
d.Set("allow_gateway_transit", peer.AllowGatewayTransit)
|
|
|
|
d.Set("use_remote_gateways", peer.UseRemoteGateways)
|
|
|
|
d.Set("remote_virtual_network_id", peer.RemoteVirtualNetwork.ID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmVirtualNetworkPeeringDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).vnetPeeringsClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
vnetName := id.Path["virtualNetworks"]
|
|
|
|
name := id.Path["virtualNetworkPeerings"]
|
|
|
|
|
|
|
|
peerMutex.Lock()
|
|
|
|
defer peerMutex.Unlock()
|
|
|
|
|
|
|
|
_, err = client.Delete(resGroup, vnetName, name, make(chan struct{}))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.VirtualNetworkPeeringPropertiesFormat {
|
|
|
|
allowVirtualNetworkAccess := d.Get("allow_virtual_network_access").(bool)
|
|
|
|
allowForwardedTraffic := d.Get("allow_forwarded_traffic").(bool)
|
|
|
|
allowGatewayTransit := d.Get("allow_gateway_transit").(bool)
|
|
|
|
useRemoteGateways := d.Get("use_remote_gateways").(bool)
|
|
|
|
remoteVirtualNetworkID := d.Get("remote_virtual_network_id").(string)
|
|
|
|
|
|
|
|
return &network.VirtualNetworkPeeringPropertiesFormat{
|
|
|
|
AllowVirtualNetworkAccess: &allowVirtualNetworkAccess,
|
|
|
|
AllowForwardedTraffic: &allowForwardedTraffic,
|
|
|
|
AllowGatewayTransit: &allowGatewayTransit,
|
|
|
|
UseRemoteGateways: &useRemoteGateways,
|
|
|
|
RemoteVirtualNetwork: &network.SubResource{
|
|
|
|
ID: &remoteVirtualNetworkID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|