Skip to content

Commit

Permalink
provider/azurerm: Reordering the checks after an Azure API Get
Browse files Browse the repository at this point in the history
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
}
```
  • Loading branch information
stack72 committed Sep 1, 2016
1 parent 5669185 commit 392f634
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 66 deletions.
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_availability_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_cdn_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_cdn_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 5 additions & 6 deletions builtin/providers/azurerm/resource_arm_local_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -110,13 +110,12 @@ func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}

resp, err := lnetClient.Get(resGroup, name)
if err != nil {
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)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("location", resp.Location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
9 changes: 4 additions & 5 deletions builtin/providers/azurerm/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,12 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err

resp, err := client.GetProperties(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return 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
}

keys, err := client.ListKeys(resGroup, name)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions builtin/providers/azurerm/resource_arm_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 4 additions & 3 deletions builtin/providers/azurerm/resource_arm_template_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions builtin/providers/azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/azurerm/resource_arm_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 392f634

@pmcatominey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this may have reverted the fix in #5878

@stack72
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmcatominey no sir, on master, I can see the following:

        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
    }

Please sign in to comment.