provider/google: Move 404 checking into a function in provider.go, call it from instance and IGM (#14190)
This commit is contained in:
parent
bfd42b0fa7
commit
b7d0140aaf
|
@ -3,6 +3,7 @@ package google
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
@ -251,3 +252,15 @@ func getNetworkNameFromSelfLink(network string) (string, error) {
|
||||||
|
|
||||||
return network, nil
|
return network, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleNotFoundError(err error, d *schema.ResourceData, resource string) error {
|
||||||
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
||||||
|
log.Printf("[WARN] Removing %s because it's gone", resource)
|
||||||
|
// The resource doesn't exist anymore
|
||||||
|
d.SetId("")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("Error reading %s: %s", resource, err)
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"google.golang.org/api/compute/v1"
|
"google.golang.org/api/compute/v1"
|
||||||
"google.golang.org/api/googleapi"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func stringScopeHashcode(v interface{}) int {
|
func stringScopeHashcode(v interface{}) int {
|
||||||
|
@ -361,16 +360,7 @@ func getInstance(config *Config, d *schema.ResourceData) (*compute.Instance, err
|
||||||
instance, err := config.clientCompute.Instances.Get(
|
instance, err := config.clientCompute.Instances.Get(
|
||||||
project, d.Get("zone").(string), d.Id()).Do()
|
project, d.Get("zone").(string), d.Id()).Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
return nil, handleNotFoundError(err, d, fmt.Sprintf("Instance %s", d.Get("name").(string)))
|
||||||
log.Printf("[WARN] Removing Instance %q because it's gone", d.Get("name").(string))
|
|
||||||
// The resource doesn't exist anymore
|
|
||||||
id := d.Id()
|
|
||||||
d.SetId("")
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("Resource %s no longer exists", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("Error reading instance: %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance, nil
|
return instance, nil
|
||||||
|
@ -713,13 +703,8 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
||||||
func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
config := meta.(*Config)
|
||||||
|
|
||||||
id := d.Id()
|
|
||||||
instance, err := getInstance(config, d)
|
instance, err := getInstance(config, d)
|
||||||
if err != nil {
|
if err != nil || instance == nil {
|
||||||
if strings.Contains(err.Error(), "no longer exists") {
|
|
||||||
log.Printf("[WARN] Google Compute Instance (%s) not found", id)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -222,7 +222,7 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
|
||||||
manager, e = config.clientCompute.InstanceGroupManagers.Get(project, zone.(string), d.Id()).Do()
|
manager, e = config.clientCompute.InstanceGroupManagers.Get(project, zone.(string), d.Id()).Do()
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return handleNotFoundError(e, d, fmt.Sprintf("Instance Group Manager %q", d.Get("name").(string)))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If the resource was imported, the only info we have is the ID. Try to find the resource
|
// If the resource was imported, the only info we have is the ID. Try to find the resource
|
||||||
|
|
Loading…
Reference in New Issue