From 392f634ff492445770be91f07456cff387c80cdd Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 1 Sep 2016 15:31:42 +0100 Subject: [PATCH] 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 } ``` --- .../azurerm/resource_arm_availability_set.go | 6 +++--- .../providers/azurerm/resource_arm_cdn_endpoint.go | 6 +++--- builtin/providers/azurerm/resource_arm_cdn_profile.go | 6 +++--- .../azurerm/resource_arm_local_network_gateway.go | 11 +++++------ .../resource_arm_local_network_gateway_test.go | 1 - .../azurerm/resource_arm_network_interface_card.go | 6 +++--- .../azurerm/resource_arm_network_security_group.go | 6 +++--- .../azurerm/resource_arm_network_security_rule.go | 6 +++--- builtin/providers/azurerm/resource_arm_public_ip.go | 6 +++--- builtin/providers/azurerm/resource_arm_route.go | 6 +++--- builtin/providers/azurerm/resource_arm_route_table.go | 6 +++--- .../azurerm/resource_arm_servicebus_namespace.go | 6 +++--- .../providers/azurerm/resource_arm_storage_account.go | 9 ++++----- builtin/providers/azurerm/resource_arm_subnet.go | 7 ++++--- .../azurerm/resource_arm_template_deployment.go | 7 ++++--- .../azurerm/resource_arm_traffic_manager_endpoint.go | 7 ++++--- .../azurerm/resource_arm_traffic_manager_profile.go | 7 ++++--- .../providers/azurerm/resource_arm_virtual_machine.go | 7 ++++--- .../azurerm/resource_arm_virtual_machine_scale_set.go | 6 +++--- .../providers/azurerm/resource_arm_virtual_network.go | 6 +++--- .../azurerm/resource_arm_virtual_network_peering.go | 7 ++++--- 21 files changed, 69 insertions(+), 66 deletions(-) diff --git a/builtin/providers/azurerm/resource_arm_availability_set.go b/builtin/providers/azurerm/resource_arm_availability_set.go index fd6edf766..b14cfb1ed 100644 --- a/builtin/providers/azurerm/resource_arm_availability_set.go +++ b/builtin/providers/azurerm/resource_arm_availability_set.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_cdn_endpoint.go b/builtin/providers/azurerm/resource_arm_cdn_endpoint.go index 719dc72cd..047de55dc 100644 --- a/builtin/providers/azurerm/resource_arm_cdn_endpoint.go +++ b/builtin/providers/azurerm/resource_arm_cdn_endpoint.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_cdn_profile.go b/builtin/providers/azurerm/resource_arm_cdn_profile.go index ab96106cf..2ab8cee42 100644 --- a/builtin/providers/azurerm/resource_arm_cdn_profile.go +++ b/builtin/providers/azurerm/resource_arm_cdn_profile.go @@ -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)) diff --git a/builtin/providers/azurerm/resource_arm_local_network_gateway.go b/builtin/providers/azurerm/resource_arm_local_network_gateway.go index 11c412b81..0edaa8151 100644 --- a/builtin/providers/azurerm/resource_arm_local_network_gateway.go +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway.go @@ -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,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) diff --git a/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go b/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go index 1b57fc906..8034dfe31 100644 --- a/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go @@ -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) } diff --git a/builtin/providers/azurerm/resource_arm_network_interface_card.go b/builtin/providers/azurerm/resource_arm_network_interface_card.go index 8e6771541..b5575efc0 100644 --- a/builtin/providers/azurerm/resource_arm_network_interface_card.go +++ b/builtin/providers/azurerm/resource_arm_network_interface_card.go @@ -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 diff --git a/builtin/providers/azurerm/resource_arm_network_security_group.go b/builtin/providers/azurerm/resource_arm_network_security_group.go index e6872c622..11a67a76a 100644 --- a/builtin/providers/azurerm/resource_arm_network_security_group.go +++ b/builtin/providers/azurerm/resource_arm_network_security_group.go @@ -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)) diff --git a/builtin/providers/azurerm/resource_arm_network_security_rule.go b/builtin/providers/azurerm/resource_arm_network_security_rule.go index eb5b4f263..e9ea8f5b5 100644 --- a/builtin/providers/azurerm/resource_arm_network_security_rule.go +++ b/builtin/providers/azurerm/resource_arm_network_security_rule.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_public_ip.go b/builtin/providers/azurerm/resource_arm_public_ip.go index e3155cbc1..001c42608 100644 --- a/builtin/providers/azurerm/resource_arm_public_ip.go +++ b/builtin/providers/azurerm/resource_arm_public_ip.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_route.go b/builtin/providers/azurerm/resource_arm_route.go index ae7b38279..e2802ddb2 100644 --- a/builtin/providers/azurerm/resource_arm_route.go +++ b/builtin/providers/azurerm/resource_arm_route.go @@ -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 } diff --git a/builtin/providers/azurerm/resource_arm_route_table.go b/builtin/providers/azurerm/resource_arm_route_table.go index 3e9571840..dc488c437 100644 --- a/builtin/providers/azurerm/resource_arm_route_table.go +++ b/builtin/providers/azurerm/resource_arm_route_table.go @@ -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 { diff --git a/builtin/providers/azurerm/resource_arm_servicebus_namespace.go b/builtin/providers/azurerm/resource_arm_servicebus_namespace.go index 4ded28d6c..f7ffb3b37 100644 --- a/builtin/providers/azurerm/resource_arm_servicebus_namespace.go +++ b/builtin/providers/azurerm/resource_arm_servicebus_namespace.go @@ -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))) diff --git a/builtin/providers/azurerm/resource_arm_storage_account.go b/builtin/providers/azurerm/resource_arm_storage_account.go index a5bd72b22..7cefd16e4 100644 --- a/builtin/providers/azurerm/resource_arm_storage_account.go +++ b/builtin/providers/azurerm/resource_arm_storage_account.go @@ -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 { diff --git a/builtin/providers/azurerm/resource_arm_subnet.go b/builtin/providers/azurerm/resource_arm_subnet.go index a09b9cab2..60fae8131 100644 --- a/builtin/providers/azurerm/resource_arm_subnet.go +++ b/builtin/providers/azurerm/resource_arm_subnet.go @@ -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)) diff --git a/builtin/providers/azurerm/resource_arm_template_deployment.go b/builtin/providers/azurerm/resource_arm_template_deployment.go index 5eb37cd70..2db462b89 100644 --- a/builtin/providers/azurerm/resource_arm_template_deployment.go +++ b/builtin/providers/azurerm/resource_arm_template_deployment.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go b/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go index f4d1c4b68..935645e18 100644 --- a/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go +++ b/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go b/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go index 53013631f..c000e3264 100644 --- a/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go +++ b/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go @@ -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 diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine.go b/builtin/providers/azurerm/resource_arm_virtual_machine.go index b58e5878b..de5577a24 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine.go @@ -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 { diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go b/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go index d8de1226a..c7e4b1f67 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go @@ -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) diff --git a/builtin/providers/azurerm/resource_arm_virtual_network.go b/builtin/providers/azurerm/resource_arm_virtual_network.go index 77ec5434f..8c7ed205f 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_network.go +++ b/builtin/providers/azurerm/resource_arm_virtual_network.go @@ -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 diff --git a/builtin/providers/azurerm/resource_arm_virtual_network_peering.go b/builtin/providers/azurerm/resource_arm_virtual_network_peering.go index 854ceeae0..0c7ab3af7 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_network_peering.go +++ b/builtin/providers/azurerm/resource_arm_virtual_network_peering.go @@ -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