diff --git a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go index ea2d99a65..ab85317bb 100644 --- a/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go +++ b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1.go @@ -130,7 +130,7 @@ func resourceBlockStorageVolumeV1Read(d *schema.ResourceData, meta interface{}) v, err := volumes.Get(blockStorageClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack volume: %s", err) + return CheckDeleted(d, err, "volume") } log.Printf("\n\ngot volume: %+v\n\n", v) diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index bcf30a6ee..a60279c3d 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -252,7 +252,7 @@ func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) err server, err := servers.Get(computeClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack server: %s", err) + return CheckDeleted(d, err, "server") } log.Printf("[DEBUG] Retreived Server %s: %+v", d.Id(), server) diff --git a/builtin/providers/openstack/resource_openstack_compute_keypair_v2.go b/builtin/providers/openstack/resource_openstack_compute_keypair_v2.go index 8cdc84913..9c1417412 100644 --- a/builtin/providers/openstack/resource_openstack_compute_keypair_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_keypair_v2.go @@ -65,7 +65,7 @@ func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) erro kp, err := keypairs.Get(computeClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack keypair: %s", err) + return CheckDeleted(d, err, "keypair") } d.Set("region", d.Get("region").(string)) diff --git a/builtin/providers/openstack/resource_openstack_compute_secgroup_v2.go b/builtin/providers/openstack/resource_openstack_compute_secgroup_v2.go index f1d9ae714..cb61e7458 100644 --- a/builtin/providers/openstack/resource_openstack_compute_secgroup_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_secgroup_v2.go @@ -111,7 +111,7 @@ func resourceComputeSecGroupV2Read(d *schema.ResourceData, meta interface{}) err sg, err := secgroups.Get(computeClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack security group: %s", err) + return CheckDeleted(d, err, "security group") } d.Set("region", d.Get("region").(string)) diff --git a/builtin/providers/openstack/resource_openstack_lb_monitor_v1.go b/builtin/providers/openstack/resource_openstack_lb_monitor_v1.go index 7483b17e4..0761ec1f4 100644 --- a/builtin/providers/openstack/resource_openstack_lb_monitor_v1.go +++ b/builtin/providers/openstack/resource_openstack_lb_monitor_v1.go @@ -120,7 +120,7 @@ func resourceLBMonitorV1Read(d *schema.ResourceData, meta interface{}) error { m, err := monitors.Get(networkingClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack LB Monitor: %s", err) + return CheckDeleted(d, err, "LB monitor") } log.Printf("[DEBUG] Retreived OpenStack LB Monitor %s: %+v", d.Id(), m) diff --git a/builtin/providers/openstack/resource_openstack_lb_pool_v1.go b/builtin/providers/openstack/resource_openstack_lb_pool_v1.go index a7b5abbaf..7e3abdbf7 100644 --- a/builtin/providers/openstack/resource_openstack_lb_pool_v1.go +++ b/builtin/providers/openstack/resource_openstack_lb_pool_v1.go @@ -154,7 +154,7 @@ func resourceLBPoolV1Read(d *schema.ResourceData, meta interface{}) error { p, err := pools.Get(networkingClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack LB Pool: %s", err) + return CheckDeleted(d, err, "LB pool") } log.Printf("[DEBUG] Retreived OpenStack LB Pool %s: %+v", d.Id(), p) diff --git a/builtin/providers/openstack/resource_openstack_lb_vip_v1.go b/builtin/providers/openstack/resource_openstack_lb_vip_v1.go index cef3977c8..6181bc8a5 100644 --- a/builtin/providers/openstack/resource_openstack_lb_vip_v1.go +++ b/builtin/providers/openstack/resource_openstack_lb_vip_v1.go @@ -133,7 +133,7 @@ func resourceLBVipV1Read(d *schema.ResourceData, meta interface{}) error { p, err := vips.Get(networkingClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack LB VIP: %s", err) + return CheckDeleted(d, err, "LB VIP") } log.Printf("[DEBUG] Retreived OpenStack LB VIP %s: %+v", d.Id(), p) diff --git a/builtin/providers/openstack/resource_openstack_networking_floatingip_v2.go b/builtin/providers/openstack/resource_openstack_networking_floatingip_v2.go index 410fb0e39..fb4965768 100644 --- a/builtin/providers/openstack/resource_openstack_networking_floatingip_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_floatingip_v2.go @@ -70,7 +70,7 @@ func resourceNetworkFloatingIPV2Read(d *schema.ResourceData, meta interface{}) e floatingIP, err := floatingips.Get(networkClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving floatingIP: %s", err) + return CheckDeleted(d, err, "floating IP") } d.Set("region", d.Get("region").(string)) diff --git a/builtin/providers/openstack/resource_openstack_networking_network_v2.go b/builtin/providers/openstack/resource_openstack_networking_network_v2.go index fc420b2e9..bf556f418 100644 --- a/builtin/providers/openstack/resource_openstack_networking_network_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_network_v2.go @@ -98,7 +98,7 @@ func resourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) e n, err := networks.Get(networkingClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack Neutron Network: %s", err) + return CheckDeleted(d, err, "network") } log.Printf("[DEBUG] Retreived Network %s: %+v", d.Id(), n) diff --git a/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go b/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go index 74a43e27c..8462611df 100644 --- a/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go @@ -155,7 +155,7 @@ func resourceNetworkingSubnetV2Read(d *schema.ResourceData, meta interface{}) er s, err := subnets.Get(networkingClient, d.Id()).Extract() if err != nil { - return fmt.Errorf("Error retrieving OpenStack Neutron Subnet: %s", err) + return CheckDeleted(d, err, "subnet") } log.Printf("[DEBUG] Retreived Subnet %s: %+v", d.Id(), s) diff --git a/builtin/providers/openstack/util.go b/builtin/providers/openstack/util.go new file mode 100644 index 000000000..7ed9f8f5f --- /dev/null +++ b/builtin/providers/openstack/util.go @@ -0,0 +1,22 @@ +package openstack + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/racker/perigee" +) + +// CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so, +// sets the resource ID to the empty string instead of throwing an error. +func CheckDeleted(d *schema.ResourceData, err error, resource string) error { + errCode, ok := err.(*perigee.UnexpectedResponseCodeError) + if !ok { + return fmt.Errorf("Error retrieving OpenStack %s: %s", resource, err) + } + if errCode.Actual == 404 { + d.SetId("") + return nil + } + return fmt.Errorf("Error retrieving OpenStack %s: %s", resource, err) +}