From e9883e3cdf43e4bfe44384577a27a47bacbd45d3 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 29 Mar 2022 12:33:02 -0500 Subject: [PATCH 01/13] r/aws_dynamodb_contributor_insights: add waiters --- internal/service/dynamodb/find.go | 18 +++++++++++++++++- internal/service/dynamodb/status.go | 20 ++++++++++++++++++++ internal/service/dynamodb/wait.go | 27 +++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/internal/service/dynamodb/find.go b/internal/service/dynamodb/find.go index 20c2e09c8a88..aff1d7db7a49 100644 --- a/internal/service/dynamodb/find.go +++ b/internal/service/dynamodb/find.go @@ -2,7 +2,6 @@ package dynamodb import ( "context" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/dynamodb" ) @@ -119,3 +118,20 @@ func FindDynamoDBTTLRDescriptionByTableName(conn *dynamodb.DynamoDB, tableName s return output.TimeToLiveDescription, nil } + +func FindContributorInsights(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string) (*dynamodb.DescribeContributorInsightsOutput, error) { + input := &dynamodb.DescribeContributorInsightsInput{ + TableName: aws.String(tableName), + } + + if indexName != "" { + input.IndexName = aws.String(indexName) + } + + output, err := conn.DescribeContributorInsightsWithContext(ctx, input) + if err != nil { + return nil, err + } + + return output, nil +} diff --git a/internal/service/dynamodb/status.go b/internal/service/dynamodb/status.go index a8d6c5c60eb8..3c07ef57b100 100644 --- a/internal/service/dynamodb/status.go +++ b/internal/service/dynamodb/status.go @@ -184,3 +184,23 @@ func statusDynamoDBTableSES(conn *dynamodb.DynamoDB, tableName string) resource. return table, aws.StringValue(table.SSEDescription.Status), nil } } + +func statusContributorInsights(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + insight, err := FindContributorInsights(ctx, conn, tableName, indexName) + + if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + if insight == nil { + return nil, "", nil + } + + return insight, aws.StringValue(insight.ContributorInsightsStatus), nil + } +} diff --git a/internal/service/dynamodb/wait.go b/internal/service/dynamodb/wait.go index 0e39ada97dd8..43484baeab58 100644 --- a/internal/service/dynamodb/wait.go +++ b/internal/service/dynamodb/wait.go @@ -19,6 +19,7 @@ const ( deleteTableTimeout = 10 * time.Minute pitrUpdateTimeout = 30 * time.Second ttlUpdateTimeout = 30 * time.Second + updateContributorInsightsTimeout = 5 * time.Minute ) func waitDynamoDBKinesisStreamingDestinationActive(ctx context.Context, conn *dynamodb.DynamoDB, streamArn, tableName string) error { @@ -258,3 +259,29 @@ func waitDynamoDBSSEUpdated(conn *dynamodb.DynamoDB, tableName string) (*dynamod return nil, err } + +func waitContributorInsightsCreated(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string) error { + stateConf := &resource.StateChangeConf{ + Pending: []string{dynamodb.ContributorInsightsStatusEnabling}, + Target: []string{dynamodb.ContributorInsightsStatusEnabled}, + Timeout: updateContributorInsightsTimeout, + Refresh: statusContributorInsights(ctx, conn, tableName, indexName), + } + + _, err := stateConf.WaitForStateContext(ctx) + + return err +} + +func waitContributorInsightsDeleted(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string) error { + stateConf := &resource.StateChangeConf{ + Pending: []string{dynamodb.ContributorInsightsStatusDisabling}, + Target: []string{dynamodb.ContributorInsightsStatusDisabled}, + Timeout: updateContributorInsightsTimeout, + Refresh: statusContributorInsights(ctx, conn, tableName, indexName), + } + + _, err := stateConf.WaitForStateContext(ctx) + + return err +} From 74b8db62514a20a194e8045ae85f55e6e371030f Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 29 Mar 2022 14:49:15 -0500 Subject: [PATCH 02/13] r/aws_dynamodb_contributor_insights: add resource and CRUD operations --- internal/provider/provider.go | 1 + .../service/dynamodb/contributor_insights.go | 133 ++++++++++++++++++ internal/service/dynamodb/wait.go | 8 +- 3 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 internal/service/dynamodb/contributor_insights.go diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 2abe46f3859c..ade8ff126104 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1195,6 +1195,7 @@ func Provider() *schema.Provider { "aws_directory_service_directory": ds.ResourceDirectory(), "aws_directory_service_log_subscription": ds.ResourceLogSubscription(), + "aws_dynamodb_contributor_insights": dynamodb.ResourceContributorInsights(), "aws_dynamodb_global_table": dynamodb.ResourceGlobalTable(), "aws_dynamodb_kinesis_streaming_destination": dynamodb.ResourceKinesisStreamingDestination(), "aws_dynamodb_table": dynamodb.ResourceTable(), diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go new file mode 100644 index 000000000000..5ccf2cc84158 --- /dev/null +++ b/internal/service/dynamodb/contributor_insights.go @@ -0,0 +1,133 @@ +package dynamodb + +import ( + "context" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/dynamodb" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" +) + +func ResourceContributorInsights() *schema.Resource { + return &schema.Resource{ + CreateWithoutTimeout: resourceContributorInsightsCreate, + ReadWithoutTimeout: resourceContributorInsightsRead, + DeleteWithoutTimeout: resourceContributorInsightsDelete, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "index_name": { + Type: schema.TypeString, + Optional: true, + }, + "table_name": { + Type: schema.TypeString, + Required: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).DynamoDBConn + + in := &dynamodb.UpdateContributorInsightsInput{ + ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionEnable), + } + + if v, ok := d.GetOk("table_name"); ok { + in.TableName = aws.String(v.(string)) + } + + var indexName string + if v, ok := d.GetOk("index_name"); ok { + in.IndexName = aws.String(v.(string)) + indexName = v.(string) + } + + out, err := conn.UpdateContributorInsightsWithContext(ctx, in) + if err != nil { + return diag.Errorf("creating dynamodb ContributorInsights for table (%s): %s", d.Get("table_name").(string), err) + } + + d.SetId(aws.StringValue(out.TableName)) + + if err := waitContributorInsightsCreated(ctx, conn, d.Id(), indexName, d.Timeout(schema.TimeoutCreate)); err != nil { + return diag.Errorf("waiting for dynamodb ContributorInsights (%s) create: %s", d.Id(), err) + } + + return resourceContributorInsightsRead(ctx, d, meta) +} + +func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).DynamoDBConn + + var indexName string + if v, ok := d.GetOk("index_name"); ok { + indexName = v.(string) + } + + out, err := FindContributorInsights(ctx, conn, d.Id(), indexName) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] DynamoDB ContributorInsights (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + if err != nil { + return diag.Errorf("reading DynamoDB ContributorInsights (%s): %s", d.Id(), err) + } + + d.Set("index_name", out.IndexName) + d.Set("table_name", out.TableName) + d.Set("status", out.ContributorInsightsStatus) + + return nil +} + +func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).DynamoDBConn + + log.Printf("[INFO] Deleting DynamoDB ContributorInsights %s", d.Id()) + + var indexName string + if v, ok := d.GetOk("index_name"); ok { + indexName = v.(string) + } + + in := &dynamodb.UpdateContributorInsightsInput{ + ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionDisable), + TableName: aws.String(d.Id()), + IndexName: aws.String(indexName), + } + _, err := conn.UpdateContributorInsightsWithContext(ctx, in) + + if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { + return nil + } + + if err != nil { + return diag.Errorf("deleting DynamoDB ContributorInsights (%s): %s", d.Id(), err) + } + + if err := waitContributorInsightsDeleted(ctx, conn, d.Id(), indexName, d.Timeout(schema.TimeoutDelete)); err != nil { + return diag.Errorf("waiting for DynamoDB ContributorInsights (%s) to be deleted: %s", d.Id(), err) + } + + return nil +} diff --git a/internal/service/dynamodb/wait.go b/internal/service/dynamodb/wait.go index 43484baeab58..ac1b9af135e3 100644 --- a/internal/service/dynamodb/wait.go +++ b/internal/service/dynamodb/wait.go @@ -260,11 +260,11 @@ func waitDynamoDBSSEUpdated(conn *dynamodb.DynamoDB, tableName string) (*dynamod return nil, err } -func waitContributorInsightsCreated(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string) error { +func waitContributorInsightsCreated(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string, timeout time.Duration) error { stateConf := &resource.StateChangeConf{ Pending: []string{dynamodb.ContributorInsightsStatusEnabling}, Target: []string{dynamodb.ContributorInsightsStatusEnabled}, - Timeout: updateContributorInsightsTimeout, + Timeout: timeout, Refresh: statusContributorInsights(ctx, conn, tableName, indexName), } @@ -273,11 +273,11 @@ func waitContributorInsightsCreated(ctx context.Context, conn *dynamodb.DynamoDB return err } -func waitContributorInsightsDeleted(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string) error { +func waitContributorInsightsDeleted(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string, timeout time.Duration) error { stateConf := &resource.StateChangeConf{ Pending: []string{dynamodb.ContributorInsightsStatusDisabling}, Target: []string{dynamodb.ContributorInsightsStatusDisabled}, - Timeout: updateContributorInsightsTimeout, + Timeout: timeout, Refresh: statusContributorInsights(ctx, conn, tableName, indexName), } From ab2049c78f74d07e073ac771fa0a3c76db883b08 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 29 Mar 2022 15:42:59 -0500 Subject: [PATCH 03/13] r/aws_dynamo_contributor_insights: update schema --- internal/service/dynamodb/contributor_insights.go | 2 ++ internal/service/dynamodb/wait.go | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index 5ccf2cc84158..d99e1f34a342 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -28,10 +28,12 @@ func ResourceContributorInsights() *schema.Resource { Schema: map[string]*schema.Schema{ "index_name": { Type: schema.TypeString, + ForceNew: true, Optional: true, }, "table_name": { Type: schema.TypeString, + ForceNew: true, Required: true, }, "status": { diff --git a/internal/service/dynamodb/wait.go b/internal/service/dynamodb/wait.go index ac1b9af135e3..f48adfbfdd7c 100644 --- a/internal/service/dynamodb/wait.go +++ b/internal/service/dynamodb/wait.go @@ -19,7 +19,6 @@ const ( deleteTableTimeout = 10 * time.Minute pitrUpdateTimeout = 30 * time.Second ttlUpdateTimeout = 30 * time.Second - updateContributorInsightsTimeout = 5 * time.Minute ) func waitDynamoDBKinesisStreamingDestinationActive(ctx context.Context, conn *dynamodb.DynamoDB, streamArn, tableName string) error { From a880ab93193eb094847eb383794054ecf2884cf9 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 30 Mar 2022 12:22:29 -0500 Subject: [PATCH 04/13] r/aws_dynamo_contributor_insights: update delete order --- internal/service/dynamodb/contributor_insights.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index d99e1f34a342..587566f4cbe2 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -107,16 +107,17 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa log.Printf("[INFO] Deleting DynamoDB ContributorInsights %s", d.Id()) + in := &dynamodb.UpdateContributorInsightsInput{ + ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionDisable), + TableName: aws.String(d.Id()), + } + var indexName string if v, ok := d.GetOk("index_name"); ok { indexName = v.(string) + in.IndexName = aws.String(v.(string)) } - in := &dynamodb.UpdateContributorInsightsInput{ - ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionDisable), - TableName: aws.String(d.Id()), - IndexName: aws.String(indexName), - } _, err := conn.UpdateContributorInsightsWithContext(ctx, in) if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { From d80625c85c59e5c8e4f1f9fa5073694086c2c73b Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 30 Mar 2022 15:21:55 -0500 Subject: [PATCH 05/13] docs: add documentation --- .../service/dynamodb/contributor_insights.go | 62 ++++++++++++++++--- internal/service/dynamodb/find.go | 1 + ...ynamodb_contributor_insights.html.markdown | 40 ++++++++++++ 3 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 website/docs/r/dynamodb_contributor_insights.html.markdown diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index 587566f4cbe2..fa0fa929b3d4 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -2,7 +2,9 @@ package dynamodb import ( "context" + "fmt" "log" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -66,9 +68,10 @@ func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceDa return diag.Errorf("creating dynamodb ContributorInsights for table (%s): %s", d.Get("table_name").(string), err) } - d.SetId(aws.StringValue(out.TableName)) + id := encodeContributorInsightsID(aws.StringValue(out.TableName), indexName, meta.(*conns.AWSClient).AccountID) + d.SetId(id) - if err := waitContributorInsightsCreated(ctx, conn, d.Id(), indexName, d.Timeout(schema.TimeoutCreate)); err != nil { + if err := waitContributorInsightsCreated(ctx, conn, aws.StringValue(out.TableName), indexName, d.Timeout(schema.TimeoutCreate)); err != nil { return diag.Errorf("waiting for dynamodb ContributorInsights (%s) create: %s", d.Id(), err) } @@ -83,7 +86,12 @@ func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData indexName = v.(string) } - out, err := FindContributorInsights(ctx, conn, d.Id(), indexName) + tableName, indexName, _, err := decodeContributorInsightsID(d.Id()) + if err != nil { + return diag.Errorf("unable to decode ContributorInsights ID (%s): %s", d.Id(), err) + } + + out, err := FindContributorInsights(ctx, conn, tableName, indexName) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] DynamoDB ContributorInsights (%s) not found, removing from state", d.Id()) @@ -107,18 +115,21 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa log.Printf("[INFO] Deleting DynamoDB ContributorInsights %s", d.Id()) + tableName, indexName, _, err := decodeContributorInsightsID(d.Id()) + if err != nil { + return diag.Errorf("unable to decode DynamoDB ContributorInsights ID (%s): %s", d.Id(), err) + } + in := &dynamodb.UpdateContributorInsightsInput{ ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionDisable), - TableName: aws.String(d.Id()), + TableName: aws.String(tableName), } - var indexName string - if v, ok := d.GetOk("index_name"); ok { - indexName = v.(string) - in.IndexName = aws.String(v.(string)) + if indexName != "" { + in.IndexName = aws.String(indexName) } - _, err := conn.UpdateContributorInsightsWithContext(ctx, in) + _, err = conn.UpdateContributorInsightsWithContext(ctx, in) if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { return nil @@ -128,9 +139,40 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa return diag.Errorf("deleting DynamoDB ContributorInsights (%s): %s", d.Id(), err) } - if err := waitContributorInsightsDeleted(ctx, conn, d.Id(), indexName, d.Timeout(schema.TimeoutDelete)); err != nil { + if err := waitContributorInsightsDeleted(ctx, conn, tableName, indexName, d.Timeout(schema.TimeoutDelete)); err != nil { return diag.Errorf("waiting for DynamoDB ContributorInsights (%s) to be deleted: %s", d.Id(), err) } return nil } + +func encodeContributorInsightsID(tableName, indexName, accountID string) string { + if indexName != "" { + return fmt.Sprintf("%s-%s/%s", tableName, indexName, accountID) + } + + return fmt.Sprintf("%s/%s", tableName, accountID) +} + +func decodeContributorInsightsID(id string) (string, string, string, error) { + idParts := strings.Split(id, "/") + if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + return "", "", "", fmt.Errorf("expected ID in the form of table_name/account_id, given: %q", id) + } + + var tableName, indexName, accountID string + + tableName = idParts[0] + if strings.Contains(tableName, "-") { + parts := strings.Split(tableName, "-") + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return "", "", "", fmt.Errorf("expected ID in the form of table_name-index_name, given: %q", id) + } + tableName = parts[0] + indexName = parts[1] + } + + accountID = idParts[1] + + return tableName, indexName, accountID, nil +} diff --git a/internal/service/dynamodb/find.go b/internal/service/dynamodb/find.go index aff1d7db7a49..8c0f1a379625 100644 --- a/internal/service/dynamodb/find.go +++ b/internal/service/dynamodb/find.go @@ -2,6 +2,7 @@ package dynamodb import ( "context" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/dynamodb" ) diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown new file mode 100644 index 000000000000..7eb92884719f --- /dev/null +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -0,0 +1,40 @@ +--- +subcategory: "DynamoDB" +layout: "aws" +page_title: "AWS: aws_dynamodb_contributor_insights" +description: |- + Provides a DynamoDB contributor insights resource +--- + +# Resource: aws_dynamodb_contributor_insights + +Provides a DynamoDB contributor insights resource + +## Example Usage + +```terraform +resource "aws_dynamodb_contributor_insights" "test" { + table_name = "ExampleTableName" +} +``` + +## ArgumentReference + +The following arguments are supported: + +* `table_name` - (Required) The name of the table to attach contributor insight +* `index_name` - (Optional) The global secondary index name + +## Atrriibute Reference + +In addition to all arguments above, teh following attributes are exported: + +* `status` - The contributor insights status + +## Import + +`aws_dynamodb_contributor_insights` can be imported using the `table_name`, or `table_name-index_name`, followed by the account number, e.g., + +``` +$ terraform import aws_dynamodb_contributor_insights.test ExampleTableName/123456789012 +``` From 293fd8b5787753c7fcfde14d5197121777edef72 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 30 Mar 2022 15:24:16 -0500 Subject: [PATCH 06/13] docs: add documentation --- website/docs/r/dynamodb_contributor_insights.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index 7eb92884719f..9c9beb25681c 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -25,9 +25,9 @@ The following arguments are supported: * `table_name` - (Required) The name of the table to attach contributor insight * `index_name` - (Optional) The global secondary index name -## Atrriibute Reference +## Attribute Reference -In addition to all arguments above, teh following attributes are exported: +In addition to all arguments above, the following attributes are exported: * `status` - The contributor insights status From b01c898266c92ed5ed2350eeecb7e7cf88119623 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 30 Mar 2022 15:43:25 -0500 Subject: [PATCH 07/13] docs: typo --- website/docs/r/dynamodb_contributor_insights.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index 9c9beb25681c..f9d46ef684f2 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -18,14 +18,14 @@ resource "aws_dynamodb_contributor_insights" "test" { } ``` -## ArgumentReference +## Argument Reference The following arguments are supported: * `table_name` - (Required) The name of the table to attach contributor insight * `index_name` - (Optional) The global secondary index name -## Attribute Reference +## Attributes Reference In addition to all arguments above, the following attributes are exported: From b1bf30d025a3bded782f02ee6baa3c302fbc7f11 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 30 Mar 2022 16:04:01 -0500 Subject: [PATCH 08/13] r/aws_dynamo_contributor_insights: remove unused variable --- .../service/dynamodb/contributor_insights.go | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index fa0fa929b3d4..66b0787eed5b 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -81,12 +81,7 @@ func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceDa func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).DynamoDBConn - var indexName string - if v, ok := d.GetOk("index_name"); ok { - indexName = v.(string) - } - - tableName, indexName, _, err := decodeContributorInsightsID(d.Id()) + tableName, indexName, err := decodeContributorInsightsID(d.Id()) if err != nil { return diag.Errorf("unable to decode ContributorInsights ID (%s): %s", d.Id(), err) } @@ -115,7 +110,7 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa log.Printf("[INFO] Deleting DynamoDB ContributorInsights %s", d.Id()) - tableName, indexName, _, err := decodeContributorInsightsID(d.Id()) + tableName, indexName, err := decodeContributorInsightsID(d.Id()) if err != nil { return diag.Errorf("unable to decode DynamoDB ContributorInsights ID (%s): %s", d.Id(), err) } @@ -154,25 +149,23 @@ func encodeContributorInsightsID(tableName, indexName, accountID string) string return fmt.Sprintf("%s/%s", tableName, accountID) } -func decodeContributorInsightsID(id string) (string, string, string, error) { +func decodeContributorInsightsID(id string) (string, string, error) { idParts := strings.Split(id, "/") if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { - return "", "", "", fmt.Errorf("expected ID in the form of table_name/account_id, given: %q", id) + return "", "", fmt.Errorf("expected ID in the form of table_name/account_id, given: %q", id) } - var tableName, indexName, accountID string + var tableName, indexName string tableName = idParts[0] if strings.Contains(tableName, "-") { parts := strings.Split(tableName, "-") if len(parts) != 2 || parts[0] == "" || parts[1] == "" { - return "", "", "", fmt.Errorf("expected ID in the form of table_name-index_name, given: %q", id) + return "", "", fmt.Errorf("expected ID in the form of table_name-index_name, given: %q", id) } tableName = parts[0] indexName = parts[1] } - accountID = idParts[1] - - return tableName, indexName, accountID, nil + return tableName, indexName, nil } From 47e9642f69e145f4d81802a9b5baf4fe523c44b1 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 31 Mar 2022 13:27:10 -0500 Subject: [PATCH 09/13] r/aws_dynamo_contributor_insights: add acctest --- .../service/dynamodb/contributor_insights.go | 34 ++-- .../dynamodb/contributor_insights_test.go | 154 ++++++++++++++++++ ...ynamodb_contributor_insights.html.markdown | 2 +- 3 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 internal/service/dynamodb/contributor_insights_test.go diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index 66b0787eed5b..01d04b19618c 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -21,6 +21,9 @@ func ResourceContributorInsights() *schema.Resource { CreateWithoutTimeout: resourceContributorInsightsCreate, ReadWithoutTimeout: resourceContributorInsightsRead, DeleteWithoutTimeout: resourceContributorInsightsDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(5 * time.Minute), @@ -68,7 +71,7 @@ func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceDa return diag.Errorf("creating dynamodb ContributorInsights for table (%s): %s", d.Get("table_name").(string), err) } - id := encodeContributorInsightsID(aws.StringValue(out.TableName), indexName, meta.(*conns.AWSClient).AccountID) + id := EncodeContributorInsightsID(aws.StringValue(out.TableName), indexName, meta.(*conns.AWSClient).AccountID) d.SetId(id) if err := waitContributorInsightsCreated(ctx, conn, aws.StringValue(out.TableName), indexName, d.Timeout(schema.TimeoutCreate)); err != nil { @@ -81,7 +84,7 @@ func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceDa func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).DynamoDBConn - tableName, indexName, err := decodeContributorInsightsID(d.Id()) + tableName, indexName, err := DecodeContributorInsightsID(d.Id()) if err != nil { return diag.Errorf("unable to decode ContributorInsights ID (%s): %s", d.Id(), err) } @@ -110,7 +113,7 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa log.Printf("[INFO] Deleting DynamoDB ContributorInsights %s", d.Id()) - tableName, indexName, err := decodeContributorInsightsID(d.Id()) + tableName, indexName, err := DecodeContributorInsightsID(d.Id()) if err != nil { return diag.Errorf("unable to decode DynamoDB ContributorInsights ID (%s): %s", d.Id(), err) } @@ -141,31 +144,18 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa return nil } -func encodeContributorInsightsID(tableName, indexName, accountID string) string { - if indexName != "" { - return fmt.Sprintf("%s-%s/%s", tableName, indexName, accountID) - } - - return fmt.Sprintf("%s/%s", tableName, accountID) +func EncodeContributorInsightsID(tableName, indexName, accountID string) string { + return fmt.Sprintf("name:%s/index:%s/%s", tableName, indexName, accountID) } -func decodeContributorInsightsID(id string) (string, string, error) { +func DecodeContributorInsightsID(id string) (string, string, error) { idParts := strings.Split(id, "/") - if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + if len(idParts) != 3 || idParts[0] == "" || idParts[2] == "" { return "", "", fmt.Errorf("expected ID in the form of table_name/account_id, given: %q", id) } - var tableName, indexName string - - tableName = idParts[0] - if strings.Contains(tableName, "-") { - parts := strings.Split(tableName, "-") - if len(parts) != 2 || parts[0] == "" || parts[1] == "" { - return "", "", fmt.Errorf("expected ID in the form of table_name-index_name, given: %q", id) - } - tableName = parts[0] - indexName = parts[1] - } + tableName := strings.TrimPrefix(idParts[0], "name:") + indexName := strings.TrimPrefix(idParts[1], "index:") return tableName, indexName, nil } diff --git a/internal/service/dynamodb/contributor_insights_test.go b/internal/service/dynamodb/contributor_insights_test.go new file mode 100644 index 000000000000..9079b2e2edbd --- /dev/null +++ b/internal/service/dynamodb/contributor_insights_test.go @@ -0,0 +1,154 @@ +package dynamodb_test + +import ( + "context" + "fmt" + "log" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/dynamodb" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tfdynamodb "github.com/hashicorp/terraform-provider-aws/internal/service/dynamodb" +) + +func TestAccContributorInsights_basic(t *testing.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(t) }, + ErrorCheck: acctest.ErrorCheck(t, dynamodb.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckContributorInsightsDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContributorInsightsBasicConfig(rName, ""), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "table_name", rName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccContributorInsightsBasicConfig(rName, indexName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "index_name", indexName), + ), + }, + }, + }) +} + +func testAccContributorInsightsBaseConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_dynamodb_table" "test" { + name = %[1]q + read_capacity = 2 + write_capacity = 2 + hash_key = %[1]q + + attribute { + name = %[1]q + type = "S" + } + + global_secondary_index { + name = "%[1]s-index" + hash_key = %[1]q + projection_type = "ALL" + read_capacity = 1 + write_capacity = 1 + } +} +`, rName) +} + +func testAccContributorInsightsBasicConfig(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 +} +`, rName, indexName)) +} + +func testAccCheckContributorInsightsExists(n string, ci *dynamodb.DescribeContributorInsightsOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("no DynamodDB Contributor Insights ID is set") + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).DynamoDBConn + + tableName, indexName, err := tfdynamodb.DecodeContributorInsightsID(rs.Primary.ID) + if err != nil { + return err + } + + output, err := tfdynamodb.FindContributorInsights(context.Background(), conn, tableName, indexName) + if err != nil { + return err + } + + ci = output + + return nil + } +} + +func testAccCheckContributorInsightsDestroy(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).DynamoDBConn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_dynamodb_contributor_insights" { + continue + } + + log.Printf("[DEBUG] Checking if DynamoDB Contributor Insights %s exists", rs.Primary.ID) + + tableName, indexName, err := tfdynamodb.DecodeContributorInsightsID(rs.Primary.ID) + if err != nil { + return err + } + + in := &dynamodb.DescribeContributorInsightsInput{ + TableName: aws.String(tableName), + } + + if indexName != "" { + in.IndexName = aws.String(indexName) + } + + _, err = conn.DescribeContributorInsightsWithContext(context.Background(), in) + if err == nil { + return fmt.Errorf("the DynamoDB Contributor Insights %s still exists. Failing", rs.Primary.ID) + } + + // Verify the error is what we want + if dbErr, ok := err.(awserr.Error); ok && dbErr.Code() == "ResourceNotFoundException" { + return nil + } + + return err + } + + return nil +} diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index f9d46ef684f2..187927ab2baf 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -22,7 +22,7 @@ resource "aws_dynamodb_contributor_insights" "test" { The following arguments are supported: -* `table_name` - (Required) The name of the table to attach contributor insight +* `table_name` - (Required) The name of the table to enable contributor insights * `index_name` - (Optional) The global secondary index name ## Attributes Reference From d23609ee6fca45321561d2a8b7abd91c9e0611fa Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 31 Mar 2022 13:47:35 -0500 Subject: [PATCH 10/13] add CHANGELOG for #23947 --- .changelog/23947.txt | 3 +++ website/docs/r/dynamodb_contributor_insights.html.markdown | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 .changelog/23947.txt diff --git a/.changelog/23947.txt b/.changelog/23947.txt new file mode 100644 index 000000000000..2dc5c08dba1e --- /dev/null +++ b/.changelog/23947.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_dynamodb_contributor_insights +``` \ No newline at end of file diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index 187927ab2baf..9672a23d73a8 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -33,8 +33,8 @@ In addition to all arguments above, the following attributes are exported: ## Import -`aws_dynamodb_contributor_insights` can be imported using the `table_name`, or `table_name-index_name`, followed by the account number, e.g., +`aws_dynamodb_contributor_insights` can be imported using the format `name:table_name/index:index_name`, followed by the account number, e.g., ``` -$ terraform import aws_dynamodb_contributor_insights.test ExampleTableName/123456789012 +$ terraform import aws_dynamodb_contributor_insights.test name:ExampleTableName/index:ExampleIndexName/123456789012 ``` From 781bc6be8047176cf2b5d0f1db7b0a6148496420 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Mon, 4 Apr 2022 11:55:48 -0500 Subject: [PATCH 11/13] r/aws_dynamo_contributor_insights: add test --- .../service/dynamodb/contributor_insights.go | 29 ++++++++----------- .../dynamodb/contributor_insights_test.go | 22 ++++++++++++++ ...ynamodb_contributor_insights.html.markdown | 6 ---- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/internal/service/dynamodb/contributor_insights.go b/internal/service/dynamodb/contributor_insights.go index 01d04b19618c..ffc6a68d8eb3 100644 --- a/internal/service/dynamodb/contributor_insights.go +++ b/internal/service/dynamodb/contributor_insights.go @@ -41,10 +41,6 @@ func ResourceContributorInsights() *schema.Resource { ForceNew: true, Required: true, }, - "status": { - Type: schema.TypeString, - Computed: true, - }, }, } } @@ -52,30 +48,30 @@ func ResourceContributorInsights() *schema.Resource { func resourceContributorInsightsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).DynamoDBConn - in := &dynamodb.UpdateContributorInsightsInput{ + input := &dynamodb.UpdateContributorInsightsInput{ ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionEnable), } if v, ok := d.GetOk("table_name"); ok { - in.TableName = aws.String(v.(string)) + input.TableName = aws.String(v.(string)) } var indexName string if v, ok := d.GetOk("index_name"); ok { - in.IndexName = aws.String(v.(string)) + input.IndexName = aws.String(v.(string)) indexName = v.(string) } - out, err := conn.UpdateContributorInsightsWithContext(ctx, in) + output, err := conn.UpdateContributorInsightsWithContext(ctx, input) if err != nil { - return diag.Errorf("creating dynamodb ContributorInsights for table (%s): %s", d.Get("table_name").(string), err) + return diag.Errorf("creating DynamoDB ContributorInsights for table (%s): %s", d.Get("table_name").(string), err) } - id := EncodeContributorInsightsID(aws.StringValue(out.TableName), indexName, meta.(*conns.AWSClient).AccountID) + id := EncodeContributorInsightsID(aws.StringValue(output.TableName), indexName, meta.(*conns.AWSClient).AccountID) d.SetId(id) - if err := waitContributorInsightsCreated(ctx, conn, aws.StringValue(out.TableName), indexName, d.Timeout(schema.TimeoutCreate)); err != nil { - return diag.Errorf("waiting for dynamodb ContributorInsights (%s) create: %s", d.Id(), err) + if err := waitContributorInsightsCreated(ctx, conn, aws.StringValue(output.TableName), indexName, d.Timeout(schema.TimeoutCreate)); err != nil { + return diag.Errorf("waiting for DynamoDB ContributorInsights (%s) create: %s", d.Id(), err) } return resourceContributorInsightsRead(ctx, d, meta) @@ -86,7 +82,7 @@ func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData tableName, indexName, err := DecodeContributorInsightsID(d.Id()) if err != nil { - return diag.Errorf("unable to decode ContributorInsights ID (%s): %s", d.Id(), err) + return diag.Errorf("unable to decode DynamoDB ContributorInsights ID (%s): %s", d.Id(), err) } out, err := FindContributorInsights(ctx, conn, tableName, indexName) @@ -103,7 +99,6 @@ func resourceContributorInsightsRead(ctx context.Context, d *schema.ResourceData d.Set("index_name", out.IndexName) d.Set("table_name", out.TableName) - d.Set("status", out.ContributorInsightsStatus) return nil } @@ -118,16 +113,16 @@ func resourceContributorInsightsDelete(ctx context.Context, d *schema.ResourceDa return diag.Errorf("unable to decode DynamoDB ContributorInsights ID (%s): %s", d.Id(), err) } - in := &dynamodb.UpdateContributorInsightsInput{ + input := &dynamodb.UpdateContributorInsightsInput{ ContributorInsightsAction: aws.String(dynamodb.ContributorInsightsActionDisable), TableName: aws.String(tableName), } if indexName != "" { - in.IndexName = aws.String(indexName) + input.IndexName = aws.String(indexName) } - _, err = conn.UpdateContributorInsightsWithContext(ctx, in) + _, err = conn.UpdateContributorInsightsWithContext(ctx, input) if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { return nil diff --git a/internal/service/dynamodb/contributor_insights_test.go b/internal/service/dynamodb/contributor_insights_test.go index 9079b2e2edbd..e1a6a7a9d240 100644 --- a/internal/service/dynamodb/contributor_insights_test.go +++ b/internal/service/dynamodb/contributor_insights_test.go @@ -52,6 +52,28 @@ func TestAccContributorInsights_basic(t *testing.T) { }) } +func TestAccContributorInsights_disappears(t *testing.T) { + var conf dynamodb.DescribeContributorInsightsOutput + rName := fmt.Sprintf("tf-acc-test-%s", sdkacctest.RandString(8)) + resourceName := "aws_dynamodb_contributor_insights.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, dynamodb.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckContributorInsightsDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContributorInsightsBasicConfig(rName, ""), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckContributorInsightsExists(resourceName, &conf), + acctest.CheckResourceDisappears(acctest.Provider, tfdynamodb.ResourceContributorInsights(), resourceName), + ), + }, + }, + }) +} + func testAccContributorInsightsBaseConfig(rName string) string { return fmt.Sprintf(` resource "aws_dynamodb_table" "test" { diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index 9672a23d73a8..0bdc4bc63964 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -25,12 +25,6 @@ The following arguments are supported: * `table_name` - (Required) The name of the table to enable contributor insights * `index_name` - (Optional) The global secondary index name -## Attributes Reference - -In addition to all arguments above, the following attributes are exported: - -* `status` - The contributor insights status - ## Import `aws_dynamodb_contributor_insights` can be imported using the format `name:table_name/index:index_name`, followed by the account number, e.g., From ab46da7516d07c97dc47e85bcdcf0b1351c322e9 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Mon, 4 Apr 2022 13:29:09 -0500 Subject: [PATCH 12/13] r/aws_dynamo_contributor_insights: better error handling --- .../dynamodb/contributor_insights_test.go | 7 +++--- internal/service/dynamodb/find.go | 22 +++++++++++++++++++ internal/service/dynamodb/status.go | 3 ++- internal/service/dynamodb/wait.go | 2 +- ...ynamodb_contributor_insights.html.markdown | 4 ++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/internal/service/dynamodb/contributor_insights_test.go b/internal/service/dynamodb/contributor_insights_test.go index e1a6a7a9d240..a9decd56ae46 100644 --- a/internal/service/dynamodb/contributor_insights_test.go +++ b/internal/service/dynamodb/contributor_insights_test.go @@ -3,11 +3,11 @@ package dynamodb_test import ( "context" "fmt" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "log" "testing" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/dynamodb" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -69,6 +69,7 @@ func TestAccContributorInsights_disappears(t *testing.T) { testAccCheckContributorInsightsExists(resourceName, &conf), acctest.CheckResourceDisappears(acctest.Provider, tfdynamodb.ResourceContributorInsights(), resourceName), ), + ExpectNonEmptyPlan: true, }, }, }) @@ -159,13 +160,13 @@ func testAccCheckContributorInsightsDestroy(s *terraform.State) error { in.IndexName = aws.String(indexName) } - _, err = conn.DescribeContributorInsightsWithContext(context.Background(), in) + _, err = tfdynamodb.FindContributorInsights(context.Background(), conn, tableName, indexName) if err == nil { return fmt.Errorf("the DynamoDB Contributor Insights %s still exists. Failing", rs.Primary.ID) } // Verify the error is what we want - if dbErr, ok := err.(awserr.Error); ok && dbErr.Code() == "ResourceNotFoundException" { + if tfresource.NotFound(err) { return nil } diff --git a/internal/service/dynamodb/find.go b/internal/service/dynamodb/find.go index 8c0f1a379625..a03c88169700 100644 --- a/internal/service/dynamodb/find.go +++ b/internal/service/dynamodb/find.go @@ -5,6 +5,9 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/dynamodb" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) func FindDynamoDBKinesisDataStreamDestination(ctx context.Context, conn *dynamodb.DynamoDB, streamArn, tableName string) (*dynamodb.KinesisDataStreamDestination, error) { @@ -130,9 +133,28 @@ func FindContributorInsights(ctx context.Context, conn *dynamodb.DynamoDB, table } output, err := conn.DescribeContributorInsightsWithContext(ctx, input) + + if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { + return nil, &resource.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + if err != nil { return nil, err } + if status := aws.StringValue(output.ContributorInsightsStatus); status == dynamodb.ContributorInsightsStatusDisabled { + return nil, &resource.NotFoundError{ + Message: status, + LastRequest: input, + } + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + return output, nil } diff --git a/internal/service/dynamodb/status.go b/internal/service/dynamodb/status.go index 3c07ef57b100..d68e4900e541 100644 --- a/internal/service/dynamodb/status.go +++ b/internal/service/dynamodb/status.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) func statusDynamoDBKinesisStreamingDestination(ctx context.Context, conn *dynamodb.DynamoDB, streamArn, tableName string) resource.StateRefreshFunc { @@ -189,7 +190,7 @@ func statusContributorInsights(ctx context.Context, conn *dynamodb.DynamoDB, tab return func() (interface{}, string, error) { insight, err := FindContributorInsights(ctx, conn, tableName, indexName) - if tfawserr.ErrCodeEquals(err, dynamodb.ErrCodeResourceNotFoundException) { + if tfresource.NotFound(err) { return nil, "", nil } diff --git a/internal/service/dynamodb/wait.go b/internal/service/dynamodb/wait.go index f48adfbfdd7c..63bec4592dba 100644 --- a/internal/service/dynamodb/wait.go +++ b/internal/service/dynamodb/wait.go @@ -275,7 +275,7 @@ func waitContributorInsightsCreated(ctx context.Context, conn *dynamodb.DynamoDB func waitContributorInsightsDeleted(ctx context.Context, conn *dynamodb.DynamoDB, tableName, indexName string, timeout time.Duration) error { stateConf := &resource.StateChangeConf{ Pending: []string{dynamodb.ContributorInsightsStatusDisabling}, - Target: []string{dynamodb.ContributorInsightsStatusDisabled}, + Target: []string{}, Timeout: timeout, Refresh: statusContributorInsights(ctx, conn, tableName, indexName), } diff --git a/website/docs/r/dynamodb_contributor_insights.html.markdown b/website/docs/r/dynamodb_contributor_insights.html.markdown index 0bdc4bc63964..5a07ee1a666e 100644 --- a/website/docs/r/dynamodb_contributor_insights.html.markdown +++ b/website/docs/r/dynamodb_contributor_insights.html.markdown @@ -25,6 +25,10 @@ The following arguments are supported: * `table_name` - (Required) The name of the table to enable contributor insights * `index_name` - (Optional) The global secondary index name +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + ## Import `aws_dynamodb_contributor_insights` can be imported using the format `name:table_name/index:index_name`, followed by the account number, e.g., From e800d516bab109ec62061fb6f75ef1712f645228 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Mon, 4 Apr 2022 13:47:37 -0500 Subject: [PATCH 13/13] r/aws_dynamo_contributor_insights: fix imports --- internal/service/dynamodb/contributor_insights_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/dynamodb/contributor_insights_test.go b/internal/service/dynamodb/contributor_insights_test.go index a9decd56ae46..a91636f38cca 100644 --- a/internal/service/dynamodb/contributor_insights_test.go +++ b/internal/service/dynamodb/contributor_insights_test.go @@ -3,7 +3,6 @@ package dynamodb_test import ( "context" "fmt" - "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "log" "testing" @@ -15,6 +14,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/acctest" "github.com/hashicorp/terraform-provider-aws/internal/conns" tfdynamodb "github.com/hashicorp/terraform-provider-aws/internal/service/dynamodb" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) func TestAccContributorInsights_basic(t *testing.T) {