Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/edwardbrowncross/terraform-…
Browse files Browse the repository at this point in the history
…provider-aws into f-aws_iot_topic_rule-add-actions
  • Loading branch information
bflad committed May 12, 2020
2 parents 4a53185 + fe372e4 commit 8ed155b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
38 changes: 35 additions & 3 deletions aws/resource_aws_iot_topic_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ func resourceAwsIotTopicRule() *schema.Resource {
},
},
},
"dynamodbv2": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"table_name": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"elasticsearch": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -327,6 +344,7 @@ func createTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload {
cloudwatchAlarmActions := d.Get("cloudwatch_alarm").(*schema.Set).List()
cloudwatchMetricActions := d.Get("cloudwatch_metric").(*schema.Set).List()
dynamoDbActions := d.Get("dynamodb").(*schema.Set).List()
dynamoDbv2Actions := d.Get("dynamodbv2").(*schema.Set).List()
elasticsearchActions := d.Get("elasticsearch").(*schema.Set).List()
firehoseActions := d.Get("firehose").(*schema.Set).List()
kinesisActions := d.Get("kinesis").(*schema.Set).List()
Expand All @@ -337,9 +355,9 @@ func createTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload {
sqsActions := d.Get("sqs").(*schema.Set).List()

numActions := len(cloudwatchAlarmActions) + len(cloudwatchMetricActions) +
len(dynamoDbActions) + len(elasticsearchActions) + len(firehoseActions) +
len(kinesisActions) + len(lambdaActions) + len(republishActions) +
len(s3Actions) + len(snsActions) + len(sqsActions)
len(dynamoDbActions) + len(dynamoDbv2Actions) + len(elasticsearchActions) +
len(firehoseActions) + len(kinesisActions) + len(lambdaActions) +
len(republishActions) + len(s3Actions) + len(snsActions) + len(sqsActions)
actions := make([]*iot.Action, numActions)

i := 0
Expand Down Expand Up @@ -406,6 +424,19 @@ func createTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload {
i++
}

// Add DynamoDBv2 actions
for _, a := range dynamoDbv2Actions {
raw := a.(map[string]interface{})
act := &iot.Action{
DynamoDBv2: &iot.DynamoDBv2Action{
PutItem: &iot.PutItemInput{TableName: aws.String(raw["table_name"].(string))},
RoleArn: aws.String(raw["role_arn"].(string)),
},
}
actions[i] = act
i++
}

// Add Elasticsearch actions

for _, a := range elasticsearchActions {
Expand Down Expand Up @@ -575,6 +606,7 @@ func resourceAwsIotTopicRuleRead(d *schema.ResourceData, meta interface{}) error
d.Set("cloudwatch_alarm", flattenIoTRuleCloudWatchAlarmActions(out.Rule.Actions))
d.Set("cloudwatch_metric", flattenIoTRuleCloudWatchMetricActions(out.Rule.Actions))
d.Set("dynamodb", flattenIoTRuleDynamoDbActions(out.Rule.Actions))
d.Set("dynamodbv2", flattenIoTRuleDynamoDbv2Actions(out.Rule.Actions))
d.Set("elasticsearch", flattenIoTRuleElasticSearchActions(out.Rule.Actions))
d.Set("firehose", flattenIoTRuleFirehoseActions(out.Rule.Actions))
d.Set("kinesis", flattenIoTRuleKinesisActions(out.Rule.Actions))
Expand Down
39 changes: 39 additions & 0 deletions aws/resource_aws_iot_topic_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ func TestAccAWSIoTTopicRule_dynamodb(t *testing.T) {
})
}

func TestAccAWSIoTTopicRule_dynamoDbv2(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_dynamoDbv2(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSIoTTopicRuleExists_basic("aws_iot_topic_rule.rule"),
),
},
},
})
}

func TestAccAWSIoTTopicRule_elasticsearch(t *testing.T) {
rName := acctest.RandString(5)
resourceName := "aws_iot_topic_rule.rule"
Expand Down Expand Up @@ -524,6 +542,27 @@ resource "aws_iot_topic_rule" "rule" {
`, rName)
}

func testAccAWSIoTTopicRule_dynamoDbv2(rName string) string {
return fmt.Sprintf(testAccAWSIoTTopicRuleRole+`
data "aws_region" "current" {
current = true
}
resource "aws_iot_topic_rule" "rule" {
name = "test_rule_%[1]s"
description = "Example rule"
enabled = true
sql = "SELECT field as column_name FROM 'topic/test'"
sql_version = "2015-10-08"
dynamodbv2 {
table_name = "${aws_iam_role.iot_role.arn}"
role_arn = "${aws_dynamodb_table.iot_table.arn}"
}
}
`, rName)
}

func testAccAWSIoTTopicRule_dynamodb(rName string) string {
return fmt.Sprintf(testAccAWSIoTTopicRuleRole+`
resource "aws_iot_topic_rule" "rule" {
Expand Down
16 changes: 16 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,22 @@ func flattenIoTRuleDynamoDbActions(actions []*iot.Action) []map[string]interface
return items
}

func flattenIoTRuleDynamoDbv2Actions(actions []*iot.Action) []map[string]interface{} {
results := make([]map[string]interface{}, 0)

for _, a := range actions {
result := make(map[string]interface{})
v := a.DynamoDBv2
if v != nil {
result["role_arn"] = *v.RoleArn
result["table_name"] = *v.PutItem
results = append(results, result)
}
}

return results
}

func flattenIoTRuleElasticSearchActions(actions []*iot.Action) []map[string]interface{} {
results := make([]map[string]interface{}, 0)

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/iot_topic_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ The `dynamodb` object takes the following arguments:
* `role_arn` - (Required) The ARN of the IAM role that grants access to the DynamoDB table.
* `table_name` - (Required) The name of the DynamoDB table.

The `dynamodbv2` object takes the following arguments:

* `role_arn` - (Required) The ARN of the IAM role that grants access to the DynamoDB table.
* `table_name` - (Required) The name of the DynamoDB table.

The `elasticsearch` object takes the following arguments:

* `endpoint` - (Required) The endpoint of your Elasticsearch domain.
Expand Down

0 comments on commit 8ed155b

Please sign in to comment.