diff --git a/.changelog/43914.txt b/.changelog/43914.txt new file mode 100644 index 000000000000..8038c6bcdf75 --- /dev/null +++ b/.changelog/43914.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_dynamodb_contributor_insights: Add `mode` argument in support of [CloudWatch contributor insights modes](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/contributorinsights_HowItWorks.html#contributorinsights_HowItWorks.Modes) +``` diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index 4bf07705ce19..44a7c0f916c8 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -46,6 +46,12 @@ func resourceContributorInsights() *schema.Resource { Optional: true, ForceNew: true, }, + names.AttrMode: { + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateDiagFunc: enum.Validate[awstypes.ContributorInsightsMode](), + }, names.AttrTableName: { Type: schema.TypeString, Required: true, @@ -60,7 +66,7 @@ func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceDa conn := meta.(*conns.AWSClient).DynamoDBClient(ctx) tableName := d.Get(names.AttrTableName).(string) - input := &dynamodb.UpdateContributorInsightsInput{ + input := dynamodb.UpdateContributorInsightsInput{ ContributorInsightsAction: awstypes.ContributorInsightsActionEnable, TableName: aws.String(tableName), } @@ -71,7 +77,11 @@ func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceDa input.IndexName = aws.String(indexName) } - _, err := conn.UpdateContributorInsights(ctx, input) + if v, ok := d.GetOk(names.AttrMode); ok { + input.ContributorInsightsMode = awstypes.ContributorInsightsMode(v.(string)) + } + + _, err := conn.UpdateContributorInsights(ctx, &input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating DynamoDB Contributor Insights for table (%s): %s", tableName, err) @@ -108,6 +118,7 @@ func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData } d.Set("index_name", output.IndexName) + d.Set(names.AttrMode, output.ContributorInsightsMode) d.Set(names.AttrTableName, output.TableName) return diags @@ -122,17 +133,16 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa return sdkdiag.AppendFromErr(diags, err) } - input := &dynamodb.UpdateContributorInsightsInput{ + input := dynamodb.UpdateContributorInsightsInput{ ContributorInsightsAction: awstypes.ContributorInsightsActionDisable, TableName: aws.String(tableName), } - if indexName != "" { input.IndexName = aws.String(indexName) } log.Printf("[INFO] Deleting DynamoDB Contributor Insights: %s", d.Id()) - _, err = conn.UpdateContributorInsights(ctx, input) + _, err = conn.UpdateContributorInsights(ctx, &input) if errs.IsA[*awstypes.ResourceNotFoundException](err) { return diags @@ -168,14 +178,14 @@ func contributorInsightsParseResourceID(id string) (string, string, error) { } func findContributorInsightsByTwoPartKey(ctx context.Context, conn *dynamodb.Client, tableName, indexName string) (*dynamodb.DescribeContributorInsightsOutput, error) { - input := &dynamodb.DescribeContributorInsightsInput{ + input := dynamodb.DescribeContributorInsightsInput{ TableName: aws.String(tableName), } if indexName != "" { input.IndexName = aws.String(indexName) } - output, err := findContributorInsights(ctx, conn, input) + output, err := findContributorInsights(ctx, conn, &input) if err != nil { return nil, err diff --git a/internal/service/dynamodb/contributor_insights_test.go b/internal/service/dynamodb/contributor_insights_test.go index d586fd022cd0..831a28d1d007 100644 --- a/internal/service/dynamodb/contributor_insights_test.go +++ b/internal/service/dynamodb/contributor_insights_test.go @@ -55,6 +55,78 @@ func TestAccDynamoDBContributorInsights_basic(t *testing.T) { }) } +func TestAccDynamoDBContributorInsights_ModeAccessedAndThrottled(t *testing.T) { + ctx := acctest.Context(t) + var conf dynamodb.DescribeContributorInsightsOutput + rName := fmt.Sprintf("tf-acc-test-%s", sdkacctest.RandString(8)) + indexName := fmt.Sprintf("%s-index", rName) + resourceName := "aws_dynamodb_contributor_insights.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.DynamoDBServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckContributorInsightsDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccContributorInsightsConfig_AccessedAndThrottledKeys(rName, ""), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, names.AttrTableName, rName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccContributorInsightsConfig_AccessedAndThrottledKeys(rName, indexName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "index_name", indexName), + ), + }, + }, + }) +} + +func TestAccDynamoDBContributorInsights_ModeThrottledOnly(t *testing.T) { + ctx := acctest.Context(t) + var conf dynamodb.DescribeContributorInsightsOutput + rName := fmt.Sprintf("tf-acc-test-%s", sdkacctest.RandString(8)) + indexName := fmt.Sprintf("%s-index", rName) + resourceName := "aws_dynamodb_contributor_insights.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.DynamoDBServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckContributorInsightsDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccContributorInsightsConfig_ThrottledKeys(rName, ""), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, names.AttrTableName, rName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccContributorInsightsConfig_ThrottledKeys(rName, indexName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "index_name", indexName), + ), + }, + }, + }) +} + func TestAccDynamoDBContributorInsights_disappears(t *testing.T) { ctx := acctest.Context(t) var conf dynamodb.DescribeContributorInsightsOutput @@ -100,7 +172,7 @@ resource "aws_dynamodb_table" "test" { write_capacity = 1 } } -`, rName) + `, rName) } func testAccContributorInsightsConfig_basic(rName, indexName string) string { @@ -112,6 +184,28 @@ resource "aws_dynamodb_contributor_insights" "test" { `, rName, indexName)) } +func testAccContributorInsightsConfig_AccessedAndThrottledKeys(rName, indexName string) string { + return acctest.ConfigCompose(testAccContributorInsightsBaseConfig(rName), fmt.Sprintf(` +resource "aws_dynamodb_contributor_insights" "test" { + table_name = aws_dynamodb_table.test.name + index_name = %[2]q + + mode = "ACCESSED_AND_THROTTLED_KEYS" +} +`, rName, indexName)) +} + +func testAccContributorInsightsConfig_ThrottledKeys(rName, indexName string) string { + return acctest.ConfigCompose(testAccContributorInsightsBaseConfig(rName), fmt.Sprintf(` +resource "aws_dynamodb_contributor_insights" "test" { + table_name = aws_dynamodb_table.test.name + index_name = %[2]q + + mode = "THROTTLED_KEYS" +} +`, rName, indexName)) +} + func testAccCheckContributorInsightsExists(ctx context.Context, n string, v *dynamodb.DescribeContributorInsightsOutput) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index 2055410ff2d2..8439028df109 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -25,6 +25,7 @@ This resource supports the following arguments: * `region` - (Optional) Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the [provider configuration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#aws-configuration-reference). * `table_name` - (Required) The name of the table to enable contributor insights * `index_name` - (Optional) The global secondary index name +* `mode` - (Optional) argument to specify the [CloudWatch contributor insights mode](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/contributorinsights_HowItWorks.html#contributorinsights_HowItWorks.Modes) ## Attribute Reference