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

New Data Source: aws_dynamodb_table #2062

Merged
merged 2 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
193 changes: 193 additions & 0 deletions aws/data_source_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package aws

import (
"bytes"
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsDynamoDbTable() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDynamoDbTableRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},
"attribute": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
return hashcode.String(buf.String())
},
},
"global_secondary_index": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"write_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"read_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"hash_key": {
Type: schema.TypeString,
Computed: true,
},
"range_key": {
Type: schema.TypeString,
Computed: true,
},
"projection_type": {
Type: schema.TypeString,
Computed: true,
},
"non_key_attributes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"hash_key": {
Type: schema.TypeString,
Computed: true,
},
"local_secondary_index": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"range_key": {
Type: schema.TypeString,
Computed: true,
},
"projection_type": {
Type: schema.TypeString,
Computed: true,
},
"non_key_attributes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
return hashcode.String(buf.String())
},
},
"range_key": {
Type: schema.TypeString,
Computed: true,
},
"read_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"stream_arn": {
Type: schema.TypeString,
Computed: true,
},
"stream_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"stream_label": {
Type: schema.TypeString,
Computed: true,
},
"stream_view_type": {
Type: schema.TypeString,
Computed: true,
StateFunc: func(v interface{}) string {
value := v.(string)
return strings.ToUpper(value)
},
},
"tags": tagsSchemaComputed(),
"ttl": {
Type: schema.TypeSet,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"attribute_name": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"write_capacity": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceAwsDynamoDbTableRead(d *schema.ResourceData, meta interface{}) error {
dynamodbconn := meta.(*AWSClient).dynamodbconn
log.Printf("[DEBUG] Loading data for DynamoDB table '%s'", d.Id())
req := &dynamodb.DescribeTableInput{
TableName: aws.String(d.Get("name").(string)),
}

result, err := dynamodbconn.DescribeTable(req)

if err != nil {
return errwrap.Wrapf("Error retrieving DynamoDB table: {{err}}", err)
}

d.SetId(*result.Table.TableName)

return flattenAwsDynamoDbTableResource(d, meta, result.Table)
}
79 changes: 79 additions & 0 deletions aws/data_source_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAwsDynamoDbTable_basic(t *testing.T) {
tableName := fmt.Sprintf("testaccawsdynamodbtable-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsDynamoDbTableConfigBasic(tableName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "name", tableName),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "read_capacity", "20"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "write_capacity", "20"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "hash_key", "UserId"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "range_key", "GameTitle"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "attribute.#", "3"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "global_secondary_index.#", "1"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "tags.%", "2"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "tags.Name", "dynamodb-table-1"),
resource.TestCheckResourceAttr("data.aws_dynamodb_table.dynamodb_table_test", "tags.Environment", "test"),
),
},
},
})
}

func testAccDataSourceAwsDynamoDbTableConfigBasic(tableName string) string {
return fmt.Sprintf(`resource "aws_dynamodb_table" "dynamodb_table_test" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the configuration so that is uses 2 spaces per indentation? this would stick with other resources/data sources.

Thanks :)

name = "%s"
read_capacity = 20
write_capacity = 20
hash_key = "UserId"
range_key = "GameTitle"

attribute {
name = "UserId"
type = "S"
}

attribute {
name = "GameTitle"
type = "S"
}

attribute {
name = "TopScore"
type = "N"
}

global_secondary_index {
name = "GameTitleIndex"
hash_key = "GameTitle"
range_key = "TopScore"
write_capacity = 10
read_capacity = 10
projection_type = "INCLUDE"
non_key_attributes = ["UserId"]
}

tags {
Name = "dynamodb-table-1"
Environment = "test"
}
}

data "aws_dynamodb_table" "dynamodb_table_test" {
name = "${aws_dynamodb_table.dynamodb_table_test.name}"
}`, tableName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func Provider() terraform.ResourceProvider {
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
"aws_ebs_volume": dataSourceAwsEbsVolume(),
Expand Down
8 changes: 6 additions & 2 deletions aws/resource_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,11 @@ func resourceAwsDynamoDbTableRead(d *schema.ResourceData, meta interface{}) erro
return err
}

table := result.Table
return flattenAwsDynamoDbTableResource(d, meta, result.Table)
}

func flattenAwsDynamoDbTableResource(d *schema.ResourceData, meta interface{}, table *dynamodb.TableDescription) error {
dynamodbconn := meta.(*AWSClient).dynamodbconn

d.Set("write_capacity", table.ProvisionedThroughput.WriteCapacityUnits)
d.Set("read_capacity", table.ProvisionedThroughput.ReadCapacityUnits)
Expand Down Expand Up @@ -753,7 +757,7 @@ func resourceAwsDynamoDbTableRead(d *schema.ResourceData, meta interface{}) erro
lsiList = append(lsiList, lsi)
}

err = d.Set("local_secondary_index", lsiList)
err := d.Set("local_secondary_index", lsiList)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<li<%= sidebar_current("docs-aws-datasource-db-snapshot") %>>
<a href="/docs/providers/aws/d/db_snapshot.html">aws_db_snapshot</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-dynamodb-table") %>>
<a href="/docs/providers/aws/d/dynamodb_table.html">aws_dynamodb_table</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-ebs-snapshot") %>>
<a href="/docs/providers/aws/d/ebs_snapshot.html">aws_ebs_snapshot</a>
</li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/dynamodb_table.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "aws"
page_title: "AWS: aws_dynamodb_table"
sidebar_current: "docs-aws-datasource-dynamodb-table"
description: |-
Provides a DynamoDB table data source.
---

# aws_dynamodb_table

Provides information about a DynamoDB table.

## Example Usage

```hcl
data "aws_dynamodb_table" "tableName" {
name = "tableName"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the DynamoDB table.

## Attributes Reference

See the [DynamoDB Table Resource](/docs/providers/aws/r/dynamodb_table.html) for details on the
returned attributes - they are identical.