Skip to content

Commit

Permalink
Cosmetics.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Mar 7, 2023
1 parent 14497ed commit 215b1dc
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 110 deletions.
101 changes: 53 additions & 48 deletions internal/service/rds/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func ResourceSnapshot() *schema.Resource {
ReadWithoutTimeout: resourceSnapshotRead,
UpdateWithoutTimeout: resourceSnapshotUpdate,
DeleteWithoutTimeout: resourceSnapshotDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(20 * time.Minute),
Create: schema.DefaultTimeout(20 * time.Minute),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -130,34 +131,37 @@ func resourceSnapshotCreate(ctx context.Context, d *schema.ResourceData, meta in
conn := meta.(*conns.AWSClient).RDSConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))
dBInstanceIdentifier := d.Get("db_instance_identifier").(string)

params := &rds.CreateDBSnapshotInput{
DBInstanceIdentifier: aws.String(dBInstanceIdentifier),
DBSnapshotIdentifier: aws.String(d.Get("db_snapshot_identifier").(string)),
dbSnapshotID := d.Get("db_snapshot_identifier").(string)
input := &rds.CreateDBSnapshotInput{
DBInstanceIdentifier: aws.String(d.Get("db_instance_identifier").(string)),
DBSnapshotIdentifier: aws.String(dbSnapshotID),
Tags: Tags(tags.IgnoreAWS()),
}

resp, err := conn.CreateDBSnapshotWithContext(ctx, params)
output, err := conn.CreateDBSnapshotWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating AWS DB Snapshot (%s): %s", dBInstanceIdentifier, err)
return sdkdiag.AppendErrorf(diags, "creating RDS DB Snapshot (%s): %s", dbSnapshotID, err)
}
d.SetId(aws.StringValue(resp.DBSnapshot.DBSnapshotIdentifier))

if err := waitDBSnapshotAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for RDS DB Snapshot (%s) to be available: %s", d.Id(), err)
d.SetId(aws.StringValue(output.DBSnapshot.DBSnapshotIdentifier))

if err := waitDBSnapshotCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for RDS DB Snapshot (%s) create: %s", d.Id(), err)
}

if v, ok := d.GetOk("shared_accounts"); ok && v.(*schema.Set).Len() > 0 {
attrInput := &rds.ModifyDBSnapshotAttributeInput{
DBSnapshotIdentifier: aws.String(dBInstanceIdentifier),
input := &rds.ModifyDBSnapshotAttributeInput{
AttributeName: aws.String("restore"),
DBSnapshotIdentifier: aws.String(d.Id()),
ValuesToAdd: flex.ExpandStringSet(v.(*schema.Set)),
}

_, err := conn.ModifyDBSnapshotAttributeWithContext(ctx, attrInput)
_, err := conn.ModifyDBSnapshotAttributeWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "error modifying AWS DB Snapshot Attribute %s: %s", dBInstanceIdentifier, err)
return sdkdiag.AppendErrorf(diags, "modifying RDS DB Snapshot (%s) attribute: %s", d.Id(), err)
}
}

Expand All @@ -171,22 +175,23 @@ func resourceSnapshotRead(ctx context.Context, d *schema.ResourceData, meta inte
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

snapshot, err := FindDBSnapshotByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] RDS DB Snapshot (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
return diags
}

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

arn := aws.StringValue(snapshot.DBSnapshotArn)
d.Set("db_snapshot_identifier", snapshot.DBSnapshotIdentifier)
d.Set("db_instance_identifier", snapshot.DBInstanceIdentifier)
d.Set("allocated_storage", snapshot.AllocatedStorage)
d.Set("availability_zone", snapshot.AvailabilityZone)
d.Set("db_instance_identifier", snapshot.DBInstanceIdentifier)
d.Set("db_snapshot_arn", arn)
d.Set("db_snapshot_identifier", snapshot.DBSnapshotIdentifier)
d.Set("encrypted", snapshot.Encrypted)
d.Set("engine", snapshot.Engine)
d.Set("engine_version", snapshot.EngineVersion)
Expand Down Expand Up @@ -218,39 +223,18 @@ func resourceSnapshotRead(ctx context.Context, d *schema.ResourceData, meta inte
return sdkdiag.AppendErrorf(diags, "setting tags_all: %s", err)
}

