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

storagezone: use d.Partial in Update instead of revertUpdateValues #2

Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 5 additions & 25 deletions internal/provider/resource_storagezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 55 additions & 0 deletions internal/provider/resource_storagezone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down