Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] suppress gzip diff unless fields are explicitly set #441

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions fastly/block_fastly_service_v1_gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (h *GzipServiceAttributeHandler) Process(d *schema.ResourceData, latestVers
// this and so we've updated the below code to convert the type asserted
// int into a uint before passing the value to gofastly.Uint().
if v, ok := modified["content_types"]; ok {
opts.ContentTypes = gofastly.String("")
set := v.(*schema.Set)
if len(set.List()) > 0 {
var s []string
Expand All @@ -137,6 +138,7 @@ func (h *GzipServiceAttributeHandler) Process(d *schema.ResourceData, latestVers
}
}
if v, ok := modified["extensions"]; ok {
opts.Extensions = gofastly.String("")
set := v.(*schema.Set)
if len(set.List()) > 0 {
var s []string
Expand Down Expand Up @@ -173,6 +175,33 @@ func (h *GzipServiceAttributeHandler) Read(d *schema.ResourceData, s *gofastly.S

gl := flattenGzips(gzipsList)

// Note: Although "content_types" and "extensions" fields are optional in spec,
// Fastly API will actually set the default value silently when these fields are not sent
// or an empty field value is sent. This will cause unexpected diff.
// We need to ignore these fields in the API response unless field values are explicitly set.
type IgnoreFields struct {
Name string
}
ignoreList := map[string][]IgnoreFields{}

for _, elem := range d.Get("gzip").(*schema.Set).List() {
name := elem.(map[string]interface{})["name"].(string)
if elem.(map[string]interface{})["content_types"].(*schema.Set).Len() == 0 {
ignoreList[name] = append(ignoreList[name], IgnoreFields{Name: "content_types"})
}
if elem.(map[string]interface{})["extensions"].(*schema.Set).Len() == 0 {
ignoreList[name] = append(ignoreList[name], IgnoreFields{Name: "extensions"})
}
}

for i, g := range gl {
if v, ok := ignoreList[g["name"].(string)]; ok && len(v) > 0 {
for _, sl := range v {
gl[i][sl.Name] = nil
}
}
}

if err := d.Set(h.GetKey(), gl); err != nil {
log.Printf("[WARN] Error setting Gzips for (%s): %s", d.Id(), err)
}
Expand Down
8 changes: 8 additions & 0 deletions fastly/block_fastly_service_v1_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ func testAccCheckFastlyServiceV1GzipsAttributes(service *gofastly.ServiceDetail,
// these ahead of time
lg.CreatedAt = nil
lg.UpdatedAt = nil
// If empty value is sent, default value is assigned automatically by the API
// and so we ignore these fields in response
if g.Extensions == "" {
lg.Extensions = ""
}
if g.ContentTypes == "" {
lg.ContentTypes = ""
}
if !reflect.DeepEqual(g, lg) {
return fmt.Errorf("Bad match Gzip match, expected (%#v), got (%#v)", g, lg)
}
Expand Down