Skip to content

Commit

Permalink
Update API request for IoT Topic Rules
Browse files Browse the repository at this point in the history
* Add an acceptance test for asserting a successful dynamodb request

* Move optional attributes out of the default API payload; They are now
only added if specified in the terraform configuration for dynamodb.
  • Loading branch information
nywilken committed Feb 8, 2019
1 parent 282bc79 commit b7d6c0e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
16 changes: 10 additions & 6 deletions aws/resource_aws_iot_topic_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
}
Expand Down
48 changes: 38 additions & 10 deletions aws/resource_aws_iot_topic_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ 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"),
),
},
},
})
}

func TestAccAWSIoTTopicRule_elasticsearch(t *testing.T) {
rName := acctest.RandString(5)

Expand Down Expand Up @@ -343,16 +361,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)
}
Expand Down Expand Up @@ -396,6 +404,26 @@ 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_elasticsearch(rName string) string {
return fmt.Sprintf(testAccAWSIoTTopicRuleRole+`
data "aws_region" "current" {
Expand Down
10 changes: 8 additions & 2 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2834,8 +2834,6 @@ func flattenIoTRuleDynamoDbActions(actions []*iot.Action) []map[string]interface
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

Expand All @@ -2847,10 +2845,18 @@ func flattenIoTRuleDynamoDbActions(actions []*iot.Action) []map[string]interface
result["payload_field"] = *v.PayloadField
}

if v.RangeKeyField != nil {
result["range_key_field"] = *v.RangeKeyField
}

if v.RangeKeyType != nil {
result["range_key_type"] = *v.RangeKeyType
}

if v.RangeKeyValue != nil {
result["range_key_value"] = *v.RangeKeyValue
}

results = append(results, result)
}
}
Expand Down

0 comments on commit b7d6c0e

Please sign in to comment.