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
}
```
This commit is contained in:
stack72 2016-09-01 15:31:42 +01:00
parent 5669185e54
commit 392f634ff4
No known key found for this signature in database
GPG Key ID: 8619A619B085CB16
21 changed files with 69 additions and 66 deletions

View File

@ -117,13 +117,13 @@ func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) er
name := id.Path["availabilitySets"]
resp, err := availSetClient.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
}
availSet := *resp.Properties
d.Set("platform_update_domain_count", availSet.PlatformUpdateDomainCount)

View File

@ -225,13 +225,13 @@ func resourceArmCdnEndpointRead(d *schema.ResourceData, meta interface{}) error
}
log.Printf("[INFO] Trying to find the AzureRM CDN Endpoint %s (Profile: %s, RG: %s)", name, profileName, resGroup)
resp, err := cdnEndpointsClient.Get(name, profileName, resGroup)
if err != nil {
return fmt.Errorf("Error making Read request on Azure CDN Endpoint %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure CDN Endpoint %s: %s", name, err)
}
d.Set("name", resp.Name)
d.Set("host_name", resp.Properties.HostName)

View File

@ -99,13 +99,13 @@ func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
name := id.Path["Profiles"]
resp, err := cdnProfilesClient.Get(name, resGroup)
if err != nil {
return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
}
if resp.Sku != nil {
d.Set("sku", string(resp.Sku.Name))

View File

@ -2,9 +2,9 @@ package azurerm
import (
"fmt"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/core/http"
"github.com/hashicorp/terraform/helper/schema"
)
@ -110,14 +110,13 @@ func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}
resp, err := lnetClient.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
}
d.Set("name", resp.Name)
d.Set("location", resp.Location)
d.Set("gateway_address", resp.Properties.GatewayIPAddress)

View File

@ -56,7 +56,6 @@ func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFu
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp)
}
return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName)
}

View File

@ -245,13 +245,13 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
name := id.Path["networkInterfaces"]
resp, err := ifaceClient.Get(resGroup, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure Network Interface %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Network Interface %s: %s", name, err)
}
iface := *resp.Properties

View File

@ -191,13 +191,13 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{
name := id.Path["networkSecurityGroups"]
resp, err := secGroupClient.Get(resGroup, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure Network Security Group %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Network Security Group %s: %s", name, err)
}
if resp.Properties.SecurityRules != nil {
d.Set("security_rule", flattenNetworkSecurityRules(resp.Properties.SecurityRules))

View File

@ -175,13 +175,13 @@ func resourceArmNetworkSecurityRuleRead(d *schema.ResourceData, meta interface{}
sgRuleName := id.Path["securityRules"]
resp, err := secRuleClient.Get(resGroup, networkSGName, sgRuleName)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Network Security Rule %s: %s", sgRuleName, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Network Security Rule %s: %s", sgRuleName, err)
}
d.Set("access", resp.Properties.Access)
d.Set("destination_address_prefix", resp.Properties.DestinationAddressPrefix)

View File

@ -165,13 +165,13 @@ func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error {
name := id.Path["publicIPAddresses"]
resp, err := publicIPClient.Get(resGroup, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
}
d.Set("location", resp.Location)
d.Set("name", resp.Name)

View File

@ -112,13 +112,13 @@ func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
routeName := id.Path["routes"]
resp, err := routesClient.Get(resGroup, rtName, routeName)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
}
return nil
}

View File

@ -142,13 +142,13 @@ func resourceArmRouteTableRead(d *schema.ResourceData, meta interface{}) error {
name := id.Path["routeTables"]
resp, err := routeTablesClient.Get(resGroup, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure Route Table %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Route Table %s: %s", name, err)
}
if resp.Properties.Subnets != nil {
if len(*resp.Properties.Subnets) > 0 {

View File

@ -112,13 +112,13 @@ func resourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{}
name := id.Path["namespaces"]
resp, err := namespaceClient.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace %s: %s", name, err)
}
d.Set("name", resp.Name)
d.Set("sku", strings.ToLower(string(resp.Sku.Name)))

View File

@ -256,14 +256,13 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err
resp, err := client.GetProperties(resGroup, name)
if err != nil {
return fmt.Errorf("Error reading the state of AzureRM Storage Account %q: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading the state of AzureRM Storage Account %q: %s", name, err)
}
keys, err := client.ListKeys(resGroup, name)
if err != nil {
return err

View File

@ -131,13 +131,14 @@ func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
name := id.Path["subnets"]
resp, err := subnetClient.Get(resGroup, vnetName, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err)
}
if resp.Properties.IPConfigurations != nil && len(*resp.Properties.IPConfigurations) > 0 {
ips := make([]string, 0, len(*resp.Properties.IPConfigurations))

View File

@ -143,13 +143,14 @@ func resourceArmTemplateDeploymentRead(d *schema.ResourceData, meta interface{})
}
resp, err := deployClient.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure RM Template Deployment %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure RM Template Deployment %s: %s", name, err)
}
var outputs map[string]string
if resp.Properties.Outputs != nil && len(*resp.Properties.Outputs) > 0 {
outputs = make(map[string]string)

View File

@ -152,13 +152,14 @@ func resourceArmTrafficManagerEndpointRead(d *schema.ResourceData, meta interfac
name := id.Path[endpointType]
resp, err := client.Get(resGroup, profileName, endpointType, name)
if err != nil {
return fmt.Errorf("Error making Read request on TrafficManager Endpoint %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on TrafficManager Endpoint %s: %s", name, err)
}
endpoint := *resp.Properties
d.Set("name", resp.Name)

View File

@ -150,13 +150,14 @@ func resourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interface
name := id.Path["trafficManagerProfiles"]
resp, err := client.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Traffic Manager Profile %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Traffic Manager Profile %s: %s", name, err)
}
profile := *resp.Properties
// update appropriate values

View File

@ -485,13 +485,14 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err
name := id.Path["virtualMachines"]
resp, err := vmClient.Get(resGroup, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure Virtual Machine %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Virtual Machine %s: %s", name, err)
}
if resp.Plan != nil {
if err := d.Set("plan", flattenAzureRmVirtualMachinePlan(resp.Plan)); err != nil {

View File

@ -431,14 +431,14 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
name := id.Path["virtualMachineScaleSets"]
resp, err := vmScaleSetClient.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Virtual Machine Scale Set %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
log.Printf("[INFO] AzureRM Virtual Machine Scale Set (%s) Not Found. Removing from State", name)
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Virtual Machine Scale Set %s: %s", name, err)
}
d.Set("location", resp.Location)
d.Set("name", resp.Name)

View File

@ -132,13 +132,13 @@ func resourceArmVirtualNetworkRead(d *schema.ResourceData, meta interface{}) err
name := id.Path["virtualNetworks"]
resp, err := vnetClient.Get(resGroup, name, "")
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network %s: %s", name, err)
}
vnet := *resp.Properties
// update appropriate values

View File

@ -123,13 +123,14 @@ func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface
name := id.Path["virtualNetworkPeerings"]
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
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
peer := *resp.Properties
// update appropriate values