Skip to content

Commit

Permalink
r/aws_backup_report_plan: Wait for correct DeploymentStatus.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Mar 31, 2022
1 parent 706e6a0 commit 7423099
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
9 changes: 9 additions & 0 deletions internal/service/backup/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ const (
frameworkStatusUpdateInProgress = "UPDATE_IN_PROGRESS"
)

// CREATE_IN_PROGRESS | UPDATE_IN_PROGRESS | DELETE_IN_PROGRESS | COMPLETED

const (
reportPlanDeploymentStatusCompleted = "COMPLETED"
reportPlanDeploymentStatusCreateInProgress = "CREATE_IN_PROGRESS"
reportPlanDeploymentStatusDeleteInProgress = "DELETE_IN_PROGRESS"
reportPlanDeploymentStatusUpdateInProgress = "UPDATE_IN_PROGRESS"
)

const (
reportDeliveryChannelFormatCSV = "CSV"
reportDeliveryChannelFormatJSON = "JSON"
Expand Down
105 changes: 93 additions & 12 deletions internal/service/backup/report_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func resourceReportPlanCreate(d *schema.ResourceData, meta interface{}) error {
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)

input := &backup.CreateReportPlanInput{
IdempotencyToken: aws.String(resource.UniqueId()),
ReportDeliveryChannel: expandReportDeliveryChannel(d.Get("report_delivery_channel").([]interface{})),
Expand All @@ -135,14 +134,19 @@ func resourceReportPlanCreate(d *schema.ResourceData, meta interface{}) error {
input.ReportPlanTags = Tags(tags.IgnoreAWS())
}

log.Printf("[DEBUG] Creating Backup Report Plan: %#v", input)
resp, err := conn.CreateReportPlan(input)
log.Printf("[DEBUG] Creating Backup Report Plan: %s", input)
output, err := conn.CreateReportPlan(input)

if err != nil {
return fmt.Errorf("error creating Backup Report Plan: %w", err)
return fmt.Errorf("error creating Backup Report Plan (%s): %w", name, err)
}

// Set ID with the name since the name is unique for the report plan
d.SetId(aws.StringValue(resp.ReportPlanName))
// Set ID with the name since the name is unique for the report plan.
d.SetId(aws.StringValue(output.ReportPlanName))

if _, err := waitReportPlanCreated(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("error waiting for Backup Report Plan (%s) create: %w", d.Id(), err)
}

return resourceReportPlanRead(d, meta)
}
Expand Down Expand Up @@ -201,7 +205,7 @@ func resourceReportPlanRead(d *schema.ResourceData, meta interface{}) error {
func resourceReportPlanUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).BackupConn

if d.HasChanges("description", "report_delivery_channel", "report_plan_description", "report_setting") {
if d.HasChangesExcept("tags_all", "tags") {
input := &backup.UpdateReportPlanInput{
IdempotencyToken: aws.String(resource.UniqueId()),
ReportDeliveryChannel: expandReportDeliveryChannel(d.Get("report_delivery_channel").([]interface{})),
Expand All @@ -210,15 +214,21 @@ func resourceReportPlanUpdate(d *schema.ResourceData, meta interface{}) error {
ReportSetting: expandReportSetting(d.Get("report_setting").([]interface{})),
}

log.Printf("[DEBUG] Updating Backup Report Plan: %#v", input)
log.Printf("[DEBUG] Updating Backup Report Plan: %s", input)
_, err := conn.UpdateReportPlan(input)

if err != nil {
return fmt.Errorf("error updating Backup Report Plan (%s): %w", d.Id(), err)
}

if _, err := waitReportPlanUpdated(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("error waiting for Backup Report Plan (%s) update: %w", d.Id(), err)
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags for Backup Report Plan (%s): %w", d.Id(), err)
}
Expand All @@ -230,13 +240,17 @@ func resourceReportPlanUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceReportPlanDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).BackupConn

input := &backup.DeleteReportPlanInput{
log.Printf("[DEBUG] Deleting Backup Report Plan: %s", d.Id())
_, err := conn.DeleteReportPlan(&backup.DeleteReportPlanInput{
ReportPlanName: aws.String(d.Id()),
}
})

_, err := conn.DeleteReportPlan(input)
if err != nil {
return fmt.Errorf("error deleting Backup Report Plan: %s", err)
return fmt.Errorf("error deleting Backup Report Plan (%s): %w", d.Id(), err)
}

if _, err := waitReportPlanDeleted(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("error waiting for Backup Report Plan (%s) delete: %w", d.Id(), err)
}

return nil
Expand Down Expand Up @@ -352,3 +366,70 @@ func FindReportPlanByName(conn *backup.Backup, name string) (*backup.ReportPlan,

return output.ReportPlan, nil
}

func statusReportPlanDeployment(conn *backup.Backup, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindReportPlanByName(conn, name)

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

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

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

func waitReportPlanCreated(conn *backup.Backup, name string, timeout time.Duration) (*backup.ReportPlan, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{reportPlanDeploymentStatusCreateInProgress},
Target: []string{reportPlanDeploymentStatusCompleted},
Timeout: timeout,
Refresh: statusReportPlanDeployment(conn, name),
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*backup.ReportPlan); ok {
return output, err
}

return nil, err
}

func waitReportPlanDeleted(conn *backup.Backup, name string, timeout time.Duration) (*backup.ReportPlan, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{reportPlanDeploymentStatusDeleteInProgress},
Target: []string{},
Timeout: timeout,
Refresh: statusReportPlanDeployment(conn, name),
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*backup.ReportPlan); ok {
return output, err
}

return nil, err
}

func waitReportPlanUpdated(conn *backup.Backup, name string, timeout time.Duration) (*backup.ReportPlan, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{reportPlanDeploymentStatusUpdateInProgress},
Target: []string{reportPlanDeploymentStatusCompleted},
Timeout: timeout,
Refresh: statusReportPlanDeployment(conn, name),
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*backup.ReportPlan); ok {
return output, err
}

return nil, err
}

0 comments on commit 7423099

Please sign in to comment.