-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)