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

Implement error_action on aws_iot_topic_rule #11922

Merged
merged 2 commits into from
Jun 23, 2020
Merged

Conversation

kurtmc
Copy link
Contributor

@kurtmc kurtmc commented Feb 6, 2020

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for pull request followers and do not help prioritize the request

Relates OR Closes #7147

Release note for CHANGELOG:

Add `error_action` configuration block to `aws_iot_topic_rule`

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccAWSIoTTopicRule'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -count 1 -parallel 20 -run=TestAccAWSIoTTopicRule -timeout 120m
?       github.com/terraform-providers/terraform-provider-aws   [no test files]
=== RUN   TestAccAWSIoTTopicRule_basic
=== PAUSE TestAccAWSIoTTopicRule_basic
=== RUN   TestAccAWSIoTTopicRule_cloudwatchalarm
=== PAUSE TestAccAWSIoTTopicRule_cloudwatchalarm
=== RUN   TestAccAWSIoTTopicRule_cloudwatchmetric
=== PAUSE TestAccAWSIoTTopicRule_cloudwatchmetric
=== RUN   TestAccAWSIoTTopicRule_dynamodb
=== PAUSE TestAccAWSIoTTopicRule_dynamodb
=== RUN   TestAccAWSIoTTopicRule_dynamoDbv2
=== PAUSE TestAccAWSIoTTopicRule_dynamoDbv2
=== RUN   TestAccAWSIoTTopicRule_elasticsearch
=== PAUSE TestAccAWSIoTTopicRule_elasticsearch
=== RUN   TestAccAWSIoTTopicRule_firehose
=== PAUSE TestAccAWSIoTTopicRule_firehose
=== RUN   TestAccAWSIoTTopicRule_firehose_separator
=== PAUSE TestAccAWSIoTTopicRule_firehose_separator
=== RUN   TestAccAWSIoTTopicRule_kinesis
=== PAUSE TestAccAWSIoTTopicRule_kinesis
=== RUN   TestAccAWSIoTTopicRule_lambda
=== PAUSE TestAccAWSIoTTopicRule_lambda
=== RUN   TestAccAWSIoTTopicRule_republish
=== PAUSE TestAccAWSIoTTopicRule_republish
=== RUN   TestAccAWSIoTTopicRule_republish_with_qos
=== PAUSE TestAccAWSIoTTopicRule_republish_with_qos
=== RUN   TestAccAWSIoTTopicRule_s3
=== PAUSE TestAccAWSIoTTopicRule_s3
=== RUN   TestAccAWSIoTTopicRule_sns
=== PAUSE TestAccAWSIoTTopicRule_sns
=== RUN   TestAccAWSIoTTopicRule_sqs
=== PAUSE TestAccAWSIoTTopicRule_sqs
=== RUN   TestAccAWSIoTTopicRule_step_functions
=== PAUSE TestAccAWSIoTTopicRule_step_functions
=== RUN   TestAccAWSIoTTopicRule_iot_analytics
=== PAUSE TestAccAWSIoTTopicRule_iot_analytics
=== RUN   TestAccAWSIoTTopicRule_iot_events
=== PAUSE TestAccAWSIoTTopicRule_iot_events
=== RUN   TestAccAWSIoTTopicRule_Tags
=== PAUSE TestAccAWSIoTTopicRule_Tags
=== RUN   TestAccAWSIoTTopicRule_errorAction
=== PAUSE TestAccAWSIoTTopicRule_errorAction
=== CONT  TestAccAWSIoTTopicRule_basic
=== CONT  TestAccAWSIoTTopicRule_republish_with_qos
=== CONT  TestAccAWSIoTTopicRule_republish
=== CONT  TestAccAWSIoTTopicRule_errorAction
=== CONT  TestAccAWSIoTTopicRule_lambda
=== CONT  TestAccAWSIoTTopicRule_Tags
=== CONT  TestAccAWSIoTTopicRule_iot_events
=== CONT  TestAccAWSIoTTopicRule_sqs
=== CONT  TestAccAWSIoTTopicRule_sns
=== CONT  TestAccAWSIoTTopicRule_s3
=== CONT  TestAccAWSIoTTopicRule_elasticsearch
=== CONT  TestAccAWSIoTTopicRule_kinesis
=== CONT  TestAccAWSIoTTopicRule_firehose_separator
=== CONT  TestAccAWSIoTTopicRule_firehose
=== CONT  TestAccAWSIoTTopicRule_step_functions
=== CONT  TestAccAWSIoTTopicRule_iot_analytics
=== CONT  TestAccAWSIoTTopicRule_dynamodb
=== CONT  TestAccAWSIoTTopicRule_dynamoDbv2
=== CONT  TestAccAWSIoTTopicRule_cloudwatchmetric
=== CONT  TestAccAWSIoTTopicRule_cloudwatchalarm
--- PASS: TestAccAWSIoTTopicRule_iot_events (57.80s)
--- PASS: TestAccAWSIoTTopicRule_iot_analytics (57.88s)
--- PASS: TestAccAWSIoTTopicRule_dynamoDbv2 (57.89s)
--- PASS: TestAccAWSIoTTopicRule_republish (62.16s)
--- PASS: TestAccAWSIoTTopicRule_basic (62.31s)
--- PASS: TestAccAWSIoTTopicRule_sqs (63.13s)
--- PASS: TestAccAWSIoTTopicRule_s3 (63.25s)
--- PASS: TestAccAWSIoTTopicRule_kinesis (63.72s)
--- PASS: TestAccAWSIoTTopicRule_cloudwatchalarm (63.95s)
--- PASS: TestAccAWSIoTTopicRule_firehose (64.39s)
--- PASS: TestAccAWSIoTTopicRule_republish_with_qos (64.41s)
--- PASS: TestAccAWSIoTTopicRule_cloudwatchmetric (64.46s)
--- PASS: TestAccAWSIoTTopicRule_errorAction (64.54s)
--- PASS: TestAccAWSIoTTopicRule_sns (64.57s)
--- PASS: TestAccAWSIoTTopicRule_step_functions (64.61s)
--- PASS: TestAccAWSIoTTopicRule_lambda (64.66s)
--- PASS: TestAccAWSIoTTopicRule_elasticsearch (67.20s)
--- PASS: TestAccAWSIoTTopicRule_dynamodb (99.45s)
--- PASS: TestAccAWSIoTTopicRule_firehose_separator (99.75s)
--- PASS: TestAccAWSIoTTopicRule_Tags (140.39s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       140.509s
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws/internal/flatmap      0.027s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags 0.057s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws/internal/naming       0.012s [no tests to run]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/apigatewayv2/waiter [no test files]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws/internal/service/batch/equivalency   0.033s [no tests to run]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/waiter   [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ecs/waiter   [no test files]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws/internal/service/eks/token    0.017s [no tests to run]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/guardduty/waiter    [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/iam/waiter   [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/kinesisanalytics/waiter      [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/kms/waiter   [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/neptune/waiter      [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/rds/waiter   [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/secretsmanager/waiter[no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/servicediscovery/waiter      [no test files]
?       github.com/terraform-providers/terraform-provider-aws/aws/internal/service/workspaces/waiter   [no test files]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws/internal/tfawsresource        0.034s [no tests to run]
?       github.com/terraform-providers/terraform-provider-aws/awsproviderlint   [no test files]
?       github.com/terraform-providers/terraform-provider-aws/awsproviderlint/helper/awsprovidertype/keyvaluetags       [no test files]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes    0.019s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSAT001   0.014s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSR001    0.017s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSR002    0.011s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/fmtsprintfcallexpr0.001s [no tests to run]

@kurtmc kurtmc requested a review from a team February 6, 2020 03:21
@ghost ghost added size/XL Managed by automation to categorize the size of a PR. needs-triage Waiting for first response or review from a maintainer. service/iot Issues and PRs that pertain to the iot service. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. documentation Introduces or discusses updates to documentation. labels Feb 6, 2020
@ghost ghost added size/XXL Managed by automation to categorize the size of a PR. and removed size/XL Managed by automation to categorize the size of a PR. labels Jun 21, 2020
@kurtmc
Copy link
Contributor Author

kurtmc commented Jun 21, 2020

I am open to suggestion about how to do this better. Right now this PR has a lot of repeated logic. I think it would be cleaner if the actions where moved from the root of the resource into it's own block like this:

  actions {
    sns {
      message_format = "RAW"
      role_arn       = "${aws_iam_role.role.arn}"
      target_arn     = "${aws_sns_topic.mytopic.arn}"
    }
  }
  error_action {
    sns {
      message_format = "RAW"
      role_arn       = "${aws_iam_role.role.arn}"
      target_arn     = "${aws_sns_topic.myerrortopic.arn}"
    }
  }

@bflad
Copy link
Contributor

bflad commented Jun 23, 2020

@kurtmc I think that should be the plan long-term (e.g. deprecation of root attributes in 3.x and removal in 4.0.0) 👍 Since I recently reviewed the resource code as part of merging some actions additions, I will provide a review shortly.

@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. and removed needs-triage Waiting for first response or review from a maintainer. labels Jun 23, 2020
@bflad bflad self-assigned this Jun 23, 2020
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kurtmc 👋 Thank you for submitting this -- overall this looks to be on the right track and we can get this in with a few adjustments. I think our main change here would be to prefer leaving the schema statically generated for now, which is our preferred pattern for resources in this provider unless absolutely necessary. Thanks!

Required: true,
ValidateFunc: validateIoTTopicRuleCloudWatchAlarmStateValue,
},
actionsSchema := map[string]*schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we will very likely want to deprecate the root attributes and since we typically prefer static schema declarations, it would be great if the existing schema was left alone and the new error_action handling was additive (copy-paste is fine) for now. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bflad Thanks for the review. One thing I did not know how to do for the error_actions block was to limit it to only 1 action, since the AWS API accepts multiple actions and only a single error action. Can you point me in the direction to add that validation to the schema?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! You can use a combination of a top level attribute of Type: schema.TypeList + MaxItems: 1 then each of the nested attributes can set ExactlyOneOf (this will require re-defining the same attribute list for each attribute since this validation functionality is per-attribute, but we have plans to move this to the resource level at some point), e.g.

// Quick conceptual schema implementation
"error_action": {
  Type: schema.TypeList,
  Optional: true,
  MaxItems: 1,
  Elem: &schema.Resource{
    Schema: map[string]*schema.Schema{
      "cloudwatch_alarm": {
         Type: schema.TypeList,
         Optional: true,
         MaxItems: 1,
         Elem: &schema.Resource{ /* ... */ },
         ExactlyOneOf: []string{
           "error_action.0.cloudwatch_alarm",
           "error_action.0.dynamodb",
           "error_action.0.dynamodbv2",
           /* ... */
         },
      },
      "dynamodb": {
         Type: schema.TypeList,
         Optional: true,
         MaxItems: 1,
         Elem: &schema.Resource{ /* ... */ },
         ExactlyOneOf: []string{
           "error_action.0.cloudwatch_alarm",
           "error_action.0.dynamodb",
           "error_action.0.dynamodbv2",
           /* ... */
         },
      },
    },
  },
}

As shown above for the new schema it would be nice to have the underlying configuration blocks using TypeList and MaxItems: 1 as well to prevent multiple configuration blocks. Just note that the attribute type assertions via .(*schema.Set).List() will become just .([]interface{}). 😄

errorAction := d.Get("error_action").(*schema.Set).List()
if len(errorAction) > 0 {
for k, v := range errorAction[0].(map[string]interface{}) {
if k == "cloudwatch_alarm" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A switch statement might be clearer here

@@ -76,6 +88,7 @@ EOF
* `enabled` - (Required) Specifies whether the rule is enabled.
* `sql` - (Required) The SQL statement used to query the topic. For more information, see AWS IoT SQL Reference (http://docs.aws.amazon.com/iot/latest/developerguide/iot-rules.html#aws-iot-sql-reference) in the AWS IoT Developer Guide.
* `sql_version` - (Required) The version of the SQL rules engine to use when evaluating the rule.
* `error_action` - (Optional) The error action to be associated with the rule.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are adding a new configuration block, we should include all the details of the nested configuration blocks (or at least reference them). 👍

* `error_action` - (Optional) Configuration block with error action to be associated with the rule. See the documentation for `cloudwatch_alarm`, `dynamodb`, `dynamodbv2`, ....... configuration blocks for further configuration details.

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Jun 23, 2020
@ghost ghost added size/XL Managed by automation to categorize the size of a PR. and removed size/XXL Managed by automation to categorize the size of a PR. labels Jun 23, 2020
@kurtmc kurtmc requested a review from bflad June 23, 2020 10:46
@kurtmc
Copy link
Contributor Author

kurtmc commented Jun 23, 2020

Thanks for the review! I have adjusted my PR based on your comments and re-requested a review.

@ghost ghost removed the waiting-response Maintainers are waiting on response from community or contributor. label Jun 23, 2020
@bflad bflad added this to the v2.68.0 milestone Jun 23, 2020
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks, @kurtmc 🚀

Output from acceptance testing:

--- PASS: TestAccAWSIoTTopicRule_iot_analytics (17.89s)
--- PASS: TestAccAWSIoTTopicRule_republish (19.37s)
--- PASS: TestAccAWSIoTTopicRule_kinesis (20.02s)
--- PASS: TestAccAWSIoTTopicRule_cloudwatchalarm (21.84s)
--- PASS: TestAccAWSIoTTopicRule_iot_events (22.06s)
--- PASS: TestAccAWSIoTTopicRule_sqs (21.99s)
--- PASS: TestAccAWSIoTTopicRule_basic (22.29s)
--- PASS: TestAccAWSIoTTopicRule_dynamoDbv2 (22.30s)
--- PASS: TestAccAWSIoTTopicRule_errorAction (22.98s)
--- PASS: TestAccAWSIoTTopicRule_cloudwatchmetric (22.87s)
--- PASS: TestAccAWSIoTTopicRule_s3 (23.61s)
--- PASS: TestAccAWSIoTTopicRule_elasticsearch (24.26s)
--- PASS: TestAccAWSIoTTopicRule_republish_with_qos (24.70s)
--- PASS: TestAccAWSIoTTopicRule_sns (24.71s)
--- PASS: TestAccAWSIoTTopicRule_lambda (24.91s)
--- PASS: TestAccAWSIoTTopicRule_firehose (24.83s)
--- PASS: TestAccAWSIoTTopicRule_step_functions (24.81s)
--- PASS: TestAccAWSIoTTopicRule_firehose_separator (29.99s)
--- PASS: TestAccAWSIoTTopicRule_Tags (39.72s)
--- PASS: TestAccAWSIoTTopicRule_dynamodb (41.09s)

@bflad bflad merged commit b5761f5 into hashicorp:master Jun 23, 2020
bflad added a commit that referenced this pull request Jun 23, 2020
@ghost
Copy link

ghost commented Jun 26, 2020

This has been released in version 2.68.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link

ghost commented Jul 23, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Jul 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. enhancement Requests to existing resources that expand the functionality or scope. service/iot Issues and PRs that pertain to the iot service. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for IOT Rule Action "errorAction"
2 participants