Skip to content

Commit

Permalink
fix(rate_limiter): lookup new ID before actioning a deletion (#715)
Browse files Browse the repository at this point in the history
This is because when we clone a service, it creates a new ratelimit ID associated with the clone. The fix is try to find the new ratelimit id from the clone and remove it.
  • Loading branch information
rayzorinc authored Jun 22, 2023
1 parent 3fe3b59 commit f180aa9
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions fastly/block_fastly_service_ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,35 @@ func (h *RateLimiterAttributeHandler) Update(_ context.Context, d *schema.Resour

// Delete deletes the resource.
func (h *RateLimiterAttributeHandler) Delete(_ context.Context, d *schema.ResourceData, resource map[string]any, serviceVersion int, conn *gofastly.Client) error {
input := h.createDeleteERLInput(d.Id(), serviceVersion, resource)
var rateLimiterID string

// IMPORTANT: Cloning a Service will result in new Rate Limiter IDs.
//
// This means, to update a Rate Limiter we have to first get a list of all
// available Rate Limiters on the cloned service version, then identify the
// one we need by its 'name' (which the API doesn't treat as unique, so
// multiple Rate Limiters in theory can have the same name, but in Terraform
// we enforce that the name must be unique otherwise we can't safely determine
// the Rate Limiter ID).

erls, err := conn.ListERLs(&gofastly.ListERLsInput{
ServiceID: d.Id(),
ServiceVersion: serviceVersion,
})
if err != nil {
return err
}

for _, e := range erls {
if e.Name == resource["name"].(string) {
rateLimiterID = e.ID
break
}
}
input := h.createDeleteERLInput(rateLimiterID)

log.Printf("[DEBUG] Delete Rate Limiter: %#v", input)
err := conn.DeleteERL(&input)
err = conn.DeleteERL(&input)
if errRes, ok := err.(*gofastly.HTTPError); ok {
if errRes.StatusCode != 404 {
return err
Expand All @@ -267,9 +292,9 @@ func (h *RateLimiterAttributeHandler) Delete(_ context.Context, d *schema.Resour
return nil
}

func (h *RateLimiterAttributeHandler) createDeleteERLInput(service string, latestVersion int, resource map[string]any) gofastly.DeleteERLInput {
func (h *RateLimiterAttributeHandler) createDeleteERLInput(rateLimiterID string) gofastly.DeleteERLInput {
return gofastly.DeleteERLInput{
ERLID: resource["ratelimiter_id"].(string),
ERLID: rateLimiterID,
}
}

Expand Down

0 comments on commit f180aa9

Please sign in to comment.