handle 404 (Not Found) in Get operations

This commit is contained in:
Jon Perritt 2015-02-09 20:27:39 -07:00
parent 9b30ef4eb2
commit fafa946871
11 changed files with 32 additions and 10 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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))

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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)

View File

@ -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)

View File

@ -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)
}