Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IoT Core Topic Rule Action to send data to an AWS IoT Analytics channel #9859

Merged
merged 3 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion aws/resource_aws_iot_topic_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,23 @@ func resourceAwsIotTopicRule() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"iot_analytics": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"channel_name": {
Type: schema.TypeString,
Required: true,
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
},
},
},
},
}
}
Expand All @@ -334,11 +351,12 @@ func createTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload {
s3Actions := d.Get("s3").(*schema.Set).List()
snsActions := d.Get("sns").(*schema.Set).List()
sqsActions := d.Get("sqs").(*schema.Set).List()
iotAnalyticsActions := d.Get("iot_analytics").(*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(s3Actions) + len(snsActions) + len(sqsActions) + len(iotAnalyticsActions)
actions := make([]*iot.Action, numActions)

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

// Add Analytic actions
for _, a := range iotAnalyticsActions {
raw := a.(map[string]interface{})
actions[i] = &iot.Action{
IotAnalytics: &iot.IotAnalyticsAction{
ChannelName: aws.String(raw["channel_name"].(string)),
RoleArn: aws.String(raw["role_arn"].(string)),
},
}
i++
}

return &iot.TopicRulePayload{
Description: aws.String(d.Get("description").(string)),
RuleDisabled: aws.Bool(!d.Get("enabled").(bool)),
Expand Down Expand Up @@ -582,6 +612,7 @@ func resourceAwsIotTopicRuleRead(d *schema.ResourceData, meta interface{}) error
d.Set("s3", flattenIoTRuleS3Actions(out.Rule.Actions))
d.Set("sns", flattenIoTRuleSnsActions(out.Rule.Actions))
d.Set("sqs", flattenIoTRuleSqsActions(out.Rule.Actions))
d.Set("iot_analytics", flattenIoTRuleIotAnalyticsActions(out.Rule.Actions))

return nil
}
Expand Down
35 changes: 35 additions & 0 deletions aws/resource_aws_iot_topic_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ func TestAccAWSIoTTopicRule_sqs(t *testing.T) {
})
}

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

func testAccCheckAWSIoTTopicRuleDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).iotconn

Expand Down Expand Up @@ -612,3 +630,20 @@ resource "aws_iot_topic_rule" "rule" {
}
`, rName)
}

func testAccAWSIoTTopicRule_iot_analytics(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"

iot_analytics {
channel_name = "fakedata"
role_arn = "${aws_iam_role.iot_role.arn}"
}
}
`, rName)
}
17 changes: 17 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2985,6 +2985,23 @@ func flattenIoTRuleSqsActions(actions []*iot.Action) []map[string]interface{} {
return results
}

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

for _, a := range actions {
result := make(map[string]interface{})
v := a.IotAnalytics
if v != nil {
result["role_arn"] = aws.StringValue(v.RoleArn)
result["channel_name"] = aws.StringValue(v.ChannelName)

results = append(results, result)
}
}

return results
}

func flattenCognitoUserPoolPasswordPolicy(s *cognitoidentityprovider.PasswordPolicyType) []map[string]interface{} {
m := map[string]interface{}{}

Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/iot_topic_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ The `sqs` object takes the following arguments:
* `role_arn` - (Required) The ARN of the IAM role that grants access.
* `use_base64` - (Required) Specifies whether to use Base64 encoding.

The `iot_analytics` object takes the following arguments:

* `channel_name` - (Required) Name of AWS IOT Analytics channel.
* `role_arn` - (Required) The ARN of the IAM role that grants access.
## Attributes Reference

In addition to all arguments above, the following attributes are exported:
Expand Down