From 31b8cde45cdf61294c5b84d82d34e121f021b661 Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Mon, 25 Jul 2016 15:31:00 +0100 Subject: [PATCH] provider/aws: Support Import `aws_dynamodb_table` (#7352) There were some changes required to the Read func to get this working. The initial set of tests showed the following: ``` testing.go:255: Step 1 error: ImportStateVerify attributes not equivalent. Difference is shown below. Top is actual, bottom is expected. (map[string]string) { } (map[string]string) (len=8) { (string) (len=8) "hash_key": (string) (len=16) "TestTableHashKey", (string) (len=23) "local_secondary_index.#": (string) (len=1) "1", (string) (len=36) "local_secondary_index.884610231.name": (string) (len=12) "TestTableLSI", (string) (len=52) "local_secondary_index.884610231.non_key_attributes.#": (string) (len=1) "0", (string) (len=47) "local_secondary_index.884610231.projection_type": (string) (len=3) "ALL", (string) (len=41) "local_secondary_index.884610231.range_key": (string) (len=15) "TestLSIRangeKey", (string) (len=4) "name": (string) (len=38) "TerraformTestTable-2710929679033484576", (string) (len=9) "range_key": (string) (len=17) "TestTableRangeKey" } ``` On investigation, this was telling me that `hash_key`, `range_key`, `name` and `local_secondary_index` were not being set on the Read func When they were being set, all looks as expected: ``` make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSDynamoDbTable_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /vendor/) TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSDynamoDbTable_ -timeout 120m === RUN TestAccAWSDynamoDbTable_importBasic --- PASS: TestAccAWSDynamoDbTable_importBasic (20.39s) === RUN TestAccAWSDynamoDbTable_basic --- PASS: TestAccAWSDynamoDbTable_basic (39.99s) === RUN TestAccAWSDynamoDbTable_streamSpecification --- PASS: TestAccAWSDynamoDbTable_streamSpecification (50.44s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 110.841s ``` --- .../aws/import_aws_dynamodb_table_test.go | 28 +++++++++++++ .../aws/resource_aws_dynamodb_table.go | 41 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 builtin/providers/aws/import_aws_dynamodb_table_test.go diff --git a/builtin/providers/aws/import_aws_dynamodb_table_test.go b/builtin/providers/aws/import_aws_dynamodb_table_test.go new file mode 100644 index 0000000000..49da4b0eba --- /dev/null +++ b/builtin/providers/aws/import_aws_dynamodb_table_test.go @@ -0,0 +1,28 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSDynamoDbTable_importBasic(t *testing.T) { + resourceName := "aws_dynamodb_table.basic-dynamodb-table" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDynamoDbTableDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSDynamoDbConfigInitialState(), + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/aws/resource_aws_dynamodb_table.go b/builtin/providers/aws/resource_aws_dynamodb_table.go index 88c5838533..04515b27fc 100644 --- a/builtin/providers/aws/resource_aws_dynamodb_table.go +++ b/builtin/providers/aws/resource_aws_dynamodb_table.go @@ -34,6 +34,9 @@ func resourceAwsDynamoDbTable() *schema.Resource { Read: resourceAwsDynamoDbTableRead, Update: resourceAwsDynamoDbTableUpdate, Delete: resourceAwsDynamoDbTableDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "arn": &schema.Schema{ @@ -601,6 +604,44 @@ func resourceAwsDynamoDbTableRead(d *schema.ResourceData, meta interface{}) erro } d.Set("attribute", attributes) + d.Set("name", table.TableName) + + for _, attribute := range table.KeySchema { + if *attribute.KeyType == "HASH" { + d.Set("hash_key", attribute.AttributeName) + } + + if *attribute.KeyType == "RANGE" { + d.Set("range_key", attribute.AttributeName) + } + } + + lsiList := make([]map[string]interface{}, 0, len(table.LocalSecondaryIndexes)) + for _, lsiObject := range table.LocalSecondaryIndexes { + lsi := map[string]interface{}{ + "name": *lsiObject.IndexName, + "projection_type": *lsiObject.Projection.ProjectionType, + } + + for _, attribute := range lsiObject.KeySchema { + + if *attribute.KeyType == "RANGE" { + lsi["range_key"] = *attribute.AttributeName + } + } + nkaList := make([]string, len(lsiObject.Projection.NonKeyAttributes)) + for _, nka := range lsiObject.Projection.NonKeyAttributes { + nkaList = append(nkaList, *nka) + } + lsi["non_key_attributes"] = nkaList + + lsiList = append(lsiList, lsi) + } + + err = d.Set("local_secondary_index", lsiList) + if err != nil { + return err + } gsiList := make([]map[string]interface{}, 0, len(table.GlobalSecondaryIndexes)) for _, gsiObject := range table.GlobalSecondaryIndexes {