2015-12-16 16:37:50 +01:00
|
|
|
package azurerm
|
|
|
|
|
2016-06-02 02:02:04 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
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
|
|
|
"net/http"
|
2016-06-02 02:02:04 +02:00
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmLocalNetworkGateway() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmLocalNetworkGatewayCreate,
|
|
|
|
Read: resourceArmLocalNetworkGatewayRead,
|
|
|
|
Update: resourceArmLocalNetworkGatewayCreate,
|
|
|
|
Delete: resourceArmLocalNetworkGatewayDelete,
|
2016-07-13 14:14:59 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-06-02 02:02:04 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"gateway_address": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"address_space": {
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
lnetClient := meta.(*ArmClient).localNetConnClient
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
location := d.Get("location").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
ipAddress := d.Get("gateway_address").(string)
|
|
|
|
|
|
|
|
// fetch the 'address_space_prefixes:
|
|
|
|
prefixes := []string{}
|
|
|
|
for _, pref := range d.Get("address_space").([]interface{}) {
|
|
|
|
prefixes = append(prefixes, pref.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
gateway := network.LocalNetworkGateway{
|
|
|
|
Name: &name,
|
|
|
|
Location: &location,
|
|
|
|
Properties: &network.LocalNetworkGatewayPropertiesFormat{
|
|
|
|
LocalNetworkAddressSpace: &network.AddressSpace{
|
|
|
|
AddressPrefixes: &prefixes,
|
|
|
|
},
|
|
|
|
GatewayIPAddress: &ipAddress,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := lnetClient.CreateOrUpdate(resGroup, name, gateway, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lnetClient.Get(resGroup, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read Virtual Network %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmLocalNetworkGatewayRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway.
|
|
|
|
func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
lnetClient := meta.(*ArmClient).localNetConnClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.Path["localNetworkGateways"]
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
|
|
|
|
resp, err := lnetClient.Get(resGroup, name)
|
|
|
|
if err != nil {
|
2016-09-30 18:57:59 +02:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-02 02:02:04 +02:00
|
|
|
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-09-28 11:58:27 +02:00
|
|
|
d.Set("resource_group_name", resGroup)
|
2016-07-13 14:14:59 +02:00
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("location", resp.Location)
|
2016-06-02 02:02:04 +02:00
|
|
|
d.Set("gateway_address", resp.Properties.GatewayIPAddress)
|
|
|
|
|
|
|
|
prefs := []string{}
|
|
|
|
if ps := *resp.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
|
|
|
|
prefs = ps
|
|
|
|
}
|
|
|
|
d.Set("address_space", prefs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
|
|
|
|
func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
lnetClient := meta.(*ArmClient).localNetConnClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.Path["localNetworkGateways"]
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
|
|
|
|
_, err = lnetClient.Delete(resGroup, name, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|