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

ressource/aws_budgets_budget_action : No longer waits for action to have completed running or be pending #33015

Merged
merged 5 commits into from
Aug 15, 2023
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
3 changes: 3 additions & 0 deletions .changelog/33015.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_budgets_budget_action: No longer times out when creating a non-triggered action
```
98 changes: 29 additions & 69 deletions internal/service/budgets/budget_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand All @@ -38,8 +39,9 @@ func ResourceBudgetAction() *schema.Resource {
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Create: schema.DefaultTimeout(5 * time.Minute), // unneeded, but a breaking change to remove
Update: schema.DefaultTimeout(5 * time.Minute), // unneeded, but a breaking change to remove
Delete: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -221,6 +223,7 @@ func ResourceBudgetAction() *schema.Resource {
}

func resourceBudgetActionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BudgetsConn(ctx)

accountID := d.Get("account_id").(string)
Expand All @@ -244,7 +247,7 @@ func resourceBudgetActionCreate(ctx context.Context, d *schema.ResourceData, met
}, budgets.ErrCodeAccessDeniedException)

if err != nil {
return diag.Errorf("creating Budget Action: %s", err)
return sdkdiag.AppendErrorf(diags, "creating Budget Action: %s", err)
}

output := outputRaw.(*budgets.CreateBudgetActionOutput)
Expand All @@ -253,20 +256,17 @@ func resourceBudgetActionCreate(ctx context.Context, d *schema.ResourceData, met

d.SetId(BudgetActionCreateResourceID(accountID, actionID, budgetName))

if _, err := waitActionAvailable(ctx, conn, accountID, actionID, budgetName, d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for Budget Action (%s) create: %s", d.Id(), err)
}

return resourceBudgetActionRead(ctx, d, meta)
return append(diags, resourceBudgetActionRead(ctx, d, meta)...)
}

func resourceBudgetActionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BudgetsConn(ctx)

accountID, actionID, budgetName, err := BudgetActionParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
return sdkdiag.AppendFromErr(diags, err)
}

output, err := FindActionByThreePartKey(ctx, conn, accountID, actionID, budgetName)
Expand All @@ -278,13 +278,13 @@ func resourceBudgetActionRead(ctx context.Context, d *schema.ResourceData, meta
}

if err != nil {
return diag.Errorf("reading Budget Action (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "reading Budget Action (%s): %s", d.Id(), err)
}

d.Set("account_id", accountID)
d.Set("action_id", actionID)
if err := d.Set("action_threshold", flattenBudgetActionActionThreshold(output.ActionThreshold)); err != nil {
return diag.Errorf("setting action_threshold: %s", err)
return sdkdiag.AppendErrorf(diags, "setting action_threshold: %s", err)
}
d.Set("action_type", output.ActionType)
d.Set("approval_model", output.ApprovalModel)
Expand All @@ -297,25 +297,26 @@ func resourceBudgetActionRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("arn", arn.String())
d.Set("budget_name", budgetName)
if err := d.Set("definition", flattenBudgetActionDefinition(output.Definition)); err != nil {
return diag.Errorf("setting definition: %s", err)
return sdkdiag.AppendErrorf(diags, "setting definition: %s", err)
}
d.Set("execution_role_arn", output.ExecutionRoleArn)
d.Set("notification_type", output.NotificationType)
d.Set("status", output.Status)
if err := d.Set("subscriber", flattenBudgetActionSubscriber(output.Subscribers)); err != nil {
return diag.Errorf("setting subscriber: %s", err)
return sdkdiag.AppendErrorf(diags, "setting subscriber: %s", err)
}

return nil
return diags
}

func resourceBudgetActionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BudgetsConn(ctx)

accountID, actionID, budgetName, err := BudgetActionParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
return sdkdiag.AppendFromErr(diags, err)
}

input := &budgets.UpdateBudgetActionInput{
Expand Down Expand Up @@ -351,41 +352,40 @@ func resourceBudgetActionUpdate(ctx context.Context, d *schema.ResourceData, met
_, err = conn.UpdateBudgetActionWithContext(ctx, input)

if err != nil {
return diag.Errorf("updating Budget Action (%s): %s", d.Id(), err)
}

if _, err := waitActionAvailable(ctx, conn, accountID, actionID, budgetName, d.Timeout(schema.TimeoutUpdate)); err != nil {
return diag.Errorf("waiting for Budget Action (%s) update: %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "updating Budget Action (%s): %s", d.Id(), err)
}

return resourceBudgetActionRead(ctx, d, meta)
return append(diags, resourceBudgetActionRead(ctx, d, meta)...)
}

func resourceBudgetActionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BudgetsConn(ctx)

accountID, actionID, budgetName, err := BudgetActionParseResourceID(d.Id())

if err != nil {
return diag.FromErr(err)
return sdkdiag.AppendFromErr(diags, err)
}

log.Printf("[DEBUG] Deleting Budget Action: %s", d.Id())
_, err = conn.DeleteBudgetActionWithContext(ctx, &budgets.DeleteBudgetActionInput{
AccountId: aws.String(accountID),
ActionId: aws.String(actionID),
BudgetName: aws.String(budgetName),
})
_, err = tfresource.RetryWhenAWSErrCodeEquals(ctx, d.Timeout(schema.TimeoutDelete), func() (any, error) {
return conn.DeleteBudgetActionWithContext(ctx, &budgets.DeleteBudgetActionInput{
AccountId: aws.String(accountID),
ActionId: aws.String(actionID),
BudgetName: aws.String(budgetName),
})
}, budgets.ErrCodeResourceLockedException)

if tfawserr.ErrCodeEquals(err, budgets.ErrCodeNotFoundException) {
return nil
}

if err != nil {
return diag.Errorf("deleting Budget Action (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "deleting Budget Action (%s): %s", d.Id(), err)
}

return nil
return diags
}

const budgetActionResourceIDSeparator = ":"
Expand Down Expand Up @@ -438,46 +438,6 @@ func FindActionByThreePartKey(ctx context.Context, conn *budgets.Budgets, accoun
return output.Action, nil
}

func statusAction(ctx context.Context, conn *budgets.Budgets, accountID, actionID, budgetName string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindActionByThreePartKey(ctx, conn, accountID, actionID, budgetName)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.Status), nil
}
}

func waitActionAvailable(ctx context.Context, conn *budgets.Budgets, accountID, actionID, budgetName string, timeout time.Duration) (*budgets.Action, error) { //nolint:unparam
stateConf := &retry.StateChangeConf{
Pending: []string{
budgets.ActionStatusExecutionInProgress,
budgets.ActionStatusStandby,
},
Target: []string{
budgets.ActionStatusExecutionSuccess,
budgets.ActionStatusExecutionFailure,
budgets.ActionStatusPending,
},
Refresh: statusAction(ctx, conn, accountID, actionID, budgetName),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if v, ok := outputRaw.(*budgets.Action); ok {
return v, err
}

return nil, err
}

func expandBudgetActionActionThreshold(l []interface{}) *budgets.ActionThreshold {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down
Loading
Loading