diff --git a/aws/resource_aws_iot_topic_rule.go b/aws/resource_aws_iot_topic_rule.go index 425247d9af8..42cd8900c6b 100644 --- a/aws/resource_aws_iot_topic_rule.go +++ b/aws/resource_aws_iot_topic_rule.go @@ -124,11 +124,11 @@ func resourceAwsIotTopicRule() *schema.Resource { }, "range_key_field": { Type: schema.TypeString, - Required: true, + Optional: true, }, "range_key_value": { Type: schema.TypeString, - Required: true, + Optional: true, }, "range_key_type": { Type: schema.TypeString, @@ -380,12 +380,10 @@ func createTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload { raw := a.(map[string]interface{}) act := &iot.Action{ DynamoDB: &iot.DynamoDBAction{ - HashKeyField: aws.String(raw["hash_key_field"].(string)), - HashKeyValue: aws.String(raw["hash_key_value"].(string)), - RangeKeyField: aws.String(raw["range_key_field"].(string)), - RangeKeyValue: aws.String(raw["range_key_value"].(string)), - RoleArn: aws.String(raw["role_arn"].(string)), - TableName: aws.String(raw["table_name"].(string)), + HashKeyField: aws.String(raw["hash_key_field"].(string)), + HashKeyValue: aws.String(raw["hash_key_value"].(string)), + RoleArn: aws.String(raw["role_arn"].(string)), + TableName: aws.String(raw["table_name"].(string)), }, } if v, ok := raw["hash_key_type"].(string); ok && v != "" { @@ -394,6 +392,12 @@ func createTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload { if v, ok := raw["range_key_type"].(string); ok && v != "" { act.DynamoDB.RangeKeyType = aws.String(v) } + if v, ok := raw["range_key_field"].(string); ok && v != "" { + act.DynamoDB.RangeKeyField = aws.String(v) + } + if v, ok := raw["range_key_value"].(string); ok && v != "" { + act.DynamoDB.RangeKeyValue = aws.String(v) + } if v, ok := raw["payload_field"].(string); ok && v != "" { act.DynamoDB.PayloadField = aws.String(v) } diff --git a/aws/resource_aws_iot_topic_rule_test.go b/aws/resource_aws_iot_topic_rule_test.go index c007ec82100..3e94210b411 100644 --- a/aws/resource_aws_iot_topic_rule_test.go +++ b/aws/resource_aws_iot_topic_rule_test.go @@ -90,6 +90,30 @@ func TestAccAWSIoTTopicRule_cloudwatchmetric(t *testing.T) { }) } +func TestAccAWSIoTTopicRule_dynamodb(t *testing.T) { + rName := acctest.RandString(5) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSIoTTopicRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSIoTTopicRule_dynamodb(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSIoTTopicRuleExists_basic("aws_iot_topic_rule.rule"), + ), + }, + { + Config: testAccAWSIoTTopicRule_dynamodb_rangekeys(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSIoTTopicRuleExists_basic("aws_iot_topic_rule.rule"), + ), + }, + }, + }) +} + func TestAccAWSIoTTopicRule_elasticsearch(t *testing.T) { rName := acctest.RandString(5) @@ -343,16 +367,6 @@ resource "aws_iot_topic_rule" "rule" { sql = "SELECT * FROM 'topic/test'" sql_version = "2015-10-08" - // Fake data - dynamodb { - hash_key_field = "hash_key_field" - hash_key_value = "hash_key_value" - payload_field = "payload_field" - range_key_field = "range_key_field" - range_key_value = "range_key_value" - role_arn = "${aws_iam_role.iot_role.arn}" - table_name = "table_name" - } } `, rName) } @@ -396,6 +410,49 @@ resource "aws_iot_topic_rule" "rule" { `, rName) } +func testAccAWSIoTTopicRule_dynamodb(rName string) string { + return fmt.Sprintf(testAccAWSIoTTopicRuleRole+` +resource "aws_iot_topic_rule" "rule" { + name = "test_rule_%[1]s" + description = "Example rule" + enabled = true + sql = "SELECT * FROM 'topic/test'" + sql_version = "2015-10-08" + + dynamodb { + hash_key_field = "hash_key_field" + hash_key_value = "hash_key_value" + payload_field = "payload_field" + role_arn = "${aws_iam_role.iot_role.arn}" + table_name = "table_name" + } +} +`, rName) +} + +func testAccAWSIoTTopicRule_dynamodb_rangekeys(rName string) string { + return fmt.Sprintf(testAccAWSIoTTopicRuleRole+` +resource "aws_iot_topic_rule" "rule" { + name = "test_rule_%[1]s" + description = "Example rule" + enabled = true + sql = "SELECT * FROM 'topic/test'" + sql_version = "2015-10-08" + + dynamodb { + hash_key_field = "hash_key_field" + hash_key_value = "hash_key_value" + payload_field = "payload_field" + range_key_field = "range_key_field" + range_key_value = "range_key_value" + range_key_type = "STRING" + role_arn = "${aws_iam_role.iot_role.arn}" + table_name = "table_name" + } +} +`, rName) +} + func testAccAWSIoTTopicRule_elasticsearch(rName string) string { return fmt.Sprintf(testAccAWSIoTTopicRuleRole+` data "aws_region" "current" { diff --git a/aws/structure.go b/aws/structure.go index 3fa41fcf3c0..255a394ec25 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -2826,36 +2826,42 @@ func flattenIoTRuleCloudWatchMetricActions(actions []*iot.Action) []map[string]i } func flattenIoTRuleDynamoDbActions(actions []*iot.Action) []map[string]interface{} { - results := make([]map[string]interface{}, 0) + items := make([]map[string]interface{}, 0, len(actions)) for _, a := range actions { - result := make(map[string]interface{}) + m := make(map[string]interface{}) v := a.DynamoDB if v != nil { - result["hash_key_field"] = *v.HashKeyField - result["hash_key_value"] = *v.HashKeyValue - result["range_key_field"] = *v.RangeKeyField - result["range_key_value"] = *v.RangeKeyValue - result["role_arn"] = *v.RoleArn - result["table_name"] = *v.TableName + m["hash_key_field"] = aws.StringValue(v.HashKeyField) + m["hash_key_value"] = aws.StringValue(v.HashKeyValue) + m["role_arn"] = aws.StringValue(v.RoleArn) + m["table_name"] = aws.StringValue(v.TableName) if v.HashKeyType != nil { - result["hash_key_type"] = *v.HashKeyType + m["hash_key_type"] = aws.StringValue(v.HashKeyType) } if v.PayloadField != nil { - result["payload_field"] = *v.PayloadField + m["payload_field"] = aws.StringValue(v.PayloadField) + } + + if v.RangeKeyField != nil { + m["range_key_field"] = aws.StringValue(v.RangeKeyField) } if v.RangeKeyType != nil { - result["range_key_type"] = *v.RangeKeyType + m["range_key_type"] = aws.StringValue(v.RangeKeyType) } - results = append(results, result) + if v.RangeKeyValue != nil { + m["range_key_value"] = aws.StringValue(v.RangeKeyValue) + } + + items = append(items, m) } } - return results + return items } func flattenIoTRuleElasticSearchActions(actions []*iot.Action) []map[string]interface{} {