Read from the zone set in state when possible in google_compute_autoscaler (#193)

This commit is contained in:
Riley Karson 2017-08-03 12:52:48 -07:00 committed by GitHub
parent 59e139b373
commit 9f429e7ed2

View File

@ -289,20 +289,37 @@ func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) err
if err != nil {
return err
}
var getAutoscaler = func(zone string) (interface{}, error) {
return config.clientCompute.Autoscalers.Get(project, zone, d.Id()).Do()
}
resource, err := getZonalResourceFromRegion(getAutoscaler, region, config.clientCompute, project)
if err != nil {
return err
var scaler *compute.Autoscaler
var e error
if zone, ok := d.GetOk("zone"); ok {
scaler, e = config.clientCompute.Autoscalers.Get(project, zone.(string), d.Id()).Do()
if e != nil {
return handleNotFoundError(e, d, fmt.Sprintf("Autoscaler %q", d.Id()))
}
} else {
// If the resource was imported, the only info we have is the ID. Try to find the resource
// by searching in the region of the project.
var resource interface{}
resource, e = getZonalResourceFromRegion(getAutoscaler, region, config.clientCompute, project)
if e != nil {
return e
}
scaler = resource.(*compute.Autoscaler)
}
if resource == nil {
if scaler == nil {
log.Printf("[WARN] Removing Autoscaler %q because it's gone", d.Get("name").(string))
d.SetId("")
return nil
}
scaler := resource.(*compute.Autoscaler)
zoneUrl := strings.Split(scaler.Zone, "/")
d.Set("self_link", scaler.SelfLink)
d.Set("name", scaler.Name)