attrInput := &rds.DescribeDBSnapshotAttributesInput{
input := &rds.DescribeDBSnapshotAttributesInput{
DBSnapshotIdentifier: aws.String(d.Id()),
}

attrResp, err := conn.DescribeDBSnapshotAttributesWithContext(ctx, attrInput)
if err != nil {
return sdkdiag.AppendErrorf(diags, "error describing AWS DB Snapshot Attribute %s: %s", d.Id(), err)
}

attr := attrResp.DBSnapshotAttributesResult.DBSnapshotAttributes[0]

d.Set("shared_accounts", flex.FlattenStringSet(attr.AttributeValues))

return diags
}

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

log.Printf("[DEBUG] Deleting RDS DB Snapshot: %s", d.Id())
_, err := conn.DeleteDBSnapshotWithContext(ctx, &rds.DeleteDBSnapshotInput{
DBSnapshotIdentifier: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, rds.ErrCodeDBSnapshotNotFoundFault) {
return diags
}
output, err := conn.DescribeDBSnapshotAttributesWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting RDS DB Snapshot (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "reading RDS DB Snapshot (%s) attribute: %s", d.Id(), err)
}

d.Set("shared_accounts", flex.FlattenStringSet(output.DBSnapshotAttributesResult.DBSnapshotAttributes[0].AttributeValues))

return diags
}

Expand All @@ -266,16 +250,17 @@ func resourceSnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta in
additionList := ns.Difference(os)
removalList := os.Difference(ns)

attrInput := &rds.ModifyDBSnapshotAttributeInput{
DBSnapshotIdentifier: aws.String(d.Id()),
input := &rds.ModifyDBSnapshotAttributeInput{
AttributeName: aws.String("restore"),
DBSnapshotIdentifier: aws.String(d.Id()),
ValuesToAdd: flex.ExpandStringSet(additionList),
ValuesToRemove: flex.ExpandStringSet(removalList),
}

_, err := conn.ModifyDBSnapshotAttributeWithContext(ctx, attrInput)
_, err := conn.ModifyDBSnapshotAttributeWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "error modifying AWS DB Snapshot Attribute %s: %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "modifying RDS DB Snapshot (%s) attribute: %s", d.Id(), err)
}
}

Expand All @@ -289,3 +274,23 @@ func resourceSnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta in

return diags
}

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

