Skip to content

Commit

Permalink
decoders for backend services and consistent hash (#3468) (#2044)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored May 7, 2020
1 parent 0ffb383 commit afa7df8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3468.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: Remove permadiff or errors on update for `google_compute_backend_service` and `google_compute_region_backend_service` when `consistent_hash` values were previously set on backend service but are not supported by updated value of `locality_lb_policy`
```
13 changes: 13 additions & 0 deletions google-beta/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3261,5 +3261,18 @@ func resourceComputeBackendServiceDecoder(d *schema.ResourceData, meta interface
delete(res, "iap")
}

// Requests with consistentHash will error for specific values of
// localityLbPolicy. However, the API will not remove it if the backend
// service is updated to from supporting to non-supporting localityLbPolicy
// (e.g. RING_HASH to RANDOM), which causes an error on subsequent update.
// In order to prevent errors, we ignore any consistentHash returned
// from the API when the localityLbPolicy doesn't support it.
if v, ok := res["localityLbPolicy"]; ok {
lbPolicy := v.(string)
if lbPolicy != "MAGLEV" && lbPolicy != "RING_HASH" {
delete(res, "consistentHash")
}
}

return res, nil
}
29 changes: 29 additions & 0 deletions google-beta/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,18 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
return handleNotFoundError(err, d, fmt.Sprintf("ComputeRegionBackendService %q", d.Id()))
}

res, err = resourceComputeRegionBackendServiceDecoder(d, meta, res)
if err != nil {
return err
}

if res == nil {
// Decoding the object has resulted in it being gone. It may be marked deleted
log.Printf("[DEBUG] Removing ComputeRegionBackendService because it no longer exists.")
d.SetId("")
return nil
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
Expand Down Expand Up @@ -2895,3 +2907,20 @@ func resourceComputeRegionBackendServiceEncoder(d *schema.ResourceData, meta int
obj["backends"] = backends
return obj, nil
}

func resourceComputeRegionBackendServiceDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
// Requests with consistentHash will error for specific values of
// localityLbPolicy. However, the API will not remove it if the backend
// service is updated to from supporting to non-supporting localityLbPolicy
// (e.g. RING_HASH to RANDOM), which causes an error on subsequent update.
// In order to prevent errors, we ignore any consistentHash returned
// from the API when the localityLbPolicy doesn't support it.
if v, ok := res["localityLbPolicy"]; ok {
lbPolicy := v.(string)
if lbPolicy != "MAGLEV" && lbPolicy != "RING_HASH" {
delete(res, "consistentHash")
}
}

return res, nil
}

0 comments on commit afa7df8

Please sign in to comment.