From e5219ba56dd8887e81b77d89c6fbefb4a9dfc883 Mon Sep 17 00:00:00 2001 From: Andreas Kyrris Date: Fri, 30 Sep 2016 17:57:59 +0100 Subject: [PATCH] Fixes behaviour when azurerm resources disappear. Regressions were introduced when fixing https://github.com/hashicorp/terraform/pull/8607 . Specifically when resources in the statefile are deleted or missing in real life, then terraform plan would exit with an error when it recieved a 404 not found. The correct behaviour would be to show a plan with the offer to create the missing resources. --- .../providers/azurerm/resource_arm_availability_set.go | 8 ++++---- builtin/providers/azurerm/resource_arm_cdn_endpoint.go | 8 ++++---- builtin/providers/azurerm/resource_arm_cdn_profile.go | 8 ++++---- .../azurerm/resource_arm_local_network_gateway.go | 8 ++++---- .../azurerm/resource_arm_network_interface_card.go | 8 ++++---- .../azurerm/resource_arm_network_security_group.go | 8 ++++---- .../azurerm/resource_arm_network_security_rule.go | 8 ++++---- builtin/providers/azurerm/resource_arm_public_ip.go | 8 ++++---- builtin/providers/azurerm/resource_arm_route.go | 8 ++++---- builtin/providers/azurerm/resource_arm_route_table.go | 8 ++++---- .../providers/azurerm/resource_arm_storage_account.go | 8 ++++---- builtin/providers/azurerm/resource_arm_subnet.go | 8 ++++---- .../azurerm/resource_arm_template_deployment.go | 8 ++++---- .../azurerm/resource_arm_traffic_manager_endpoint.go | 8 ++++---- .../azurerm/resource_arm_traffic_manager_profile.go | 8 ++++---- .../providers/azurerm/resource_arm_virtual_machine.go | 8 ++++---- .../azurerm/resource_arm_virtual_machine_scale_set.go | 10 +++++----- .../providers/azurerm/resource_arm_virtual_network.go | 9 +++++---- .../azurerm/resource_arm_virtual_network_peering.go | 8 ++++---- 19 files changed, 78 insertions(+), 77 deletions(-) diff --git a/builtin/providers/azurerm/resource_arm_availability_set.go b/builtin/providers/azurerm/resource_arm_availability_set.go index b14cfb1ed61c..e56fb1b7f154 100644 --- a/builtin/providers/azurerm/resource_arm_availability_set.go +++ b/builtin/providers/azurerm/resource_arm_availability_set.go @@ -118,12 +118,12 @@ func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) er resp, err := availSetClient.Get(resGroup, name) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } 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 047de55dc64c..f4d3b1e55758 100644 --- a/builtin/providers/azurerm/resource_arm_cdn_endpoint.go +++ b/builtin/providers/azurerm/resource_arm_cdn_endpoint.go @@ -226,12 +226,12 @@ 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 { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } 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 2ab8cee42370..878394b5ea22 100644 --- a/builtin/providers/azurerm/resource_arm_cdn_profile.go +++ b/builtin/providers/azurerm/resource_arm_cdn_profile.go @@ -100,12 +100,12 @@ func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error { resp, err := cdnProfilesClient.Get(name, resGroup) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 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 0edaa8151f56..9cff34635120 100644 --- a/builtin/providers/azurerm/resource_arm_local_network_gateway.go +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway.go @@ -110,12 +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_network_interface_card.go b/builtin/providers/azurerm/resource_arm_network_interface_card.go index b5575efc0da3..e7f803890f3e 100644 --- a/builtin/providers/azurerm/resource_arm_network_interface_card.go +++ b/builtin/providers/azurerm/resource_arm_network_interface_card.go @@ -246,12 +246,12 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e resp, err := ifaceClient.Get(resGroup, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } 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 11a67a76a652..84049b88683a 100644 --- a/builtin/providers/azurerm/resource_arm_network_security_group.go +++ b/builtin/providers/azurerm/resource_arm_network_security_group.go @@ -192,12 +192,12 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{ resp, err := secGroupClient.Get(resGroup, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 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 e9ea8f5b59b6..be6f75a59b92 100644 --- a/builtin/providers/azurerm/resource_arm_network_security_rule.go +++ b/builtin/providers/azurerm/resource_arm_network_security_rule.go @@ -176,12 +176,12 @@ func resourceArmNetworkSecurityRuleRead(d *schema.ResourceData, meta interface{} resp, err := secRuleClient.Get(resGroup, networkSGName, sgRuleName) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } 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 001c42608aa1..db721a787a99 100644 --- a/builtin/providers/azurerm/resource_arm_public_ip.go +++ b/builtin/providers/azurerm/resource_arm_public_ip.go @@ -166,12 +166,12 @@ func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error { resp, err := publicIPClient.Get(resGroup, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } 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 e2802ddb2c01..44953c91ee0f 100644 --- a/builtin/providers/azurerm/resource_arm_route.go +++ b/builtin/providers/azurerm/resource_arm_route.go @@ -113,12 +113,12 @@ func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error { resp, err := routesClient.Get(resGroup, rtName, routeName) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } return nil } diff --git a/builtin/providers/azurerm/resource_arm_route_table.go b/builtin/providers/azurerm/resource_arm_route_table.go index dc488c4373ed..5558d07dc458 100644 --- a/builtin/providers/azurerm/resource_arm_route_table.go +++ b/builtin/providers/azurerm/resource_arm_route_table.go @@ -143,12 +143,12 @@ func resourceArmRouteTableRead(d *schema.ResourceData, meta interface{}) error { resp, err := routeTablesClient.Get(resGroup, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 resp.Properties.Subnets != nil { if len(*resp.Properties.Subnets) > 0 { diff --git a/builtin/providers/azurerm/resource_arm_storage_account.go b/builtin/providers/azurerm/resource_arm_storage_account.go index 7cefd16e4d22..fadfd075a911 100644 --- a/builtin/providers/azurerm/resource_arm_storage_account.go +++ b/builtin/providers/azurerm/resource_arm_storage_account.go @@ -256,12 +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 60fae813160f..796202c3e186 100644 --- a/builtin/providers/azurerm/resource_arm_subnet.go +++ b/builtin/providers/azurerm/resource_arm_subnet.go @@ -133,12 +133,12 @@ func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { resp, err := subnetClient.Get(resGroup, vnetName, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 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 2db462b89049..9349c9b6abfa 100644 --- a/builtin/providers/azurerm/resource_arm_template_deployment.go +++ b/builtin/providers/azurerm/resource_arm_template_deployment.go @@ -144,12 +144,12 @@ func resourceArmTemplateDeploymentRead(d *schema.ResourceData, meta interface{}) resp, err := deployClient.Get(resGroup, name) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } var outputs map[string]string if resp.Properties.Outputs != nil && len(*resp.Properties.Outputs) > 0 { diff --git a/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go b/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go index 935645e18140..ff341a52cb0b 100644 --- a/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go +++ b/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go @@ -153,12 +153,12 @@ func resourceArmTrafficManagerEndpointRead(d *schema.ResourceData, meta interfac resp, err := client.Get(resGroup, profileName, endpointType, name) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return nil + } return fmt.Errorf("Error making Read request on TrafficManager Endpoint %s: %s", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } endpoint := *resp.Properties diff --git a/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go b/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go index c000e3264ca6..38834b2a9d6a 100644 --- a/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go +++ b/builtin/providers/azurerm/resource_arm_traffic_manager_profile.go @@ -151,12 +151,12 @@ func resourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interface resp, err := client.Get(resGroup, name) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } profile := *resp.Properties diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine.go b/builtin/providers/azurerm/resource_arm_virtual_machine.go index 435f91c46298..e8aff8269628 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine.go @@ -520,12 +520,12 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err resp, err := vmClient.Get(resGroup, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 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 c7e4b1f6773a..9e2be1dc7c2f 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go @@ -432,13 +432,13 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac resp, err := vmScaleSetClient.Get(resGroup, name) if err != nil { + if resp.StatusCode == http.StatusNotFound { + log.Printf("[INFO] AzureRM Virtual Machine Scale Set (%s) Not Found. Removing from State", name) + d.SetId("") + return 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 - } 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 8c7ed205f3b8..71daad9096c4 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_network.go +++ b/builtin/providers/azurerm/resource_arm_virtual_network.go @@ -133,12 +133,13 @@ func resourceArmVirtualNetworkRead(d *schema.ResourceData, meta interface{}) err resp, err := vnetClient.Get(resGroup, name, "") if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } + 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 0c7ab3af770d..28e9d07c15fd 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_network_peering.go +++ b/builtin/providers/azurerm/resource_arm_virtual_network_peering.go @@ -124,12 +124,12 @@ func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface resp, err := client.Get(resGroup, vnetName, name) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return 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 - } peer := *resp.Properties