log.Printf("[DEBUG] Deleting RDS DB Snapshot: %s", d.Id())
_, err := conn.DeleteDBSnapshotWithContext(ctx, &rds.DeleteDBSnapshotInput{
DBSnapshotIdentifier: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, rds.ErrCodeDBSnapshotNotFoundFault) {
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting RDS DB Snapshot (%s): %s", d.Id(), err)
}

return diags
}
49 changes: 28 additions & 21 deletions internal/service/rds/snapshot_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func ResourceSnapshotCopy() *schema.Resource {
ReadWithoutTimeout: resourceSnapshotCopyRead,
UpdateWithoutTimeout: resourceSnapshotCopyUpdate,
DeleteWithoutTimeout: resourceSnapshotCopyDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -146,37 +147,43 @@ func resourceSnapshotCopyCreate(ctx context.Context, d *schema.ResourceData, met
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

in := &rds.CopyDBSnapshotInput{
targetDBSnapshotID := d.Get("target_db_snapshot_identifier").(string)
input := &rds.CopyDBSnapshotInput{
SourceDBSnapshotIdentifier: aws.String(d.Get("source_db_snapshot_identifier").(string)),
TargetDBSnapshotIdentifier: aws.String(d.Get("target_db_snapshot_identifier").(string)),
Tags: Tags(tags.IgnoreAWS()),
TargetDBSnapshotIdentifier: aws.String(targetDBSnapshotID),
}

if v, ok := d.GetOk("copy_tags"); ok {
in.CopyTags = aws.Bool(v.(bool))
input.CopyTags = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("destination_region"); ok {
input.DestinationRegion = aws.String(v.(string))
}

if v, ok := d.GetOk("kms_key_id"); ok {
in.KmsKeyId = aws.String(v.(string))
input.KmsKeyId = aws.String(v.(string))
}

if v, ok := d.GetOk("option_group_name"); ok {
in.OptionGroupName = aws.String(v.(string))
}
if v, ok := d.GetOk("destination_region"); ok {
in.DestinationRegion = aws.String(v.(string))
input.OptionGroupName = aws.String(v.(string))
}

if v, ok := d.GetOk("presigned_url"); ok {
in.PreSignedUrl = aws.String(v.(string))
input.PreSignedUrl = aws.String(v.(string))
}

out, err := conn.CopyDBSnapshotWithContext(ctx, in)
output, err := conn.CopyDBSnapshotWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "error creating RDS DB Snapshot Copy %s", err)
return sdkdiag.AppendErrorf(diags, "creating RDS DB Snapshot Copy (%s): %s", targetDBSnapshotID, err)
}

d.SetId(aws.StringValue(out.DBSnapshot.DBSnapshotIdentifier))
d.SetId(aws.StringValue(output.DBSnapshot.DBSnapshotIdentifier))

if err := waitDBSnapshotAvailable(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for RDS DB Snapshot Copy (%s) to be available: %s", d.Id(), err)
if err := waitDBSnapshotCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for RDS DB Snapshot Copy (%s) create: %s", d.Id(), err)
}

return append(diags, resourceSnapshotCopyRead(ctx, d, meta)...)
Expand All @@ -197,7 +204,7 @@ func resourceSnapshotCopyRead(ctx context.Context, d *schema.ResourceData, meta
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading RDS DB snapshot (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "reading RDS DB Snapshot Copy (%s): %s", d.Id(), err)
}

arn := aws.StringValue(snapshot.DBSnapshotArn)
Expand All @@ -222,18 +229,18 @@ func resourceSnapshotCopyRead(ctx context.Context, d *schema.ResourceData, meta
tags, err := ListTags(ctx, conn, arn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "error listing tags for RDS DB Snapshot (%s): %s", arn, err)
return sdkdiag.AppendErrorf(diags, "listing tags for RDS DB Snapshot Copy (%s): %s", arn, err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "error setting tags: %s", err)
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "error setting tags_all: %s", err)
return sdkdiag.AppendErrorf(diags, "setting tags_all: %s", err)
}

return diags
Expand All @@ -247,7 +254,7 @@ func resourceSnapshotCopyUpdate(ctx context.Context, d *schema.ResourceData, met
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Get("db_snapshot_arn").(string), o, n); err != nil {
sdkdiag.AppendErrorf(diags, "error updating RDS DB Snapshot (%s) tags: %s", d.Get("db_snapshot_arn").(string), err)
sdkdiag.AppendErrorf(diags, "updating RDS DB Snapshot Copy (%s) tags: %s", d.Get("db_snapshot_arn").(string), err)
}
}

Expand All @@ -258,7 +265,7 @@ func resourceSnapshotCopyDelete(ctx context.Context, d *schema.ResourceData, met
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).RDSConn()

