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

Add shared_accounts parameter to db_snapshot_copy #34843

Merged
merged 32 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a081a27
Add shared_accounts parameter to db_snapshot_copy
OpenGLShaders Dec 9, 2023
d72a082
formatting
OpenGLShaders Dec 10, 2023
624fb61
make it compile
OpenGLShaders Dec 10, 2023
0dac5ca
adding tests
OpenGLShaders Dec 10, 2023
c3e09ef
Merge branch 'main' into HEAD
ewbankkit Jul 23, 2024
540935a
Run 'make fix-constants PKG=rds'.
ewbankkit Jul 23, 2024
bfb2c0f
Correct CHANGELOG entry.
ewbankkit Jul 23, 2024
550b6a5
Cosmetics.
ewbankkit Jul 23, 2024
369eb17
Acceptance test output:
ewbankkit Jul 23, 2024
2bd3fd3
Add 'tfslices.PredicateValue'.
ewbankkit Jul 23, 2024
e386006
r/aws_db_snapshot: Migrate to AWS SDK for Go v2.
ewbankkit Jul 23, 2024
b0c847e
d/aws_db_snapshot: Migrate to AWS SDK for Go v2.
ewbankkit Jul 23, 2024
6451df9
r/aws_db_snapshot_copy: Migrate to AWS SDK for Go v2.
ewbankkit Jul 23, 2024
f061e67
Acceptance test output:
ewbankkit Jul 24, 2024
4c72379
Add 'TestAccRDSSnapshotCopy_destinationRegion'.
ewbankkit Jul 24, 2024
09e90c5
Acceptance test output:
ewbankkit Jul 24, 2024
917a7e9
r/aws_db_snapshot: Add '% progress' to 'waitDBSnapshotCreated'.
ewbankkit Jul 24, 2024
d5a480c
r/aws_db_snapshot_copy: Migrate 'destination_region' functionality to…
ewbankkit Jul 24, 2024
86dd6f8
d/aws_rds_reserved_instance_offering: Migrate to AWS SDK for Go v2.
ewbankkit Jul 24, 2024
4a62b96
r/aws_db_parameter_group: Migrate to AWS SDK for Go v2.
ewbankkit Jul 25, 2024
24a397b
Fix 'TestAccRDSParameterGroup_caseParameters'.
ewbankkit Jul 25, 2024
18f2da3
r/aws_rds_cluster_parameter_group: Migrate to AWS SDK for Go v2.
ewbankkit Jul 25, 2024
6826612
rds: Move flatteners and expanders around.
ewbankkit Jul 25, 2024
9606c59
r/aws_rds_cluster_endpoint: Migrate to AWS SDK for Go v2.
ewbankkit Jul 25, 2024
95815c3
r/aws_rds_cluster_endpoint: Fix acceptance tests.
ewbankkit Jul 25, 2024
ee1b5ad
r/aws_rds_cluster_role_association: Migrate to AWS SDK for Go v2.
ewbankkit Jul 25, 2024
8df8631
Fix semgrep 'ci.semgrep.pluginsdk.append-Read-to-diags'.
ewbankkit Jul 25, 2024
07d29de
Fix golangci-lint 'whitespace'.
ewbankkit Jul 25, 2024
008a0f1
Fix golangci-lint 'unparam'.
ewbankkit Jul 25, 2024
0ab5629
Merge branch 'main' into HEAD
ewbankkit Jul 25, 2024
336d2c0
Fix terrafmt error.
ewbankkit Jul 25, 2024
446ff1b
rds: Skip 'no matching RDS Reserved Instance Offering found' acceptan…
ewbankkit Jul 25, 2024
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/31212.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/db_snapshot_copy: Add `shared_accounts` argument
```
58 changes: 57 additions & 1 deletion internal/service/rds/snapshot_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"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"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -103,6 +104,11 @@ func ResourceSnapshotCopy() *schema.Resource {
Optional: true,
ForceNew: true,
},
"shared_accounts": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"source_db_snapshot_identifier": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -188,6 +194,20 @@ func resourceSnapshotCopyCreate(ctx context.Context, d *schema.ResourceData, met
return sdkdiag.AppendErrorf(diags, "waiting for RDS DB Snapshot Copy (%s) create: %s", d.Id(), err)
}

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

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

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

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

Expand Down Expand Up @@ -226,11 +246,47 @@ func resourceSnapshotCopyRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("target_db_snapshot_identifier", snapshot.DBSnapshotIdentifier)
d.Set("vpc_id", snapshot.VpcId)

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

output, err := conn.DescribeDBSnapshotAttributesWithContext(ctx, input)

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

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

return diags
}

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

if d.HasChange("shared_accounts") {
o, n := d.GetChange("shared_accounts")
os := o.(*schema.Set)
ns := n.(*schema.Set)

additionList := ns.Difference(os)
removalList := os.Difference(ns)

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

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

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

return resourceSnapshotCopyRead(ctx, d, meta)
}

Expand Down
52 changes: 52 additions & 0 deletions internal/service/rds/snapshot_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func TestAccRDSSnapshotCopy_basic(t *testing.T) {
Config: testAccSnapshotCopyConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSnapshotCopyExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "shared_accounts.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand All @@ -49,6 +51,46 @@ func TestAccRDSSnapshotCopy_basic(t *testing.T) {
})
}

func TestAccRDSSnapshotCopy_share(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var v rds.DBSnapshot
resourceName := "aws_db_snapshot_copy.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, rds.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckSnapshotCopyDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccSnapshotCopyConfig_share(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSnapshotCopyExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "shared_accounts.#", "1"),
resource.TestCheckTypeSetElemAttr(resourceName, "shared_accounts.*", "all"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccSnapshotCopyConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSnapshotCopyExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "shared_accounts.#", "0"),
),
},
},
})
}

func TestAccRDSSnapshotCopy_tags(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
Expand Down Expand Up @@ -242,3 +284,13 @@ resource "aws_db_snapshot_copy" "test" {
}
}`, rName, tagKey1, tagValue1, tagKey2, tagValue2))
}

func testAccSnapshotCopyConfig_share(rName string) string {
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"
shared_accounts = ["all"]
}
`, rName))
}
1 change: 1 addition & 0 deletions website/docs/r/db_snapshot_copy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ This resource exports the following attributes in addition to the arguments abov
* `kms_key_id` - The ARN for the KMS encryption key.
* `license_model` - License model information for the restored DB instance.
* `option_group_name` - Provides the option group name for the DB snapshot.
* `shared_accounts` - (Optional) List of AWS Account ids to share snapshot with, use `all` to make snaphot public.
* `source_db_snapshot_identifier` - The DB snapshot Arn that the DB snapshot was copied from. It only has value in case of cross customer or cross region copy.
* `source_region` - The region that the DB snapshot was created in or copied from.
* `storage_type` - Specifies the storage type associated with DB snapshot.
Expand Down