From 7cc316e67832ab1af843962bdf2d4eeb04f7f0f2 Mon Sep 17 00:00:00 2001 From: Nathan Mclean Date: Mon, 28 Jan 2019 13:58:43 +0000 Subject: [PATCH] add the capacityMap to the dynamoTable reqest when updating billing_mode to PROVISIONED --- aws/resource_aws_dynamodb_table.go | 2 + aws/resource_aws_dynamodb_table_test.go | 75 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/aws/resource_aws_dynamodb_table.go b/aws/resource_aws_dynamodb_table.go index 4dc7e8d2893..73f3d68e98b 100644 --- a/aws/resource_aws_dynamodb_table.go +++ b/aws/resource_aws_dynamodb_table.go @@ -405,6 +405,8 @@ func resourceAwsDynamoDbTableUpdate(d *schema.ResourceData, meta interface{}) er return err } + req.ProvisionedThroughput = expandDynamoDbProvisionedThroughput(capacityMap, billingMode) + _, err := conn.UpdateTable(req) if err != nil { return fmt.Errorf("Error updating DynamoDB Table (%s) billing mode: %s", d.Id(), err) diff --git a/aws/resource_aws_dynamodb_table_test.go b/aws/resource_aws_dynamodb_table_test.go index 88b191d739b..686c7c5513c 100644 --- a/aws/resource_aws_dynamodb_table_test.go +++ b/aws/resource_aws_dynamodb_table_test.go @@ -513,6 +513,30 @@ func TestAccAWSDynamoDbTable_BillingMode(t *testing.T) { }) } +func TestAccAWSDynamoDbTable_BillingModeUpdate(t *testing.T) { + rName := acctest.RandomWithPrefix("TerraformTestTable-") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDynamoDbTableDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDynamoDbBilling_PayPerRequest(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDynamoDbTableHasBilling_PayPerRequest("aws_dynamodb_table.basic-dynamodb-table"), + ), + }, + { + Config: testAccAWSDynamoDbBilling_Provisioned(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDynamoDbTableHasBilling_Provisioned("aws_dynamodb_table.basic-dynamodb-table"), + ), + }, + }, + }) +} + func TestAccAWSDynamoDbTable_streamSpecification(t *testing.T) { var conf dynamodb.DescribeTableOutput @@ -1139,6 +1163,39 @@ func testAccCheckDynamoDbTableHasBilling_PayPerRequest(n string) resource.TestCh } } +func testAccCheckDynamoDbTableHasBilling_Provisioned(n string) 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 DynamoDB table name specified!") + } + + conn := testAccProvider.Meta().(*AWSClient).dynamodbconn + params := &dynamodb.DescribeTableInput{ + TableName: aws.String(rs.Primary.ID), + } + resp, err := conn.DescribeTable(params) + + if err != nil { + return err + } + table := resp.Table + + if table.BillingModeSummary == nil { + return fmt.Errorf("Billing Mode summary was empty, expected summary to exist and contain billing mode %s", dynamodb.BillingModeProvisioned) + } else if aws.StringValue(table.BillingModeSummary.BillingMode) != dynamodb.BillingModeProvisioned { + return fmt.Errorf("Billing Mode was %s, not %s!", aws.StringValue(table.BillingModeSummary.BillingMode), dynamodb.BillingModeProvisioned) + + } + + return nil + } +} + func testAccCheckDynamoDbTableWasUpdated(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -1274,6 +1331,24 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" { `, rName) } +func testAccAWSDynamoDbBilling_Provisioned(rName string) string { + return fmt.Sprintf(` +resource "aws_dynamodb_table" "basic-dynamodb-table" { + name = "%s" + billing_mode = "PROVISIONED" + hash_key = "TestTableHashKey" + + read_capacity = 5 + write_capacity = 5 + + attribute { + name = "TestTableHashKey" + type = "S" + } +} +`, rName) +} + func testAccAWSDynamoDbBilling_PayPerRequestWithGSI(rName string) string { return fmt.Sprintf(` resource "aws_dynamodb_table" "basic-dynamodb-table" {