log.Printf("[DEBUG] Deleting RDS DB Snapshot: %s", d.Id())
log.Printf("[DEBUG] Deleting RDS DB Snapshot Copy: %s", d.Id())
_, err := conn.DeleteDBSnapshotWithContext(ctx, &rds.DeleteDBSnapshotInput{
DBSnapshotIdentifier: aws.String(d.Id()),
})
Expand All @@ -268,7 +275,7 @@ func resourceSnapshotCopyDelete(ctx context.Context, d *schema.ResourceData, met
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting RDS DB Snapshot (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "deleting RDS DB Snapshot Copy (%s): %s", d.Id(), err)
}

return diags
Expand Down
26 changes: 10 additions & 16 deletions internal/service/rds/snapshot_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func testAccCheckSnapshotCopyDestroy(ctx context.Context) resource.TestCheckFunc
return err
}

return fmt.Errorf("RDS DB Snapshot %s still exists", rs.Primary.ID)
return fmt.Errorf("RDS DB Snapshot Copy %s still exists", rs.Primary.ID)
}

return nil
Expand All @@ -158,7 +158,7 @@ func testAccCheckSnapshotCopyExists(ctx context.Context, n string, v *rds.DBSnap
}

if rs.Primary.ID == "" {
return fmt.Errorf("No RDS DB Snapshot ID is set")
return fmt.Errorf("No RDS DB Snapshot Copy ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).RDSConn()
Expand All @@ -169,13 +169,13 @@ func testAccCheckSnapshotCopyExists(ctx context.Context, n string, v *rds.DBSnap
return err
}

v = output
*v = *output

return nil
}
}

func testAccSnapshotCopyBaseConfig(rName string) string {
func testAccSnapshotCopyConfig_base(rName string) string {
return fmt.Sprintf(`
data "aws_rds_engine_version" "default" {
engine = "mysql"
Expand All @@ -192,10 +192,10 @@ resource "aws_db_instance" "test" {
engine = data.aws_rds_engine_version.default.engine
engine_version = data.aws_rds_engine_version.default.version
instance_class = data.aws_rds_orderable_db_instance.test.instance_class
name = "baz"
name = "test"
identifier = %[1]q
password = "barbarbarbar"
username = "foo"
password = "avoid-plaintext-passwords"
username = "tfacctest"
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.${data.aws_rds_engine_version.default.parameter_group_family}"
Expand All @@ -209,19 +209,15 @@ resource "aws_db_snapshot" "test" {
}

func testAccSnapshotCopyConfig_basic(rName string) string {
return acctest.ConfigCompose(
testAccSnapshotCopyBaseConfig(rName),
fmt.Sprintf(`
return acctest.ConfigCompose(testAccSnapshotCopyConfig_base(rName), fmt.Sprintf(`
resource "aws_db_snapshot_copy" "test" {
source_db_snapshot_identifier = aws_db_snapshot.test.db_snapshot_arn
target_db_snapshot_identifier = "%[1]s-target"
}`, rName))
}

func testAccSnapshotCopyConfig_tags1(rName, tagKey, tagValue string) string {
return acctest.ConfigCompose(
testAccSnapshotCopyBaseConfig(rName),
fmt.Sprintf(`
return acctest.ConfigCompose(testAccSnapshotCopyConfig_base(rName), fmt.Sprintf(`
resource "aws_db_snapshot_copy" "test" {
source_db_snapshot_identifier = aws_db_snapshot.test.db_snapshot_arn
target_db_snapshot_identifier = "%[1]s-target"
Expand All @@ -233,9 +229,7 @@ resource "aws_db_snapshot_copy" "test" {
}

func testAccSnapshotCopyConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return acctest.ConfigCompose(
testAccSnapshotCopyBaseConfig(rName),
fmt.Sprintf(`
return acctest.ConfigCompose(testAccSnapshotCopyConfig_base(rName), fmt.Sprintf(`
resource "aws_db_snapshot_copy" "test" {
source_db_snapshot_identifier = aws_db_snapshot.test.db_snapshot_arn
target_db_snapshot_identifier = "%[1]s-target"
Expand Down
Loading

0 comments on commit 215b1dc

Please sign in to comment.