Skip to content

Commit

Permalink
Merge pull request #41678 from hashicorp/b-account-region-enable
Browse files Browse the repository at this point in the history
B account region enable
  • Loading branch information
nam054 authored Mar 6, 2025
2 parents 071d276 + 92268bb commit 955ae48
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changelog/41678.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_account_region: Prevent errors when the region is an ENABLING or DISABLING state during creation
```

```release-note:enhancement
resource/aws_account_region: Allow adoption of regions in an ENABLED or DISABLED state without an explicit import operation
```
32 changes: 22 additions & 10 deletions internal/service/account/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func resourceRegionUpdate(ctx context.Context, d *schema.ResourceData, meta inte
timeout = d.Timeout(schema.TimeoutUpdate)
}

status, err := findRegionOptStatus(ctx, conn, accountID, region)
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Account Region status: %s", err)
}

if v := d.Get(names.AttrEnabled).(bool); v {
input := account.EnableRegionInput{
RegionName: aws.String(region),
Expand All @@ -99,10 +104,11 @@ func resourceRegionUpdate(ctx context.Context, d *schema.ResourceData, meta inte
input.AccountId = aws.String(accountID)
}

_, err := conn.EnableRegion(ctx, &input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "enabling Account Region (%s): %s", id, err)
if requiresStatusChange(status.RegionOptStatus, true) {
_, err := conn.EnableRegion(ctx, &input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "enabling Account Region (%s): %s", id, err)
}
}

if _, err := waitRegionEnabled(ctx, conn, accountID, region, timeout); err != nil {
Expand All @@ -116,10 +122,11 @@ func resourceRegionUpdate(ctx context.Context, d *schema.ResourceData, meta inte
input.AccountId = aws.String(accountID)
}

_, err := conn.DisableRegion(ctx, &input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "enabling Account Region (%s): %s", id, err)
if requiresStatusChange(status.RegionOptStatus, false) {
_, err := conn.DisableRegion(ctx, &input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "disabling Account Region (%s): %s", id, err)
}
}

if _, err := waitRegionDisabled(ctx, conn, accountID, region, timeout); err != nil {
Expand Down Expand Up @@ -208,7 +215,6 @@ func waitRegionEnabled(ctx context.Context, conn *account.Client, accountID, reg
Target: enum.Slice(types.RegionOptStatusEnabled),
Refresh: statusRegionOptStatus(ctx, conn, accountID, region),
Timeout: timeout,
Delay: 1 * time.Minute,
PollInterval: 30 * time.Second,
}

Expand All @@ -227,7 +233,6 @@ func waitRegionDisabled(ctx context.Context, conn *account.Client, accountID, re
Target: enum.Slice(types.RegionOptStatusDisabled),
Refresh: statusRegionOptStatus(ctx, conn, accountID, region),
Timeout: timeout,
Delay: 1 * time.Minute,
PollInterval: 30 * time.Second,
}

Expand All @@ -239,3 +244,10 @@ func waitRegionDisabled(ctx context.Context, conn *account.Client, accountID, re

return nil, err
}

func requiresStatusChange(status types.RegionOptStatus, enable bool) bool {
if enable {
return status != types.RegionOptStatusEnabled && status != types.RegionOptStatusEnabling
}
return status != types.RegionOptStatusDisabled && status != types.RegionOptStatusDisabling
}

0 comments on commit 955ae48

Please sign in to comment.