Skip to content

Commit

Permalink
Merge pull request #18844 from hashicorp/td-read-method-of-d-data-sou…
Browse files Browse the repository at this point in the history
…rces

tech-debt: refactor dynamodb data and resources to use different Read methods
  • Loading branch information
anGie44 authored Apr 14, 2021
2 parents 02e5827 + 550504c commit f3d8aad
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 112 deletions.
95 changes: 80 additions & 15 deletions aws/data_source_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
Expand Down Expand Up @@ -216,48 +217,112 @@ func dataSourceAwsDynamoDbTableRead(d *schema.ResourceData, meta interface{}) er
conn := meta.(*AWSClient).dynamodbconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

name := d.Get("name").(string)

result, err := conn.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(d.Get("name").(string)),
TableName: aws.String(name),
})

if err != nil {
return fmt.Errorf("Error retrieving DynamoDB table: %w", err)
return fmt.Errorf("error reading Dynamodb Table (%s): %w", name, err)
}

d.SetId(aws.StringValue(result.Table.TableName))
if result == nil || result.Table == nil {
return fmt.Errorf("error reading Dynamodb Table (%s): not found", name)
}

err = flattenDynamoDbTableResource(d, result.Table)
if err != nil {
return err
table := result.Table

d.SetId(aws.StringValue(table.TableName))

d.Set("arn", table.TableArn)
d.Set("name", table.TableName)

if table.BillingModeSummary != nil {
d.Set("billing_mode", table.BillingModeSummary.BillingMode)
} else {
d.Set("billing_mode", dynamodb.BillingModeProvisioned)
}

if table.ProvisionedThroughput != nil {
d.Set("write_capacity", table.ProvisionedThroughput.WriteCapacityUnits)
d.Set("read_capacity", table.ProvisionedThroughput.ReadCapacityUnits)
}

if err := d.Set("attribute", flattenDynamoDbTableAttributeDefinitions(table.AttributeDefinitions)); err != nil {
return fmt.Errorf("error setting attribute: %w", err)
}

for _, attribute := range table.KeySchema {
if aws.StringValue(attribute.KeyType) == dynamodb.KeyTypeHash {
d.Set("hash_key", attribute.AttributeName)
}

if aws.StringValue(attribute.KeyType) == dynamodb.KeyTypeRange {
d.Set("range_key", attribute.AttributeName)
}
}

if err := d.Set("local_secondary_index", flattenDynamoDbTableLocalSecondaryIndex(table.LocalSecondaryIndexes)); err != nil {
return fmt.Errorf("error setting local_secondary_index: %w", err)
}

if err := d.Set("global_secondary_index", flattenDynamoDbTableGlobalSecondaryIndex(table.GlobalSecondaryIndexes)); err != nil {
return fmt.Errorf("error setting global_secondary_index: %w", err)
}

if table.StreamSpecification != nil {
d.Set("stream_view_type", table.StreamSpecification.StreamViewType)
d.Set("stream_enabled", table.StreamSpecification.StreamEnabled)
} else {
d.Set("stream_view_type", "")
d.Set("stream_enabled", false)
}

d.Set("stream_arn", table.LatestStreamArn)
d.Set("stream_label", table.LatestStreamLabel)

if err := d.Set("server_side_encryption", flattenDynamodDbTableServerSideEncryption(table.SSEDescription)); err != nil {
return fmt.Errorf("error setting server_side_encryption: %w", err)
}

if err := d.Set("replica", flattenDynamoDbReplicaDescriptions(table.Replicas)); err != nil {
return fmt.Errorf("error setting replica: %w", err)
}

pitrOut, err := conn.DescribeContinuousBackups(&dynamodb.DescribeContinuousBackupsInput{
TableName: aws.String(d.Id()),
})

if err != nil && !tfawserr.ErrCodeEquals(err, "UnknownOperationException") {
return fmt.Errorf("error describing DynamoDB Table (%s) Continuous Backups: %w", d.Id(), err)
}

if err := d.Set("point_in_time_recovery", flattenDynamoDbPitr(pitrOut)); err != nil {
return fmt.Errorf("error setting point_in_time_recovery: %w", err)
}

ttlOut, err := conn.DescribeTimeToLive(&dynamodb.DescribeTimeToLiveInput{
TableName: aws.String(d.Id()),
})

if err != nil {
return fmt.Errorf("error describing DynamoDB Table (%s) Time to Live: %w", d.Id(), err)
}

if err := d.Set("ttl", flattenDynamoDbTtl(ttlOut)); err != nil {
return fmt.Errorf("error setting ttl: %w", err)
}

tags, err := keyvaluetags.DynamodbListTags(conn, d.Get("arn").(string))

if err != nil && !isAWSErr(err, "UnknownOperationException", "Tagging is not currently supported in DynamoDB Local.") {
if err != nil && !tfawserr.ErrMessageContains(err, "UnknownOperationException", "Tagging is not currently supported in DynamoDB Local.") {
return fmt.Errorf("error listing tags for DynamoDB Table (%s): %w", d.Get("arn").(string), err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

pitrOut, err := conn.DescribeContinuousBackups(&dynamodb.DescribeContinuousBackupsInput{
TableName: aws.String(d.Id()),
})
if err != nil && !isAWSErr(err, "UnknownOperationException", "") {
return err
}
d.Set("point_in_time_recovery", flattenDynamoDbPitr(pitrOut))

return nil
}
Loading

0 comments on commit f3d8aad

Please sign in to comment.