Skip to content

Commit

Permalink
r/aws_lightsail_database: Add 'waitDatabasePubliclyAccessibleModified'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jan 3, 2023
1 parent 95a773b commit 243b2f6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
6 changes: 6 additions & 0 deletions internal/service/lightsail/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ func resourceDatabaseUpdate(ctx context.Context, d *schema.ResourceData, meta in
}
}

if d.HasChange("publicly_accessible") {
if err := waitDatabasePubliclyAccessibleModified(ctx, conn, aws.String(d.Id()), d.Get("publicly_accessible").(bool)); err != nil {
return diag.Errorf("waiting for Lightsail Relational Database (%s) publicly accessible update: %s", d.Id(), err)
}
}

// Some Operations can complete before the Database enters the Available state. Added a waiter to make sure the Database is available before continuing.
if _, err = waitDatabaseModified(ctx, conn, aws.String(d.Id())); err != nil {
return diag.Errorf("waiting for Lightsail Relational Database (%s) to become available: %s", d.Id(), err)
Expand Down
26 changes: 24 additions & 2 deletions internal/service/lightsail/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,30 @@ func statusDatabaseBackupRetention(conn *lightsail.Lightsail, db *string) resour
return nil, "Failed", fmt.Errorf("Error retrieving Database info for (%s)", dbValue)
}

log.Printf("[DEBUG] Lightsail Database (%s) Backup Retention setting is currently %t", dbValue, *output.RelationalDatabase.BackupRetentionEnabled)
return output, strconv.FormatBool(*output.RelationalDatabase.BackupRetentionEnabled), nil
return output, strconv.FormatBool(aws.BoolValue(output.RelationalDatabase.BackupRetentionEnabled)), nil
}
}

func statusDatabasePubliclyAccessible(conn *lightsail.Lightsail, db *string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &lightsail.GetRelationalDatabaseInput{
RelationalDatabaseName: db,
}

dbValue := aws.StringValue(db)
log.Printf("[DEBUG] Checking if Lightsail Database (%s) Backup Retention setting has been updated.", dbValue)

output, err := conn.GetRelationalDatabase(input)

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

if output.RelationalDatabase == nil {
return nil, "Failed", fmt.Errorf("Error retrieving Database info for (%s)", dbValue)
}

return output, strconv.FormatBool(aws.BoolValue(output.RelationalDatabase.PubliclyAccessible)), nil
}
}

Expand Down
25 changes: 22 additions & 3 deletions internal/service/lightsail/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func waitDatabaseModified(ctx context.Context, conn *lightsail.Lightsail, db *st

// waitDatabaseBackupRetentionModified waits for a Modified BackupRetention on Database return available

func waitDatabaseBackupRetentionModified(ctx context.Context, conn *lightsail.Lightsail, db *string, status *bool) error {
func waitDatabaseBackupRetentionModified(ctx context.Context, conn *lightsail.Lightsail, db *string, target bool) error {
stateConf := &resource.StateChangeConf{
Pending: []string{strconv.FormatBool(!aws.BoolValue(status))},
Target: []string{strconv.FormatBool(aws.BoolValue(status))},
Pending: []string{strconv.FormatBool(!target)},
Target: []string{strconv.FormatBool(target)},
Refresh: statusDatabaseBackupRetention(conn, db),
Timeout: DatabaseTimeout,
Delay: DatabaseDelay,
Expand All @@ -95,6 +95,25 @@ func waitDatabaseBackupRetentionModified(ctx context.Context, conn *lightsail.Li
return err
}

func waitDatabasePubliclyAccessibleModified(ctx context.Context, conn *lightsail.Lightsail, db *string, target bool) error {
stateConf := &resource.StateChangeConf{
Pending: []string{strconv.FormatBool(!target)},
Target: []string{strconv.FormatBool(target)},
Refresh: statusDatabasePubliclyAccessible(conn, db),
Timeout: DatabaseTimeout,
Delay: DatabaseDelay,
MinTimeout: DatabaseMinTimeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if _, ok := outputRaw.(*lightsail.GetRelationalDatabaseOutput); ok {
return err
}

return err
}

func waitContainerServiceCreated(ctx context.Context, conn *lightsail.Lightsail, serviceName string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{lightsail.ContainerServiceStatePending},
Expand Down

0 comments on commit 243b2f6

Please sign in to comment.