diff --git a/internal/provider/resource_storagezone.go b/internal/provider/resource_storagezone.go index 967fd7d..e8ef344 100644 --- a/internal/provider/resource_storagezone.go +++ b/internal/provider/resource_storagezone.go @@ -272,14 +272,11 @@ func resourceStorageZoneUpdate(ctx context.Context, d *schema.ResourceData, meta updateErr := clt.StorageZone.Update(ctx, id, storageZone) if updateErr != nil { - // if our update failed then revert our values to their original - // state so that we can run an apply again. - revertErr := revertUpdateValues(d) - - if revertErr != nil { - return diagsErrFromErr("updating storage zone via API failed", revertErr) - } - + // The storagezone contains fields /custom_404_file_path) that are only updated by the + // provider and not retrieved via ReadContext(). This causes that we run into the bug + // https://github.com/hashicorp/terraform-plugin-sdk/issues/476. + // As workaround d.Partial(true) is called. + d.Partial(true) return diagsErrFromErr("updating storage zone via API failed", updateErr) } @@ -370,23 +367,6 @@ func storageZoneToResource(sz *bunny.StorageZone, d *schema.ResourceData) error return nil } -func revertUpdateValues(d *schema.ResourceData) error { - o, _ := d.GetChange(keyOriginURL) - if err := d.Set(keyOriginURL, o); err != nil { - return err - } - o, _ = d.GetChange(keyCustom404FilePath) - if err := d.Set(keyCustom404FilePath, o); err != nil { - return err - } - o, _ = d.GetChange(keyRewrite404To200) - if err := d.Set(keyRewrite404To200, o); err != nil { - return err - } - - return nil -} - // storageZoneFromResource returns a StorageZoneUpdateOptions API type that // has fields set to the values in d. func storageZoneFromResource(d *schema.ResourceData) *bunny.StorageZoneUpdateOptions { diff --git a/internal/provider/resource_storagezone_test.go b/internal/provider/resource_storagezone_test.go index 35369cc..4a0473c 100644 --- a/internal/provider/resource_storagezone_test.go +++ b/internal/provider/resource_storagezone_test.go @@ -295,6 +295,61 @@ func checkSzState(t *testing.T, resourceName string, wanted *bunny.StorageZone) } } +// TestAccFailedUpdateDoesNotApplychanges tests the scenario described in +// https://github.com/5-stones/terraform-provider-bunny/pull/1#discussion_r898134629 +func TestAccFailedUpdateDoesNotApplychanges(t *testing.T) { + attrs := storageZoneWanted{ + TerraformResourceName: "bunny_storagezone.mytest1", + Name: randResourceName(), + Region: "DE", + } + + resource.Test(t, resource.TestCase{ + Providers: testProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` +resource "bunny_storagezone" "mytest1" { + name = "%s" + region = "%s" + custom_404_file_path = "/error.html" +}`, + attrs.Name, + attrs.Region, + ), + Check: checkBasicStorageZoneAPIState(&attrs), + }, + // change custom_404_file_path to a value that spawns a generation error on bunny side + { + Config: fmt.Sprintf(` +resource "bunny_storagezone" "mytest1" { + name = "%s" + region = "%s" + custom_404_file_path = "abc" +}`, + attrs.Name, + attrs.Region, + ), + Check: checkBasicStorageZoneAPIState(&attrs), + ExpectError: regexp.MustCompile(".*updating storage zone via API failed.*"), + }, + { + Config: fmt.Sprintf(` +resource "bunny_storagezone" "mytest1" { + name = "%s" + region = "%s" + custom_404_file_path = "/error.html" +}`, + attrs.Name, + attrs.Region, + ), + PlanOnly: true, + }, + }, + CheckDestroy: checkStorageZoneNotExists(attrs.Name), + }) +} + // storageZoneDiffIgnoredFields contains a list of fieldsnames in a bunny.StorageZone struct that are ignored by szDiff. var storageZoneDiffIgnoredFields = map[string]struct{}{ "ID": {}, // computed field