From 38f631859bbe26a77d20c7d0ef0816312d308930 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sun, 14 Feb 2021 18:27:47 +0200 Subject: [PATCH 01/10] add encoding and tests --- aws/resource_aws_ses_receipt_rule.go | 9 ++ aws/resource_aws_ses_receipt_rule_test.go | 160 ++++++++++++++++++++-- 2 files changed, 155 insertions(+), 14 deletions(-) diff --git a/aws/resource_aws_ses_receipt_rule.go b/aws/resource_aws_ses_receipt_rule.go index a8e21cdf772..7e04351f32a 100644 --- a/aws/resource_aws_ses_receipt_rule.go +++ b/aws/resource_aws_ses_receipt_rule.go @@ -285,6 +285,12 @@ func resourceAwsSesReceiptRule() *schema.Resource { Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "encoding": { + Type: schema.TypeString, + Default: ses.SNSActionEncodingUtf8, + Optional: true, + ValidateFunc: validation.StringInSlice(ses.SNSActionEncoding_Values(), false), + }, "topic_arn": { Type: schema.TypeString, Required: true, @@ -300,6 +306,7 @@ func resourceAwsSesReceiptRule() *schema.Resource { Set: func(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%s-", m["encoding"].(string))) buf.WriteString(fmt.Sprintf("%s-", m["topic_arn"].(string))) buf.WriteString(fmt.Sprintf("%d-", m["position"].(int))) @@ -560,6 +567,7 @@ func resourceAwsSesReceiptRuleRead(d *schema.ResourceData, meta interface{}) err if element.SNSAction != nil { snsAction := map[string]interface{}{ "topic_arn": aws.StringValue(element.SNSAction.TopicArn), + "encoding": aws.StringValue(element.SNSAction.Encoding), "position": i + 1, } @@ -771,6 +779,7 @@ func buildReceiptRule(d *schema.ResourceData) *ses.ReceiptRule { snsAction := &ses.SNSAction{ TopicArn: aws.String(elem["topic_arn"].(string)), + Encoding: aws.String(elem["encoding"].(string)), } actions[elem["position"].(int)] = &ses.ReceiptAction{ diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index e30041cd004..f83a6f06229 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -2,7 +2,6 @@ package aws import ( "fmt" - "reflect" "testing" "github.com/aws/aws-sdk-go/aws" @@ -31,7 +30,21 @@ func TestAccAWSSESReceiptRule_basic(t *testing.T) { Config: testAccAWSSESReceiptRuleBasicConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAwsSESReceiptRuleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "rule_set_name", rName), testAccCheckResourceAttrRegionalARN(resourceName, "arn", "ses", fmt.Sprintf("receipt-rule-set/%s:receipt-rule/%s", rName, rName)), + resource.TestCheckResourceAttr(resourceName, "add_header_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "bounce_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "lambda_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "s3_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sns_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "stop_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "workmail_action.#", "0"), + resource.TestCheckResourceAttr(resourceName, "recipients.#", "1"), + resource.TestCheckTypeSetElemAttr(resourceName, "recipients.*", "test@example.com"), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "scan_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "tls_policy", "Require"), ), }, { @@ -61,6 +74,72 @@ func TestAccAWSSESReceiptRule_s3Action(t *testing.T) { Config: testAccAWSSESReceiptRuleS3ActionConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAwsSESReceiptRuleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "s3_action.#", "1"), + resource.TestCheckTypeSetElemAttrPair(resourceName, "s3_action.*.bucket_name", "aws_s3_bucket.test", "id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAwsSesReceiptRuleImportStateIdFunc(resourceName), + }, + }, + }) +} + +func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ses_receipt_rule.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAWSSES(t) + testAccPreCheckSESReceiptRule(t) + }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSESReceiptRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSESReceiptRuleSNSActionConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsSESReceiptRuleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "sns_action.#", "1"), + resource.TestCheckTypeSetElemAttrPair(resourceName, "sns_action.*.topic_arn", "aws_sns_topic.test", "arn"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAwsSesReceiptRuleImportStateIdFunc(resourceName), + }, + }, + }) +} + +func TestAccAWSSESReceiptRule_stopAction(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ses_receipt_rule.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAWSSES(t) + testAccPreCheckSESReceiptRule(t) + }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSESReceiptRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSESReceiptRuleStopActionConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsSESReceiptRuleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "stop_action.#", "1"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "stop_action.*", map[string]string{ + "scope": "RuleSet", + }), + resource.TestCheckTypeSetElemAttrPair(resourceName, "stop_action.*.topic_arn", "aws_sns_topic.test", "arn"), ), }, { @@ -214,26 +293,26 @@ func testAccCheckAwsSESReceiptRuleExists(n string) resource.TestCheckFunc { RuleSetName: aws.String(rs.Primary.Attributes["rule_set_name"]), } - response, err := conn.DescribeReceiptRule(params) + _, err := conn.DescribeReceiptRule(params) if err != nil { return err } - if !aws.BoolValue(response.Rule.Enabled) { - return fmt.Errorf("Enabled (%v) was not set to true", *response.Rule.Enabled) - } + // if !aws.BoolValue(response.Rule.Enabled) { + // return fmt.Errorf("Enabled (%v) was not set to true", *response.Rule.Enabled) + // } - if !reflect.DeepEqual(response.Rule.Recipients, []*string{aws.String("test@example.com")}) { - return fmt.Errorf("Recipients (%v) was not set to [test@example.com]", response.Rule.Recipients) - } + // if !reflect.DeepEqual(response.Rule.Recipients, []*string{aws.String("test@example.com")}) { + // return fmt.Errorf("Recipients (%v) was not set to [test@example.com]", response.Rule.Recipients) + // } - if !aws.BoolValue(response.Rule.ScanEnabled) { - return fmt.Errorf("ScanEnabled (%v) was not set to true", *response.Rule.ScanEnabled) - } + // if !aws.BoolValue(response.Rule.ScanEnabled) { + // return fmt.Errorf("ScanEnabled (%v) was not set to true", *response.Rule.ScanEnabled) + // } - if aws.StringValue(response.Rule.TlsPolicy) != ses.TlsPolicyRequire { - return fmt.Errorf("TLS Policy (%s) was not set to Require", *response.Rule.TlsPolicy) - } + // if aws.StringValue(response.Rule.TlsPolicy) != ses.TlsPolicyRequire { + // return fmt.Errorf("TLS Policy (%s) was not set to Require", *response.Rule.TlsPolicy) + // } return nil } @@ -407,6 +486,59 @@ resource "aws_ses_receipt_rule" "test" { `, rName) } +func testAccAWSSESReceiptRuleSNSActionConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_ses_receipt_rule_set" "test" { + rule_set_name = %[1]q +} + +resource "aws_sns_topic" "test" { + name = %[1]q +} + +resource "aws_ses_receipt_rule" "test" { + name = %[1]q + rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name + recipients = ["test@example.com"] + enabled = true + scan_enabled = true + tls_policy = "Require" + + sns_action { + topic_arn = aws_sns_topic.test.arn + position = 1 + } +} +`, rName) +} + +func testAccAWSSESReceiptRuleStopActionConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_ses_receipt_rule_set" "test" { + rule_set_name = %[1]q +} + +resource "aws_sns_topic" "test" { + name = %[1]q +} + +resource "aws_ses_receipt_rule" "test" { + name = %[1]q + rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name + recipients = ["test@example.com"] + enabled = true + scan_enabled = true + tls_policy = "Require" + + stop_action { + topic_arn = aws_sns_topic.test.arn + scope = "RuleSet" + position = 1 + } +} +`, rName) +} + func testAccAWSSESReceiptRuleOrderConfig(rName string) string { return fmt.Sprintf(` resource "aws_ses_receipt_rule_set" "test" { From b836305bc94b4b29fabb1048f734ec3864cd5a08 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sun, 14 Feb 2021 18:44:26 +0200 Subject: [PATCH 02/10] tests --- aws/resource_aws_ses_receipt_rule_test.go | 151 ++++++---------------- 1 file changed, 37 insertions(+), 114 deletions(-) diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index f83a6f06229..5050d1e92f3 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -13,6 +13,8 @@ import ( ) func TestAccAWSSESReceiptRule_basic(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -29,7 +31,7 @@ func TestAccAWSSESReceiptRule_basic(t *testing.T) { { Config: testAccAWSSESReceiptRuleBasicConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "rule_set_name", rName), testAccCheckResourceAttrRegionalARN(resourceName, "arn", "ses", fmt.Sprintf("receipt-rule-set/%s:receipt-rule/%s", rName, rName)), @@ -57,6 +59,8 @@ func TestAccAWSSESReceiptRule_basic(t *testing.T) { } func TestAccAWSSESReceiptRule_s3Action(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -73,7 +77,7 @@ func TestAccAWSSESReceiptRule_s3Action(t *testing.T) { { Config: testAccAWSSESReceiptRuleS3ActionConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "s3_action.#", "1"), resource.TestCheckTypeSetElemAttrPair(resourceName, "s3_action.*.bucket_name", "aws_s3_bucket.test", "id"), @@ -89,6 +93,8 @@ func TestAccAWSSESReceiptRule_s3Action(t *testing.T) { } func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -104,7 +110,7 @@ func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { { Config: testAccAWSSESReceiptRuleSNSActionConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), resource.TestCheckResourceAttr(resourceName, "sns_action.#", "1"), resource.TestCheckTypeSetElemAttrPair(resourceName, "sns_action.*.topic_arn", "aws_sns_topic.test", "arn"), ), @@ -119,6 +125,8 @@ func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { } func TestAccAWSSESReceiptRule_stopAction(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -134,7 +142,7 @@ func TestAccAWSSESReceiptRule_stopAction(t *testing.T) { { Config: testAccAWSSESReceiptRuleStopActionConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), resource.TestCheckResourceAttr(resourceName, "stop_action.#", "1"), resource.TestCheckTypeSetElemNestedAttrs(resourceName, "stop_action.*", map[string]string{ "scope": "RuleSet", @@ -152,6 +160,8 @@ func TestAccAWSSESReceiptRule_stopAction(t *testing.T) { } func TestAccAWSSESReceiptRule_order(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -168,7 +178,9 @@ func TestAccAWSSESReceiptRule_order(t *testing.T) { { Config: testAccAWSSESReceiptRuleOrderConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleOrder(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), + resource.TestCheckResourceAttr(resourceName, "name", "second"), + resource.TestCheckResourceAttrPair(resourceName, "after", "aws_ses_receipt_rule.test1", "name"), ), }, { @@ -181,6 +193,8 @@ func TestAccAWSSESReceiptRule_order(t *testing.T) { } func TestAccAWSSESReceiptRule_actions(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -197,7 +211,17 @@ func TestAccAWSSESReceiptRule_actions(t *testing.T) { { Config: testAccAWSSESReceiptRuleActionsConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleActions(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "add_header_action.*", map[string]string{ + "header_name": "Added-By", + "header_value": "Terraform", + "position": "2", + }), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "add_header_action.*", map[string]string{ + "header_name": "Another-Header", + "header_value": "First", + "position": "1", + }), ), }, { @@ -210,6 +234,8 @@ func TestAccAWSSESReceiptRule_actions(t *testing.T) { } func TestAccAWSSESReceiptRule_disappears(t *testing.T) { + var rule ses.ReceiptRule + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_ses_receipt_rule.test" @@ -228,7 +254,7 @@ func TestAccAWSSESReceiptRule_disappears(t *testing.T) { { Config: testAccAWSSESReceiptRuleBasicConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), testAccCheckResourceDisappears(testAccProvider, resourceAwsSesReceiptRuleSet(), ruleSetResourceName), ), ExpectNonEmptyPlan: true, @@ -236,7 +262,7 @@ func TestAccAWSSESReceiptRule_disappears(t *testing.T) { { Config: testAccAWSSESReceiptRuleBasicConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName), + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), testAccCheckResourceDisappears(testAccProvider, resourceAwsSesReceiptRule(), resourceName), ), ExpectNonEmptyPlan: true, @@ -275,7 +301,7 @@ func testAccCheckSESReceiptRuleDestroy(s *terraform.State) error { } -func testAccCheckAwsSESReceiptRuleExists(n string) resource.TestCheckFunc { +func testAccCheckAwsSESReceiptRuleExists(n string, rule *ses.ReceiptRule) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -293,26 +319,12 @@ func testAccCheckAwsSESReceiptRuleExists(n string) resource.TestCheckFunc { RuleSetName: aws.String(rs.Primary.Attributes["rule_set_name"]), } - _, err := conn.DescribeReceiptRule(params) + resp, err := conn.DescribeReceiptRule(params) if err != nil { return err } - // if !aws.BoolValue(response.Rule.Enabled) { - // return fmt.Errorf("Enabled (%v) was not set to true", *response.Rule.Enabled) - // } - - // if !reflect.DeepEqual(response.Rule.Recipients, []*string{aws.String("test@example.com")}) { - // return fmt.Errorf("Recipients (%v) was not set to [test@example.com]", response.Rule.Recipients) - // } - - // if !aws.BoolValue(response.Rule.ScanEnabled) { - // return fmt.Errorf("ScanEnabled (%v) was not set to true", *response.Rule.ScanEnabled) - // } - - // if aws.StringValue(response.Rule.TlsPolicy) != ses.TlsPolicyRequire { - // return fmt.Errorf("TLS Policy (%s) was not set to Require", *response.Rule.TlsPolicy) - // } + *rule = *resp.Rule return nil } @@ -329,95 +341,6 @@ func testAccAwsSesReceiptRuleImportStateIdFunc(resourceName string) resource.Imp } } -func testAccCheckAwsSESReceiptRuleOrder(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("SES Receipt Rule not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("SES Receipt Rule name not set") - } - - conn := testAccProvider.Meta().(*AWSClient).sesconn - - params := &ses.DescribeReceiptRuleSetInput{ - RuleSetName: aws.String(rs.Primary.Attributes["rule_set_name"]), - } - - response, err := conn.DescribeReceiptRuleSet(params) - if err != nil { - return err - } - - if len(response.Rules) != 2 { - return fmt.Errorf("Number of rules (%d) was not equal to 2", len(response.Rules)) - } else if aws.StringValue(response.Rules[0].Name) != "first" || - aws.StringValue(response.Rules[1].Name) != "second" { - return fmt.Errorf("Order of rules (%v) was incorrect", response.Rules) - } - - return nil - } -} - -func testAccCheckAwsSESReceiptRuleActions(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("SES Receipt Rule not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("SES Receipt Rule name not set") - } - - conn := testAccProvider.Meta().(*AWSClient).sesconn - - params := &ses.DescribeReceiptRuleInput{ - RuleName: aws.String(rs.Primary.Attributes["name"]), - RuleSetName: aws.String(rs.Primary.Attributes["rule_set_name"]), - } - - response, err := conn.DescribeReceiptRule(params) - if err != nil { - return err - } - - actions := response.Rule.Actions - - if len(actions) != 3 { - return fmt.Errorf("Number of rules (%d) was not equal to 3", len(actions)) - } - - addHeaderAction := actions[0].AddHeaderAction - if aws.StringValue(addHeaderAction.HeaderName) != "Another-Header" { - return fmt.Errorf("Header Name (%s) was not equal to Another-Header", *addHeaderAction.HeaderName) - } - - if aws.StringValue(addHeaderAction.HeaderValue) != "First" { - return fmt.Errorf("Header Value (%s) was not equal to First", *addHeaderAction.HeaderValue) - } - - secondAddHeaderAction := actions[1].AddHeaderAction - if aws.StringValue(secondAddHeaderAction.HeaderName) != "Added-By" { - return fmt.Errorf("Header Name (%s) was not equal to Added-By", *secondAddHeaderAction.HeaderName) - } - - if aws.StringValue(secondAddHeaderAction.HeaderValue) != "Terraform" { - return fmt.Errorf("Header Value (%s) was not equal to Terraform", *secondAddHeaderAction.HeaderValue) - } - - stopAction := actions[2].StopAction - if aws.StringValue(stopAction.Scope) != ses.StopScopeRuleSet { - return fmt.Errorf("Scope (%s) was not equal to RuleSet", *stopAction.Scope) - } - - return nil - } -} - func testAccPreCheckSESReceiptRule(t *testing.T) { conn := testAccProvider.Meta().(*AWSClient).sesconn From 41a3e5c7046ec05b0559a71b745eb55dea18af6b Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 17 Feb 2021 10:00:20 +0200 Subject: [PATCH 03/10] add sns encoding test + lambda test --- aws/resource_aws_ses_receipt_rule.go | 2 +- aws/resource_aws_ses_receipt_rule_test.go | 149 ++++++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_ses_receipt_rule.go b/aws/resource_aws_ses_receipt_rule.go index 7e04351f32a..5e0132a7cc8 100644 --- a/aws/resource_aws_ses_receipt_rule.go +++ b/aws/resource_aws_ses_receipt_rule.go @@ -188,7 +188,7 @@ func resourceAwsSesReceiptRule() *schema.Resource { "invocation_type": { Type: schema.TypeString, Optional: true, - Computed: true, + Default: ses.InvocationTypeEvent, ValidateFunc: validation.StringInSlice(ses.InvocationType_Values(), false), }, diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index 5050d1e92f3..e0b2a6077a6 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -113,6 +113,79 @@ func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), resource.TestCheckResourceAttr(resourceName, "sns_action.#", "1"), resource.TestCheckTypeSetElemAttrPair(resourceName, "sns_action.*.topic_arn", "aws_sns_topic.test", "arn"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sns_action.*", map[string]string{ + "encpding": "UTF-8", + }), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAwsSesReceiptRuleImportStateIdFunc(resourceName), + }, + }, + }) +} + +func TestAccAWSSESReceiptRule_snsActionEncoding(t *testing.T) { + var rule ses.ReceiptRule + + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ses_receipt_rule.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAWSSES(t) + testAccPreCheckSESReceiptRule(t) + }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSESReceiptRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSESReceiptRuleSNSActionEncodingConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), + resource.TestCheckResourceAttr(resourceName, "sns_action.#", "1"), + resource.TestCheckTypeSetElemAttrPair(resourceName, "sns_action.*.topic_arn", "aws_sns_topic.test", "arn"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sns_action.*", map[string]string{ + "encoding": "Base64", + }), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAwsSesReceiptRuleImportStateIdFunc(resourceName), + }, + }, + }) +} + +func TestAccAWSSESReceiptRule_lambdaAction(t *testing.T) { + var rule ses.ReceiptRule + + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ses_receipt_rule.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAWSSES(t) + testAccPreCheckSESReceiptRule(t) + }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSESReceiptRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSESReceiptRuleSNSActionConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), + resource.TestCheckResourceAttr(resourceName, "lambda_action.#", "1"), + resource.TestCheckTypeSetElemAttrPair(resourceName, "lambda_action.*.function_arn", "aws_lambda_function.test", "arn"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "lambda_action.*", map[string]string{ + "invocation_type": "Event", + }), ), }, { @@ -435,6 +508,82 @@ resource "aws_ses_receipt_rule" "test" { `, rName) } +func testAccAWSSESReceiptRuleSNSActionEncodingConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_ses_receipt_rule_set" "test" { + rule_set_name = %[1]q +} + +resource "aws_sns_topic" "test" { + name = %[1]q +} + +resource "aws_ses_receipt_rule" "test" { + name = %[1]q + rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name + recipients = ["test@example.com"] + enabled = true + scan_enabled = true + tls_policy = "Require" + + sns_action { + topic_arn = aws_sns_topic.test.arn + encoding = "Base64" + position = 1 + } +} +`, rName) +} + +func testAccAWSSESReceiptRuleLambdaActionConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_ses_receipt_rule_set" "test" { + rule_set_name = %[1]q +} + +resource "aws_iam_role" "test" { + name = %[1]q + + assume_role_policy = jsonencode({ + "Version" : "2012-10-17", + "Statement" : [ + { + "Action" : "sts:AssumeRole", + "Principal" : { + "Service" : "lambda.amazonaws.com" + }, + "Effect" : "Allow", + "Sid" : "" + } + ] + }) +} + +resource "aws_lambda_function" "test" { + filename = "test-fixtures/lambdatest.zip" + source_code_hash = filebase64sha256("test-fixtures/lambdatest.zip") + function_name = %[1]q + role = aws_iam_role.test.arn + handler = "exports.example" + runtime = "nodejs12.x" +} + +resource "aws_ses_receipt_rule" "test" { + name = %[1]q + rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name + recipients = ["test@example.com"] + enabled = true + scan_enabled = true + tls_policy = "Require" + + lambda_action { + function_arn = aws_lambda_function.test.arn + position = 1 + } +} +`, rName) +} + func testAccAWSSESReceiptRuleStopActionConfig(rName string) string { return fmt.Sprintf(` resource "aws_ses_receipt_rule_set" "test" { From 467332100260325387e9efd421b1421d5fccb39f Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 17 Feb 2021 10:24:18 +0200 Subject: [PATCH 04/10] changelog --- .changelog/17654.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/17654.txt diff --git a/.changelog/17654.txt b/.changelog/17654.txt new file mode 100644 index 00000000000..9d0d62ee841 --- /dev/null +++ b/.changelog/17654.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_ses_receipt_rule: Add `encoding` argument to `sns_action` configuration block. +``` From 7ab3bff0841beb9997826650ba75f02d42f6bafc Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 17 Feb 2021 10:31:44 +0200 Subject: [PATCH 05/10] bounce, lambda permission + fmt --- aws/resource_aws_ses_receipt_rule_test.go | 83 +++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index e0b2a6077a6..dfca5362899 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -81,6 +81,46 @@ func TestAccAWSSESReceiptRule_s3Action(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "s3_action.#", "1"), resource.TestCheckTypeSetElemAttrPair(resourceName, "s3_action.*.bucket_name", "aws_s3_bucket.test", "id"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "s3_action.*", map[string]string{ + "position": "1", + }), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAwsSesReceiptRuleImportStateIdFunc(resourceName), + }, + }, + }) +} + +func TestAccAWSSESReceiptRule_bounceAction(t *testing.T) { + var rule ses.ReceiptRule + + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ses_receipt_rule.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckAWSSES(t) + testAccPreCheckSESReceiptRule(t) + }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSESReceiptRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSESReceiptRuleBounceActionConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), + resource.TestCheckResourceAttr(resourceName, "bounce_action.#", "1"), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "bounce_action.*", map[string]string{ + "message": rName, + "sender": "test@example.com", + "smtp_reply_code": "2yz", + "position": "1", + }), ), }, { @@ -114,7 +154,8 @@ func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "sns_action.#", "1"), resource.TestCheckTypeSetElemAttrPair(resourceName, "sns_action.*.topic_arn", "aws_sns_topic.test", "arn"), resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sns_action.*", map[string]string{ - "encpding": "UTF-8", + "encoding": "UTF-8", + "position": "1", }), ), }, @@ -150,6 +191,7 @@ func TestAccAWSSESReceiptRule_snsActionEncoding(t *testing.T) { resource.TestCheckTypeSetElemAttrPair(resourceName, "sns_action.*.topic_arn", "aws_sns_topic.test", "arn"), resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sns_action.*", map[string]string{ "encoding": "Base64", + "position": "1", }), ), }, @@ -178,13 +220,14 @@ func TestAccAWSSESReceiptRule_lambdaAction(t *testing.T) { CheckDestroy: testAccCheckSESReceiptRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSSESReceiptRuleSNSActionConfig(rName), + Config: testAccAWSSESReceiptRuleLambdaActionConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), resource.TestCheckResourceAttr(resourceName, "lambda_action.#", "1"), resource.TestCheckTypeSetElemAttrPair(resourceName, "lambda_action.*.function_arn", "aws_lambda_function.test", "arn"), resource.TestCheckTypeSetElemNestedAttrs(resourceName, "lambda_action.*", map[string]string{ "invocation_type": "Event", + "position": "1", }), ), }, @@ -482,6 +525,30 @@ resource "aws_ses_receipt_rule" "test" { `, rName) } +func testAccAWSSESReceiptRuleBounceActionConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_ses_receipt_rule_set" "test" { + rule_set_name = %[1]q +} + +resource "aws_ses_receipt_rule" "test" { + name = %[1]q + rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name + recipients = ["test@example.com"] + enabled = true + scan_enabled = true + tls_policy = "Require" + + bounce_action { + message = %[1]q + sender = "test@example.com" + smtp_reply_code = "2yz" + position = 1 + } +} +`, rName) +} + func testAccAWSSESReceiptRuleSNSActionConfig(rName string) string { return fmt.Sprintf(` resource "aws_ses_receipt_rule_set" "test" { @@ -568,6 +635,12 @@ resource "aws_lambda_function" "test" { runtime = "nodejs12.x" } +resource "aws_lambda_permission" "test" { + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.test.arn + principal = "ses.amazonaws.com" +} + resource "aws_ses_receipt_rule" "test" { name = %[1]q rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name @@ -575,11 +648,13 @@ resource "aws_ses_receipt_rule" "test" { enabled = true scan_enabled = true tls_policy = "Require" - + lambda_action { - function_arn = aws_lambda_function.test.arn + function_arn = aws_lambda_function.test.arn position = 1 } + + depends_on = [aws_lambda_permission.test] } `, rName) } From 5a1a09e3a8dfefc0f750f7699388641a68fcdda8 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 17 Feb 2021 10:32:17 +0200 Subject: [PATCH 06/10] fmt --- aws/resource_aws_ses_receipt_rule_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index dfca5362899..94bc48618a8 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -651,7 +651,7 @@ resource "aws_ses_receipt_rule" "test" { lambda_action { function_arn = aws_lambda_function.test.arn - position = 1 + position = 1 } depends_on = [aws_lambda_permission.test] @@ -676,11 +676,11 @@ resource "aws_ses_receipt_rule" "test" { enabled = true scan_enabled = true tls_policy = "Require" - + stop_action { - topic_arn = aws_sns_topic.test.arn + topic_arn = aws_sns_topic.test.arn scope = "RuleSet" - position = 1 + position = 1 } } `, rName) From 491a7576c3830df265110854847e07e1ddb3d06d Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 17 Feb 2021 10:35:33 +0200 Subject: [PATCH 07/10] fmt --- aws/resource_aws_ses_receipt_rule_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index 94bc48618a8..e75573cf806 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -538,7 +538,7 @@ resource "aws_ses_receipt_rule" "test" { enabled = true scan_enabled = true tls_policy = "Require" - + bounce_action { message = %[1]q sender = "test@example.com" @@ -566,10 +566,10 @@ resource "aws_ses_receipt_rule" "test" { enabled = true scan_enabled = true tls_policy = "Require" - + sns_action { - topic_arn = aws_sns_topic.test.arn - position = 1 + topic_arn = aws_sns_topic.test.arn + position = 1 } } `, rName) @@ -592,11 +592,11 @@ resource "aws_ses_receipt_rule" "test" { enabled = true scan_enabled = true tls_policy = "Require" - + sns_action { - topic_arn = aws_sns_topic.test.arn + topic_arn = aws_sns_topic.test.arn encoding = "Base64" - position = 1 + position = 1 } } `, rName) From 0625a84589a30e293c4ac42e823483749ac41cca Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Wed, 17 Feb 2021 11:19:34 +0200 Subject: [PATCH 08/10] remove bounce --- aws/resource_aws_ses_receipt_rule_test.go | 61 ----------------------- 1 file changed, 61 deletions(-) diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index e75573cf806..e0254ecd912 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -95,43 +95,6 @@ func TestAccAWSSESReceiptRule_s3Action(t *testing.T) { }) } -func TestAccAWSSESReceiptRule_bounceAction(t *testing.T) { - var rule ses.ReceiptRule - - rName := acctest.RandomWithPrefix("tf-acc-test") - resourceName := "aws_ses_receipt_rule.test" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - testAccPreCheckAWSSES(t) - testAccPreCheckSESReceiptRule(t) - }, - Providers: testAccProviders, - CheckDestroy: testAccCheckSESReceiptRuleDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSSESReceiptRuleBounceActionConfig(rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAwsSESReceiptRuleExists(resourceName, &rule), - resource.TestCheckResourceAttr(resourceName, "bounce_action.#", "1"), - resource.TestCheckTypeSetElemNestedAttrs(resourceName, "bounce_action.*", map[string]string{ - "message": rName, - "sender": "test@example.com", - "smtp_reply_code": "2yz", - "position": "1", - }), - ), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateIdFunc: testAccAwsSesReceiptRuleImportStateIdFunc(resourceName), - }, - }, - }) -} - func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { var rule ses.ReceiptRule @@ -525,30 +488,6 @@ resource "aws_ses_receipt_rule" "test" { `, rName) } -func testAccAWSSESReceiptRuleBounceActionConfig(rName string) string { - return fmt.Sprintf(` -resource "aws_ses_receipt_rule_set" "test" { - rule_set_name = %[1]q -} - -resource "aws_ses_receipt_rule" "test" { - name = %[1]q - rule_set_name = aws_ses_receipt_rule_set.test.rule_set_name - recipients = ["test@example.com"] - enabled = true - scan_enabled = true - tls_policy = "Require" - - bounce_action { - message = %[1]q - sender = "test@example.com" - smtp_reply_code = "2yz" - position = 1 - } -} -`, rName) -} - func testAccAWSSESReceiptRuleSNSActionConfig(rName string) string { return fmt.Sprintf(` resource "aws_ses_receipt_rule_set" "test" { From abbd548347bd462ad5074df2d1a54262d668e569 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 11 Mar 2021 01:48:43 +0200 Subject: [PATCH 09/10] docs --- website/docs/r/ses_receipt_rule.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/ses_receipt_rule.html.markdown b/website/docs/r/ses_receipt_rule.html.markdown index 28c9158009e..f1a9b91b322 100644 --- a/website/docs/r/ses_receipt_rule.html.markdown +++ b/website/docs/r/ses_receipt_rule.html.markdown @@ -87,6 +87,7 @@ SNS actions support the following: * `topic_arn` - (Required) The ARN of an SNS topic to notify * `position` - (Required) The position of the action in the receipt rule +* `encoding` - (Optional) The encoding to use for the email within the Amazon SNS notification. Default value is `UTF-8`. Stop actions support the following: From 9a9b0e2a08264de4454fe28caeb13a7aaf9d6d4c Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 27 Mar 2021 19:22:52 +0300 Subject: [PATCH 10/10] error check --- aws/resource_aws_ses_receipt_rule_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aws/resource_aws_ses_receipt_rule_test.go b/aws/resource_aws_ses_receipt_rule_test.go index e0254ecd912..8abf3918537 100644 --- a/aws/resource_aws_ses_receipt_rule_test.go +++ b/aws/resource_aws_ses_receipt_rule_test.go @@ -107,6 +107,7 @@ func TestAccAWSSESReceiptRule_snsAction(t *testing.T) { testAccPreCheckAWSSES(t) testAccPreCheckSESReceiptRule(t) }, + ErrorCheck: testAccErrorCheck(t, ses.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckSESReceiptRuleDestroy, Steps: []resource.TestStep{ @@ -143,6 +144,7 @@ func TestAccAWSSESReceiptRule_snsActionEncoding(t *testing.T) { testAccPreCheckAWSSES(t) testAccPreCheckSESReceiptRule(t) }, + ErrorCheck: testAccErrorCheck(t, ses.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckSESReceiptRuleDestroy, Steps: []resource.TestStep{ @@ -179,6 +181,7 @@ func TestAccAWSSESReceiptRule_lambdaAction(t *testing.T) { testAccPreCheckAWSSES(t) testAccPreCheckSESReceiptRule(t) }, + ErrorCheck: testAccErrorCheck(t, ses.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckSESReceiptRuleDestroy, Steps: []resource.TestStep{ @@ -215,6 +218,7 @@ func TestAccAWSSESReceiptRule_stopAction(t *testing.T) { testAccPreCheckAWSSES(t) testAccPreCheckSESReceiptRule(t) }, + ErrorCheck: testAccErrorCheck(t, ses.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckSESReceiptRuleDestroy, Steps: []resource.TestStep{