diff --git a/azurerm/express_route_circuit.go b/azurerm/express_route_circuit.go index 297b55f560da..fac55b2310a6 100644 --- a/azurerm/express_route_circuit.go +++ b/azurerm/express_route_circuit.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/network" "github.com/hashicorp/errwrap" @@ -30,7 +29,7 @@ func retrieveErcByResourceId(resourceId string, meta interface{}) (erc *network. resp, err := ercClient.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { return nil, "", nil } return nil, "", errwrap.Wrapf(fmt.Sprintf("Error making Read request on Express Route Circuit %s: {{err}}", name), err) diff --git a/azurerm/import_arm_eventhub_namespace_test.go b/azurerm/import_arm_eventhub_namespace_test.go index fb1d16a2ef21..95ef399e0186 100644 --- a/azurerm/import_arm_eventhub_namespace_test.go +++ b/azurerm/import_arm_eventhub_namespace_test.go @@ -4,6 +4,7 @@ import ( "testing" "fmt" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" ) diff --git a/azurerm/import_arm_sql_elasticpool_test.go b/azurerm/import_arm_sql_elasticpool_test.go index 1657f5c15c26..f4193fd2c0fc 100644 --- a/azurerm/import_arm_sql_elasticpool_test.go +++ b/azurerm/import_arm_sql_elasticpool_test.go @@ -2,9 +2,10 @@ package azurerm import ( "fmt" + "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" - "testing" ) func TestAccAzureRMSqlElasticPool_importBasic(t *testing.T) { diff --git a/azurerm/resource_arm_application_insights.go b/azurerm/resource_arm_application_insights.go index 8218cdb942e5..d33861f5314f 100644 --- a/azurerm/resource_arm_application_insights.go +++ b/azurerm/resource_arm_application_insights.go @@ -118,7 +118,7 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{} resp, err := client.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_availability_set.go b/azurerm/resource_arm_availability_set.go index 48f9b4931fcc..e4327c1f4e77 100644 --- a/azurerm/resource_arm_availability_set.go +++ b/azurerm/resource_arm_availability_set.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/compute" @@ -117,7 +116,7 @@ func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) er resp, err := client.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_cdn_endpoint.go b/azurerm/resource_arm_cdn_endpoint.go index 358da7ed3c26..71205efc56a4 100644 --- a/azurerm/resource_arm_cdn_endpoint.go +++ b/azurerm/resource_arm_cdn_endpoint.go @@ -225,7 +225,7 @@ 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(resGroup, profileName, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_cdn_profile.go b/azurerm/resource_arm_cdn_profile.go index 801b1e6ddc5f..2ce84f623f65 100644 --- a/azurerm/resource_arm_cdn_profile.go +++ b/azurerm/resource_arm_cdn_profile.go @@ -3,8 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" - "strings" "github.com/Azure/azure-sdk-for-go/arm/cdn" @@ -100,7 +98,7 @@ func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error { resp, err := cdnProfilesClient.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_container_registry.go b/azurerm/resource_arm_container_registry.go index efd03cd7502d..69408bcbcb6d 100644 --- a/azurerm/resource_arm_container_registry.go +++ b/azurerm/resource_arm_container_registry.go @@ -210,12 +210,13 @@ func resourceArmContainerRegistryRead(d *schema.ResourceData, meta interface{}) resp, err := client.Get(resourceGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on Azure Container Registry %s: %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("resource_group_name", resourceGroup) diff --git a/azurerm/resource_arm_container_service.go b/azurerm/resource_arm_container_service.go index 54b7ed3baa3d..af73cf2d48a0 100644 --- a/azurerm/resource_arm_container_service.go +++ b/azurerm/resource_arm_container_service.go @@ -268,12 +268,13 @@ func resourceArmContainerServiceRead(d *schema.ResourceData, meta interface{}) e resp, err := containerServiceClient.Get(resGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on Azure Container Service %s: %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("location", azureRMNormalizeLocation(*resp.Location)) diff --git a/azurerm/resource_arm_cosmos_db_account.go b/azurerm/resource_arm_cosmos_db_account.go index 75c46e6ffb8f..f07ef45e8970 100644 --- a/azurerm/resource_arm_cosmos_db_account.go +++ b/azurerm/resource_arm_cosmos_db_account.go @@ -203,12 +203,13 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er resp, err := client.Get(resGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on AzureRM CosmosDB Account '%s': %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("location", azureRMNormalizeLocation(*resp.Location)) diff --git a/azurerm/resource_arm_dns_a_record.go b/azurerm/resource_arm_dns_a_record.go index 9e535ebbaf91..a0f1362e2531 100644 --- a/azurerm/resource_arm_dns_a_record.go +++ b/azurerm/resource_arm_dns_a_record.go @@ -106,12 +106,12 @@ func resourceArmDnsARecordRead(d *schema.ResourceData, meta interface{}) error { resp, err := dnsClient.Get(resGroup, zoneName, name, dns.A) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS A record %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_aaaa_record.go b/azurerm/resource_arm_dns_aaaa_record.go index a46d63f4b3af..84e6c82bd0bf 100644 --- a/azurerm/resource_arm_dns_aaaa_record.go +++ b/azurerm/resource_arm_dns_aaaa_record.go @@ -106,12 +106,12 @@ func resourceArmDnsAaaaRecordRead(d *schema.ResourceData, meta interface{}) erro resp, err := dnsClient.Get(resGroup, zoneName, name, dns.AAAA) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS AAAA record %s: %v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_cname_record.go b/azurerm/resource_arm_dns_cname_record.go index 3ab7755cee1a..29f515fb6d21 100644 --- a/azurerm/resource_arm_dns_cname_record.go +++ b/azurerm/resource_arm_dns_cname_record.go @@ -108,27 +108,27 @@ func resourceArmDnsCNameRecordRead(d *schema.ResourceData, meta interface{}) err name := id.Path["CNAME"] zoneName := id.Path["dnszones"] - result, err := dnsClient.Get(resGroup, zoneName, name, dns.CNAME) + resp, err := dnsClient.Get(resGroup, zoneName, name, dns.CNAME) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS CNAME record %s: %+v", name, err) } - if result.Response.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) d.Set("zone_name", zoneName) - d.Set("ttl", result.TTL) + d.Set("ttl", resp.TTL) - if props := result.RecordSetProperties; props != nil { + if props := resp.RecordSetProperties; props != nil { if record := props.CnameRecord; record != nil { d.Set("record", record.Cname) } } - flattenAndSetTags(d, result.Metadata) + flattenAndSetTags(d, resp.Metadata) return nil } diff --git a/azurerm/resource_arm_dns_mx_record.go b/azurerm/resource_arm_dns_mx_record.go index ec7b0b1e67fd..b5f815bf72aa 100644 --- a/azurerm/resource_arm_dns_mx_record.go +++ b/azurerm/resource_arm_dns_mx_record.go @@ -121,12 +121,12 @@ func resourceArmDnsMxRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := client.Get(resGroup, zoneName, name, dns.MX) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS MX record %s: %v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_ns_record.go b/azurerm/resource_arm_dns_ns_record.go index b37fb177bc7c..4917a714c844 100644 --- a/azurerm/resource_arm_dns_ns_record.go +++ b/azurerm/resource_arm_dns_ns_record.go @@ -111,12 +111,12 @@ func resourceArmDnsNsRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := dnsClient.Get(resGroup, zoneName, name, dns.NS) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS NS record %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_ptr_record.go b/azurerm/resource_arm_dns_ptr_record.go index edba56b80e00..f862b1d5334b 100644 --- a/azurerm/resource_arm_dns_ptr_record.go +++ b/azurerm/resource_arm_dns_ptr_record.go @@ -106,7 +106,7 @@ func resourceArmDnsPtrRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := dnsClient.Get(resGroup, zoneName, name, dns.PTR) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_dns_srv_record.go b/azurerm/resource_arm_dns_srv_record.go index ad8550e6e015..a822619f0a79 100644 --- a/azurerm/resource_arm_dns_srv_record.go +++ b/azurerm/resource_arm_dns_srv_record.go @@ -130,12 +130,12 @@ func resourceArmDnsSrvRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := client.Get(resGroup, zoneName, name, dns.SRV) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS SRV record %s: %v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_txt_record.go b/azurerm/resource_arm_dns_txt_record.go index f6c542090534..14d31232f071 100644 --- a/azurerm/resource_arm_dns_txt_record.go +++ b/azurerm/resource_arm_dns_txt_record.go @@ -112,12 +112,12 @@ func resourceArmDnsTxtRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := client.Get(resGroup, zoneName, name, dns.TXT) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS TXT record %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_zone.go b/azurerm/resource_arm_dns_zone.go index 0fd236bda1fa..91c2dac47f10 100644 --- a/azurerm/resource_arm_dns_zone.go +++ b/azurerm/resource_arm_dns_zone.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/dns" "github.com/hashicorp/terraform/helper/schema" @@ -97,12 +96,12 @@ func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error { resp, err := zonesClient.Get(resGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error reading DNS zone %s (resource group %s): %+v", name, resGroup, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_eventhub.go b/azurerm/resource_arm_eventhub.go index 672f8a927cf2..2aac9bc52925 100644 --- a/azurerm/resource_arm_eventhub.go +++ b/azurerm/resource_arm_eventhub.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/eventhub" @@ -115,12 +114,12 @@ func resourceArmEventHubRead(d *schema.ResourceData, meta interface{}) error { resp, err := eventhubClient.Get(resGroup, namespaceName, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure EventHub %s: %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("location", azureRMNormalizeLocation(*resp.Location)) diff --git a/azurerm/resource_arm_eventhub_authorization_rule.go b/azurerm/resource_arm_eventhub_authorization_rule.go index 2c3e53622e7d..d866850c7a25 100644 --- a/azurerm/resource_arm_eventhub_authorization_rule.go +++ b/azurerm/resource_arm_eventhub_authorization_rule.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/eventhub" @@ -144,12 +143,12 @@ func resourceArmEventHubAuthorizationRuleRead(d *schema.ResourceData, meta inter resp, err := client.GetAuthorizationRule(resGroup, namespaceName, eventHubName, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure EventHub Authorization Rule %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } keysResp, err := client.ListKeys(resGroup, namespaceName, eventHubName, name) if err != nil { diff --git a/azurerm/resource_arm_eventhub_consumer_group.go b/azurerm/resource_arm_eventhub_consumer_group.go index 618ec0db1a94..c29eb552bb46 100644 --- a/azurerm/resource_arm_eventhub_consumer_group.go +++ b/azurerm/resource_arm_eventhub_consumer_group.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/eventhub" @@ -109,12 +108,12 @@ func resourceArmEventHubConsumerGroupRead(d *schema.ResourceData, meta interface resp, err := eventhubClient.Get(resGroup, namespaceName, eventHubName, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure EventHub Consumer Group %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("location", azureRMNormalizeLocation(*resp.Location)) diff --git a/azurerm/resource_arm_eventhub_namespace.go b/azurerm/resource_arm_eventhub_namespace.go index aa940dae27ce..7a92af548cf8 100644 --- a/azurerm/resource_arm_eventhub_namespace.go +++ b/azurerm/resource_arm_eventhub_namespace.go @@ -133,12 +133,12 @@ func resourceArmEventHubNamespaceRead(d *schema.ResourceData, meta interface{}) resp, err := namespaceClient.Get(resGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure EventHub Namespace %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("location", azureRMNormalizeLocation(*resp.Location)) diff --git a/azurerm/resource_arm_express_route_circuit.go b/azurerm/resource_arm_express_route_circuit.go index e003231a2620..a241dcc4e4df 100644 --- a/azurerm/resource_arm_express_route_circuit.go +++ b/azurerm/resource_arm_express_route_circuit.go @@ -168,8 +168,8 @@ func resourceArmExpressRouteCircuitRead(d *schema.ResourceData, meta interface{} } if erc == nil { - d.SetId("") log.Printf("[INFO] Express Route Circuit %q not found. Removing from state", d.Get("name").(string)) + d.SetId("") return nil } diff --git a/azurerm/resource_arm_image.go b/azurerm/resource_arm_image.go index 0a1303673afd..74b35cb40f95 100644 --- a/azurerm/resource_arm_image.go +++ b/azurerm/resource_arm_image.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/compute" "github.com/hashicorp/terraform/helper/schema" @@ -242,7 +241,7 @@ func resourceArmImageRead(d *schema.ResourceData, meta interface{}) error { resp, err := imageClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_key_vault.go b/azurerm/resource_arm_key_vault.go index 2f893cf650c9..95fa3df45de9 100644 --- a/azurerm/resource_arm_key_vault.go +++ b/azurerm/resource_arm_key_vault.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/keyvault" "github.com/hashicorp/terraform/helper/schema" @@ -204,12 +203,12 @@ func resourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error { resp, err := client.Get(resGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure KeyVault %s: %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_local_network_gateway.go b/azurerm/resource_arm_local_network_gateway.go index a0c79d434a5f..0490f58f6797 100644 --- a/azurerm/resource_arm_local_network_gateway.go +++ b/azurerm/resource_arm_local_network_gateway.go @@ -109,7 +109,7 @@ func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{} resp, err := lnetClient.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_managed_disk.go b/azurerm/resource_arm_managed_disk.go index 915898496001..2b494fdba81f 100644 --- a/azurerm/resource_arm_managed_disk.go +++ b/azurerm/resource_arm_managed_disk.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/disk" @@ -182,7 +181,7 @@ func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error resp, err := diskClient.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_network_interface_card.go b/azurerm/resource_arm_network_interface_card.go index 64543eebb10f..7e0ae6db685e 100644 --- a/azurerm/resource_arm_network_interface_card.go +++ b/azurerm/resource_arm_network_interface_card.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/network" @@ -261,7 +260,7 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e resp, err := ifaceClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_network_security_group.go b/azurerm/resource_arm_network_security_group.go index f3e11b8c0d61..6dd38ea43bbd 100644 --- a/azurerm/resource_arm_network_security_group.go +++ b/azurerm/resource_arm_network_security_group.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "net/http" "time" "github.com/Azure/azure-sdk-for-go/arm/network" @@ -194,7 +193,7 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{ resp, err := secGroupClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_network_security_rule.go b/azurerm/resource_arm_network_security_rule.go index 9ac09c296e02..5a48ef08ea80 100644 --- a/azurerm/resource_arm_network_security_rule.go +++ b/azurerm/resource_arm_network_security_rule.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/network" "github.com/hashicorp/terraform/helper/schema" @@ -177,7 +176,7 @@ func resourceArmNetworkSecurityRuleRead(d *schema.ResourceData, meta interface{} resp, err := secRuleClient.Get(resGroup, networkSGName, sgRuleName) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_public_ip.go b/azurerm/resource_arm_public_ip.go index da34e72734d7..108ceef0445c 100644 --- a/azurerm/resource_arm_public_ip.go +++ b/azurerm/resource_arm_public_ip.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "regexp" "strings" @@ -161,7 +160,7 @@ func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error { resp, err := publicIPClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_route.go b/azurerm/resource_arm_route.go index 9f01662bcda9..617ac271758c 100644 --- a/azurerm/resource_arm_route.go +++ b/azurerm/resource_arm_route.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/network" @@ -121,7 +120,7 @@ func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error { resp, err := routesClient.Get(resGroup, rtName, routeName) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_route_table.go b/azurerm/resource_arm_route_table.go index 3daf6a7bcd33..136f22a4c1aa 100644 --- a/azurerm/resource_arm_route_table.go +++ b/azurerm/resource_arm_route_table.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/network" @@ -144,7 +143,7 @@ func resourceArmRouteTableRead(d *schema.ResourceData, meta interface{}) error { resp, err := routeTablesClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_servicebus_namespace.go b/azurerm/resource_arm_servicebus_namespace.go index fe11c5e99b45..549515664693 100644 --- a/azurerm/resource_arm_servicebus_namespace.go +++ b/azurerm/resource_arm_servicebus_namespace.go @@ -139,12 +139,12 @@ func resourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{} resp, err := namespaceClient.Get(resGroup, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace '%s': %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_servicebus_queue.go b/azurerm/resource_arm_servicebus_queue.go index 3b8be54402c5..6f3b2078c769 100644 --- a/azurerm/resource_arm_servicebus_queue.go +++ b/azurerm/resource_arm_servicebus_queue.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/servicebus" "github.com/hashicorp/terraform/helper/schema" @@ -190,12 +189,12 @@ func resourceArmServiceBusQueueRead(d *schema.ResourceData, meta interface{}) er resp, err := client.Get(resGroup, namespaceName, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure ServiceBus Queue %s: %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_servicebus_subscription.go b/azurerm/resource_arm_servicebus_subscription.go index d0c4fc64e11f..a03184ea8680 100644 --- a/azurerm/resource_arm_servicebus_subscription.go +++ b/azurerm/resource_arm_servicebus_subscription.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/servicebus" "github.com/hashicorp/terraform/helper/schema" @@ -163,12 +162,12 @@ func resourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interfac resp, err := client.Get(resGroup, namespaceName, topicName, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure ServiceBus Subscription %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_servicebus_topic.go b/azurerm/resource_arm_servicebus_topic.go index cd2d7b69e631..fdb8ff82e9c0 100644 --- a/azurerm/resource_arm_servicebus_topic.go +++ b/azurerm/resource_arm_servicebus_topic.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/servicebus" "github.com/hashicorp/terraform/helper/schema" @@ -188,12 +187,12 @@ func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) er resp, err := client.Get(resGroup, namespaceName, name) if err != nil { + if responseWasNotFound(resp.Response) { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure ServiceBus Topic %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_sql_elasticpool.go b/azurerm/resource_arm_sql_elasticpool.go index 67f9f32c3f8d..9d78c2bd33f7 100644 --- a/azurerm/resource_arm_sql_elasticpool.go +++ b/azurerm/resource_arm_sql_elasticpool.go @@ -2,12 +2,12 @@ package azurerm import ( "fmt" + "log" + "time" + "github.com/Azure/azure-sdk-for-go/arm/sql" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" - "log" - "net/http" - "time" ) func resourceArmSqlElasticPool() *schema.Resource { @@ -131,7 +131,7 @@ func resourceArmSqlElasticPoolRead(d *schema.ResourceData, meta interface{}) err resp, err := elasticPoolsClient.Get(resGroup, serverName, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_sql_elasticpool_test.go b/azurerm/resource_arm_sql_elasticpool_test.go index 991eb691bfca..dd224a166ca3 100644 --- a/azurerm/resource_arm_sql_elasticpool_test.go +++ b/azurerm/resource_arm_sql_elasticpool_test.go @@ -2,11 +2,12 @@ package azurerm import ( "fmt" + "net/http" + "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "net/http" - "testing" ) func TestAccAzureRMSqlElasticPool_basic(t *testing.T) { diff --git a/azurerm/resource_arm_storage_account.go b/azurerm/resource_arm_storage_account.go index 59dbc0c273d2..6e67353d0fa8 100644 --- a/azurerm/resource_arm_storage_account.go +++ b/azurerm/resource_arm_storage_account.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "regexp" "strings" "time" @@ -368,7 +367,7 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err resp, err := client.GetProperties(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_subnet.go b/azurerm/resource_arm_subnet.go index b991b4a1a40f..4af8fa5ea094 100644 --- a/azurerm/resource_arm_subnet.go +++ b/azurerm/resource_arm_subnet.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/network" "github.com/hashicorp/terraform/helper/schema" @@ -154,7 +153,7 @@ func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { resp, err := subnetClient.Get(resGroup, vnetName, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_template_deployment.go b/azurerm/resource_arm_template_deployment.go index 4431f537e80a..9ad824b327f4 100644 --- a/azurerm/resource_arm_template_deployment.go +++ b/azurerm/resource_arm_template_deployment.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "log" - "net/http" "strconv" "strings" "time" @@ -146,7 +145,7 @@ func resourceArmTemplateDeploymentRead(d *schema.ResourceData, meta interface{}) resp, err := deployClient.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_traffic_manager_endpoint.go b/azurerm/resource_arm_traffic_manager_endpoint.go index 0936ce828ec1..d648d9b04c8f 100644 --- a/azurerm/resource_arm_traffic_manager_endpoint.go +++ b/azurerm/resource_arm_traffic_manager_endpoint.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "regexp" "github.com/Azure/azure-sdk-for-go/arm/trafficmanager" @@ -156,7 +155,7 @@ func resourceArmTrafficManagerEndpointRead(d *schema.ResourceData, meta interfac resp, err := client.Get(resGroup, profileName, endpointType, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_traffic_manager_profile.go b/azurerm/resource_arm_traffic_manager_profile.go index c4bf1d05b233..291d3cfd3fe1 100644 --- a/azurerm/resource_arm_traffic_manager_profile.go +++ b/azurerm/resource_arm_traffic_manager_profile.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/trafficmanager" @@ -153,7 +152,7 @@ func resourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interface resp, err := client.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_virtual_machine.go b/azurerm/resource_arm_virtual_machine.go index b1c8787af7e5..8464a91a8cda 100644 --- a/azurerm/resource_arm_virtual_machine.go +++ b/azurerm/resource_arm_virtual_machine.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "net/http" "net/url" "strings" @@ -626,7 +625,7 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err resp, err := vmClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_virtual_machine_extension.go b/azurerm/resource_arm_virtual_machine_extension.go index 49155cd39dcd..efe3729dbf55 100644 --- a/azurerm/resource_arm_virtual_machine_extension.go +++ b/azurerm/resource_arm_virtual_machine_extension.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "net/http" "github.com/Azure/azure-sdk-for-go/arm/compute" "github.com/hashicorp/terraform/helper/schema" @@ -156,7 +155,7 @@ func resourceArmVirtualMachineExtensionsRead(d *schema.ResourceData, meta interf resp, err := client.Get(resGroup, vmName, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_virtual_machine_scale_set.go b/azurerm/resource_arm_virtual_machine_scale_set.go index f65b4d11e378..9f6d13c2b1d0 100644 --- a/azurerm/resource_arm_virtual_machine_scale_set.go +++ b/azurerm/resource_arm_virtual_machine_scale_set.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "net/http" "strings" "github.com/Azure/azure-sdk-for-go/arm/compute" @@ -610,7 +609,7 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac resp, err := vmScaleSetClient.Get(resGroup, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { log.Printf("[INFO] AzureRM Virtual Machine Scale Set (%s) Not Found. Removing from State", name) d.SetId("") return nil diff --git a/azurerm/resource_arm_virtual_network.go b/azurerm/resource_arm_virtual_network.go index e3cf34292d56..2898fbb62002 100644 --- a/azurerm/resource_arm_virtual_network.go +++ b/azurerm/resource_arm_virtual_network.go @@ -150,7 +150,7 @@ func resourceArmVirtualNetworkRead(d *schema.ResourceData, meta interface{}) err resp, err := vnetClient.Get(resGroup, name, "") if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/resource_arm_virtual_network_peering.go b/azurerm/resource_arm_virtual_network_peering.go index e783d6cd8648..2849cefcfabe 100644 --- a/azurerm/resource_arm_virtual_network_peering.go +++ b/azurerm/resource_arm_virtual_network_peering.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "net/http" "sync" "github.com/Azure/azure-sdk-for-go/arm/network" @@ -125,7 +124,7 @@ func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface resp, err := client.Get(resGroup, vnetName, name) if err != nil { - if resp.StatusCode == http.StatusNotFound { + if responseWasNotFound(resp.Response) { d.SetId("") return nil } diff --git a/azurerm/response.go b/azurerm/response.go new file mode 100644 index 000000000000..e3b01100b50b --- /dev/null +++ b/azurerm/response.go @@ -0,0 +1,17 @@ +package azurerm + +import ( + "net/http" + + "github.com/Azure/go-autorest/autorest" +) + +func responseWasNotFound(resp autorest.Response) bool { + if r := resp.Response; r != nil { + if r.StatusCode == http.StatusNotFound { + return true + } + } + + return false +} diff --git a/azurerm/response_test.go b/azurerm/response_test.go new file mode 100644 index 000000000000..e25e29b6ea92 --- /dev/null +++ b/azurerm/response_test.go @@ -0,0 +1,39 @@ +package azurerm + +import ( + "net/http" + "testing" + + "github.com/Azure/go-autorest/autorest" +) + +func TestResponseNotFound_DroppedConnection(t *testing.T) { + resp := autorest.Response{} + if responseWasNotFound(resp) { + t.Fatalf("responseWasNotFound should return `false` for a dropped connection") + } +} + +func TestResponseNotFound_StatusCodes(t *testing.T) { + testCases := []struct { + statusCode int + expectedResult bool + }{ + {http.StatusOK, false}, + {http.StatusInternalServerError, false}, + {http.StatusNotFound, true}, + } + + for _, test := range testCases { + resp := autorest.Response{ + Response: &http.Response{ + StatusCode: test.statusCode, + }, + } + result := responseWasNotFound(resp) + if test.expectedResult != result { + t.Fatalf("Expected '%+v' for status code '%d' - got '%+v'", + test.expectedResult, test.statusCode, result) + } + } +}