Skip to content

Commit

Permalink
Change Delete so it resets all settings to default.
Browse files Browse the repository at this point in the history
  • Loading branch information
daboross committed May 1, 2024
1 parent fa6d3b4 commit d73d1c3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
12 changes: 7 additions & 5 deletions docs/guides/image_optimizer_default_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ Fastly Image Optimizer supports a variety of image formats and applies specific
The [Image Optimizer default settings](https://developer.fastly.com/reference/api/services/image-optimizer-default-settings/) API allows customers to configure
default settings for Image Optimizer requests, configuring the way images are optimized when not overridden by URL parameters on specific requests.

Not all customers are entitled to use these endpoints and so care needs to be given when configuring an `image_optimizer` block in your Terraform configuration.

Image Optimizer must also be enabled on the specific service. This can be achieved in terraform with the `product_enablement` block.
The service must have the Image Optimizer product enabled using the Product Enablement API, UI, or Terraform block to use the `image_optimizer` block.

## Example Usage

Expand Down Expand Up @@ -59,5 +57,9 @@ NOTE: When added, `image_optimizer_default_settings` will always set all default

## Delete

As Image Optimizer default settings will always have some value when a service has image optimizer enabled, deleting the `image_optimizer_default_settings`
block is a no-op.
Deleting the resource will reset all Image Optimizer default settings to their default values.

If deleting the resource errors due to Image Optimizer no longer being enabled on the service, then this error will be ignored.

When Image Optimizer is next re-enabled on a service, that service's Image Optimizer default settings will be reset - so a disabled service effectively already
has deleted/default Image Optimizer default settings.
86 changes: 84 additions & 2 deletions fastly/block_fastly_service_image_optimizer_default_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"log"
"net/http"
"strings"

gofastly "github.com/fastly/go-fastly/v9/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -248,7 +250,87 @@ func (h *ImageOptimizerDefaultSettingsServiceAttributeHandler) Update(_ context.

// Delete deletes the resource.
//
// Note: The API does not expose any way to reset default settings, so we don't have much to do here.
func (h *ImageOptimizerDefaultSettingsServiceAttributeHandler) Delete(_ context.Context, d *schema.ResourceData, _ map[string]any, _ int, conn *gofastly.Client) error {
// This resets Image Optimizer default settings to their defaults, to make it possible to easily undo any effect this block had.
//
// This assumes the service wasn't modified with the UI or any other non-terraform method. Given terraform's regular mode of operating within the world is to
// assume its in control of everything, I think that's quite a reasonable assumption.
func (h *ImageOptimizerDefaultSettingsServiceAttributeHandler) Delete(_ context.Context, d *schema.ResourceData, resource map[string]any, serviceVersion int, conn *gofastly.Client) error {
serviceID := d.Id()
log.Println("[DEBUG] Update Image Optimizer default settings")

apiInput := gofastly.UpdateImageOptimizerDefaultSettingsInput{
ServiceID: serviceID,
ServiceVersion: serviceVersion,
}

for key, value := range resource {
switch key {
case "resize_filter":
var resizeFilter gofastly.ResizeFilter
switch value.(string) {
case "lanczos3":
resizeFilter = gofastly.Lanczos3
case "lanczos2":
resizeFilter = gofastly.Lanczos2
case "bicubic":
resizeFilter = gofastly.Bicubic
case "bilinear":
resizeFilter = gofastly.Bilinear
case "nearest":
resizeFilter = gofastly.Nearest
default:
return fmt.Errorf("got unexpected resize_filter: %v", value)
}
apiInput.ResizeFilter = &resizeFilter
case "webp":
apiInput.Webp = gofastly.ToPointer(value.(bool))
case "webp_quality":
apiInput.WebpQuality = gofastly.ToPointer(value.(int))
case "jpeg_type":
var jpegType gofastly.JpegType
switch value.(string) {
case "auto":
jpegType = gofastly.Auto
case "baseline":
jpegType = gofastly.Baseline
case "progressive":
jpegType = gofastly.Progressive
default:
return fmt.Errorf("got unexpected jpeg_type: %v", value)
}
apiInput.JpegType = &jpegType
case "jpeg_quality":
apiInput.JpegQuality = gofastly.ToPointer(value.(int))
case "upscale":
apiInput.Upscale = gofastly.ToPointer(value.(bool))
case "allow_video":
apiInput.AllowVideo = gofastly.ToPointer(value.(bool))
case "name":
continue
default:
return fmt.Errorf("got unexpected image_optimizer_default_settings key: %v", key)
}
}

log.Printf("[DEBUG] Calling Image Optimizer default settings update API with parameters: %+v", apiInput)

if _, err := conn.UpdateImageOptimizerDefaultSettings(&apiInput); err != nil {
// inspect the error type for a title that has a message indicating the user cannot call the API because they do not have Image Optimizer
// enabled. For these users we want to skip the error so that we can allow them to clean up their Terraform state. (also, because the Image Optimizer
// default settings for services with Image Optimizer are effectively cleared by disabling Image Optimizer.)
if he, ok := err.(*gofastly.HTTPError); ok {
if he.StatusCode == http.StatusBadRequest {
for _, e := range he.Errors {
if strings.Contains(e.Detail, "Image Optimizer is not enabled on this service") {
return nil
}
}
}
}

return err

}

return nil
}
12 changes: 7 additions & 5 deletions templates/guides/image_optimizer_default_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ Fastly Image Optimizer supports a variety of image formats and applies specific
The [Image Optimizer default settings](https://developer.fastly.com/reference/api/services/image-optimizer-default-settings/) API allows customers to configure
default settings for Image Optimizer requests, configuring the way images are optimized when not overridden by URL parameters on specific requests.

Not all customers are entitled to use these endpoints and so care needs to be given when configuring an `image_optimizer` block in your Terraform configuration.

Image Optimizer must also be enabled on the specific service. This can be achieved in terraform with the `product_enablement` block.
The service must have the Image Optimizer product enabled using the Product Enablement API, UI, or Terraform block to use the `image_optimizer` block.

## Example Usage

Expand Down Expand Up @@ -59,5 +57,9 @@ NOTE: When added, `image_optimizer_default_settings` will always set all default

## Delete

As Image Optimizer default settings will always have some value when a service has image optimizer enabled, deleting the `image_optimizer_default_settings`
block is a no-op.
Deleting the resource will reset all Image Optimizer default settings to their default values.

If deleting the resource errors due to Image Optimizer no longer being enabled on the service, then this error will be ignored.

When Image Optimizer is next re-enabled on a service, that service's Image Optimizer default settings will be reset - so a disabled service effectively already
has deleted/default Image Optimizer default settings.

0 comments on commit d73d1c3

Please sign in to comment.