From e97600a6578257acb4cfb67ade9f123a7bb80891 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 21 Jan 2020 11:19:50 +0200 Subject: [PATCH 1/5] refactor error add plan time validation to `arn`, `role_arn`, `launch_type`, `task_definition_arn` refactor tests --- aws/resource_aws_cloudwatch_event_target.go | 45 ++--- ...source_aws_cloudwatch_event_target_test.go | 175 +++++++++--------- 2 files changed, 115 insertions(+), 105 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index be45a0fd083..20d3323f2f5 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" events "github.com/aws/aws-sdk-go/service/cloudwatchevents" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) @@ -44,8 +43,9 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { }, "arn": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + ValidateFunc: validateArn, }, "input": { @@ -63,8 +63,9 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { }, "role_arn": { - Type: schema.TypeString, - Optional: true, + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateArn, }, "run_command_targets": { @@ -101,7 +102,11 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { "launch_type": { Type: schema.TypeString, Optional: true, - Default: "EC2", + Default: events.LaunchTypeEc2, + ValidateFunc: validation.StringInSlice([]string{ + events.LaunchTypeEc2, + events.LaunchTypeFargate, + }, true), }, "network_configuration": { Type: schema.TypeList, @@ -143,7 +148,7 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { "task_definition_arn": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringLenBetween(1, 1600), + ValidateFunc: validateArn, }, }, }, @@ -276,22 +281,20 @@ func resourceAwsCloudWatchEventTargetRead(d *schema.ResourceData, meta interface d.SetId("") return nil } - if awsErr, ok := err.(awserr.Error); ok { - // This should never happen, but it's useful - // for recovering from https://github.com/hashicorp/terraform/issues/5389 - if awsErr.Code() == "ValidationException" { - log.Printf("[WARN] Removing CloudWatch Event Target %q because it never existed.", d.Id()) - d.SetId("") - return nil - } - - if awsErr.Code() == "ResourceNotFoundException" { - log.Printf("[WARN] CloudWatch Event Target (%q) not found. Removing it from state.", d.Id()) - d.SetId("") - return nil - } + // This should never happen, but it's useful + // for recovering from https://github.com/hashicorp/terraform/issues/5389 + if isAWSErr(err, "ValidationException", "") { + log.Printf("[WARN] Removing CloudWatch Event Target %q because it never existed.", d.Id()) + d.SetId("") + return nil + } + if isAWSErr(err, events.ErrCodeResourceNotFoundException, "") { + log.Printf("[WARN] CloudWatch Event Target (%q) not found. Removing it from state.", d.Id()) + d.SetId("") + return nil } + return err } log.Printf("[DEBUG] Found Event Target: %s", t) diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 067cec25dc9..67ce89b853c 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -92,13 +92,12 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target - rName1 := acctest.RandString(5) - rName2 := acctest.RandString(5) - ruleName := fmt.Sprintf("tf-acc-cw-event-rule-basic-%s", rName1) - snsTopicName1 := fmt.Sprintf("tf-acc-%s", rName1) - snsTopicName2 := fmt.Sprintf("tf-acc-%s", rName2) - targetID1 := fmt.Sprintf("tf-acc-cw-target-%s", rName1) - targetID2 := fmt.Sprintf("tf-acc-cw-target-%s", rName2) + resourceName := "aws_cloudwatch_event_target.test" + ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-basic") + snsTopicName1 := acctest.RandomWithPrefix("tf-acc") + snsTopicName2 := acctest.RandomWithPrefix("tf-acc") + targetID1 := acctest.RandomWithPrefix("tf-acc-cw-target") + targetID2 := acctest.RandomWithPrefix("tf-acc-cw-target") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -112,6 +111,7 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "rule", ruleName), resource.TestCheckResourceAttr(resourceName, "target_id", targetID1), resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_sns_topic.test", "arn"), + ), }, { @@ -137,9 +137,9 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target - rName := acctest.RandString(5) - ruleName := fmt.Sprintf("tf-acc-cw-event-rule-missing-target-id-%s", rName) - snsTopicName := fmt.Sprintf("tf-acc-%s", rName) + resourceName := "aws_cloudwatch_event_target.test" + ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-missing-target-id") + snsTopicName := acctest.RandomWithPrefix("tf-acc") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -167,10 +167,10 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target - rName := acctest.RandString(5) - ruleName := fmt.Sprintf("tf-acc-cw-event-rule-full-%s", rName) + resourceName := "aws_cloudwatch_event_target.test" + ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-full") ssmDocumentName := acctest.RandomWithPrefix("tf_ssm_Document") - targetID := fmt.Sprintf("tf-acc-cw-target-full-%s", rName) + targetID := acctest.RandomWithPrefix("tf-acc-cw-target-full") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -200,6 +200,7 @@ func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { func TestAccAWSCloudWatchEventTarget_ssmDocument(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_ssm_Document") resource.ParallelTest(t, resource.TestCase{ @@ -210,13 +211,13 @@ func TestAccAWSCloudWatchEventTarget_ssmDocument(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigSsmDocument(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -225,6 +226,7 @@ func TestAccAWSCloudWatchEventTarget_ssmDocument(t *testing.T) { func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_ecs_target") resource.ParallelTest(t, resource.TestCase{ @@ -235,13 +237,13 @@ func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigEcs(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -250,6 +252,7 @@ func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { func TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_ecs_target") resource.ParallelTest(t, resource.TestCase{ @@ -260,14 +263,14 @@ func TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigEcsWithBlankTaskCount(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), - resource.TestCheckResourceAttr("aws_cloudwatch_event_target.test", "ecs_target.0.task_count", "1"), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.task_count", "1"), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -276,6 +279,7 @@ func TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount(t *testing.T) { func TestAccAWSCloudWatchEventTarget_batch(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_batch_target") resource.ParallelTest(t, resource.TestCase{ @@ -286,13 +290,13 @@ func TestAccAWSCloudWatchEventTarget_batch(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigBatch(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -301,6 +305,7 @@ func TestAccAWSCloudWatchEventTarget_batch(t *testing.T) { func TestAccAWSCloudWatchEventTarget_kinesis(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_kinesis_target") resource.ParallelTest(t, resource.TestCase{ @@ -311,13 +316,13 @@ func TestAccAWSCloudWatchEventTarget_kinesis(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigKinesis(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -326,6 +331,7 @@ func TestAccAWSCloudWatchEventTarget_kinesis(t *testing.T) { func TestAccAWSCloudWatchEventTarget_sqs(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_sqs_target") resource.ParallelTest(t, resource.TestCase{ @@ -336,13 +342,13 @@ func TestAccAWSCloudWatchEventTarget_sqs(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigSqs(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -351,6 +357,7 @@ func TestAccAWSCloudWatchEventTarget_sqs(t *testing.T) { func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) { var target events.Target + resourceName := "aws_cloudwatch_event_target.test" rName := acctest.RandomWithPrefix("tf_input_transformer") resource.ParallelTest(t, resource.TestCase{ @@ -361,13 +368,13 @@ func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) { { Config: testAccAWSCloudWatchEventTargetConfigInputTransformer(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + testAccCheckCloudWatchEventTargetExists(resourceName, &target), ), }, { - ResourceName: "aws_cloudwatch_event_target.test", + ResourceName: resourceName, ImportState: true, - ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.test"), + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), ImportStateVerify: true, }, }, @@ -466,10 +473,10 @@ func testAccAWSCloudWatchEventTargetConfig_full(ruleName, targetName, rName stri resource "aws_cloudwatch_event_rule" "test" { name = "%s" schedule_expression = "rate(1 hour)" - role_arn = aws_iam_role.role.arn + role_arn = aws_iam_role.test.arn } -resource "aws_iam_role" "role" { +resource "aws_iam_role" "test" { name = "%s" assume_role_policy = < Date: Tue, 21 Jan 2020 11:53:17 +0200 Subject: [PATCH 2/5] refactor tests --- aws/resource_aws_cloudwatch_event_target_test.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 67ce89b853c..4d533dcd4e9 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -93,9 +93,10 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { var target events.Target resourceName := "aws_cloudwatch_event_target.test" + topicResourceName := "aws_sns_topic.test" ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-basic") - snsTopicName1 := acctest.RandomWithPrefix("tf-acc") - snsTopicName2 := acctest.RandomWithPrefix("tf-acc") + snsTopicName1 := acctest.RandomWithPrefix("tf-acc-topic") + snsTopicName2 := acctest.RandomWithPrefix("tf-acc-topic-second") targetID1 := acctest.RandomWithPrefix("tf-acc-cw-target") targetID2 := acctest.RandomWithPrefix("tf-acc-cw-target") @@ -110,8 +111,7 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { testAccCheckCloudWatchEventTargetExists(resourceName, &target), resource.TestCheckResourceAttr(resourceName, "rule", ruleName), resource.TestCheckResourceAttr(resourceName, "target_id", targetID1), - resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_sns_topic.test", "arn"), - + resource.TestCheckResourceAttrPair(resourceName, "arn", topicResourceName, "arn"), ), }, { @@ -120,7 +120,7 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { testAccCheckCloudWatchEventTargetExists(resourceName, &target), resource.TestCheckResourceAttr(resourceName, "rule", ruleName), resource.TestCheckResourceAttr(resourceName, "target_id", targetID2), - resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_sns_topic.test", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "arn", topicResourceName, "arn"), ), }, { @@ -138,6 +138,7 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { var target events.Target resourceName := "aws_cloudwatch_event_target.test" + topicResourceName := "aws_sns_topic.test" ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-missing-target-id") snsTopicName := acctest.RandomWithPrefix("tf-acc") @@ -151,7 +152,7 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckCloudWatchEventTargetExists(resourceName, &target), resource.TestCheckResourceAttr(resourceName, "rule", ruleName), - resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_sns_topic.test", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "arn", topicResourceName, "arn"), ), }, { @@ -168,6 +169,7 @@ func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target resourceName := "aws_cloudwatch_event_target.test" + streamResourceName := "aws_kinesis_stream.test" ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-full") ssmDocumentName := acctest.RandomWithPrefix("tf_ssm_Document") targetID := acctest.RandomWithPrefix("tf-acc-cw-target-full") @@ -183,7 +185,7 @@ func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { testAccCheckCloudWatchEventTargetExists(resourceName, &target), resource.TestCheckResourceAttr(resourceName, "rule", ruleName), resource.TestCheckResourceAttr(resourceName, "target_id", targetID), - resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_kinesis_stream.test", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "arn", streamResourceName, "arn"), resource.TestCheckResourceAttr(resourceName, "input", "{ \"source\": [\"aws.cloudtrail\"] }\n"), resource.TestCheckResourceAttr(resourceName, "input_path", ""), ), From 34a519f9bc7bb25d11611e3e0ea92413f41c5509 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 22 May 2020 18:37:34 +0300 Subject: [PATCH 3/5] fix --- aws/resource_aws_cloudwatch_event_target_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 4d533dcd4e9..15cbbc6f7fe 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -92,7 +92,6 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target - resourceName := "aws_cloudwatch_event_target.test" topicResourceName := "aws_sns_topic.test" ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-basic") snsTopicName1 := acctest.RandomWithPrefix("tf-acc-topic") @@ -137,7 +136,6 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target - resourceName := "aws_cloudwatch_event_target.test" topicResourceName := "aws_sns_topic.test" ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-missing-target-id") snsTopicName := acctest.RandomWithPrefix("tf-acc") @@ -168,7 +166,6 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var target events.Target - resourceName := "aws_cloudwatch_event_target.test" streamResourceName := "aws_kinesis_stream.test" ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-full") ssmDocumentName := acctest.RandomWithPrefix("tf_ssm_Document") From fce5f99a39df2acd0d02bee1632834fafa8066c2 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sun, 24 May 2020 11:43:33 +0300 Subject: [PATCH 4/5] disappears test use aws value funcs --- aws/resource_aws_cloudwatch_event_target.go | 30 +++++++++---------- ...source_aws_cloudwatch_event_target_test.go | 27 ++++++++++++++++- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index 20d3323f2f5..55e13a78d00 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -118,13 +118,11 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { Type: schema.TypeSet, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, "subnets": { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, "assign_public_ip": { Type: schema.TypeBool, @@ -357,7 +355,7 @@ func findEventTargetById(id, rule string, nextToken *string, conn *events.CloudW } for _, t := range out.Targets { - if *t.Id == id { + if aws.StringValue(t.Id) == id { return t, nil } } @@ -585,7 +583,7 @@ func flattenAwsCloudWatchEventTargetRunParameters(runCommand *events.RunCommandP for _, x := range runCommand.RunCommandTargets { config := make(map[string]interface{}) - config["key"] = *x.Key + config["key"] = aws.StringValue(x.Key) config["values"] = flattenStringList(x.Values) result = append(result, config) @@ -596,17 +594,17 @@ func flattenAwsCloudWatchEventTargetRunParameters(runCommand *events.RunCommandP func flattenAwsCloudWatchEventTargetEcsParameters(ecsParameters *events.EcsParameters) []map[string]interface{} { config := make(map[string]interface{}) if ecsParameters.Group != nil { - config["group"] = *ecsParameters.Group + config["group"] = aws.StringValue(ecsParameters.Group) } if ecsParameters.LaunchType != nil { - config["launch_type"] = *ecsParameters.LaunchType + config["launch_type"] = aws.StringValue(ecsParameters.LaunchType) } config["network_configuration"] = flattenAwsCloudWatchEventTargetEcsParametersNetworkConfiguration(ecsParameters.NetworkConfiguration) if ecsParameters.PlatformVersion != nil { - config["platform_version"] = *ecsParameters.PlatformVersion + config["platform_version"] = aws.StringValue(ecsParameters.PlatformVersion) } - config["task_count"] = *ecsParameters.TaskCount - config["task_definition_arn"] = *ecsParameters.TaskDefinitionArn + config["task_count"] = aws.Int64Value(ecsParameters.TaskCount) + config["task_definition_arn"] = aws.StringValue(ecsParameters.TaskDefinitionArn) result := []map[string]interface{}{config} return result } @@ -616,11 +614,11 @@ func flattenAwsCloudWatchEventTargetEcsParametersNetworkConfiguration(nc *events } result := make(map[string]interface{}) - result["security_groups"] = schema.NewSet(schema.HashString, flattenStringList(nc.AwsvpcConfiguration.SecurityGroups)) - result["subnets"] = schema.NewSet(schema.HashString, flattenStringList(nc.AwsvpcConfiguration.Subnets)) + result["security_groups"] = flattenStringSet(nc.AwsvpcConfiguration.SecurityGroups) + result["subnets"] = flattenStringSet(nc.AwsvpcConfiguration.Subnets) if nc.AwsvpcConfiguration.AssignPublicIp != nil { - result["assign_public_ip"] = *nc.AwsvpcConfiguration.AssignPublicIp == events.AssignPublicIpEnabled + result["assign_public_ip"] = aws.StringValue(nc.AwsvpcConfiguration.AssignPublicIp) == events.AssignPublicIpEnabled } return []interface{}{result} @@ -642,14 +640,14 @@ func flattenAwsCloudWatchEventTargetBatchParameters(batchParameters *events.Batc func flattenAwsCloudWatchEventTargetKinesisParameters(kinesisParameters *events.KinesisParameters) []map[string]interface{} { config := make(map[string]interface{}) - config["partition_key_path"] = *kinesisParameters.PartitionKeyPath + config["partition_key_path"] = aws.StringValue(kinesisParameters.PartitionKeyPath) result := []map[string]interface{}{config} return result } func flattenAwsCloudWatchEventTargetSqsParameters(sqsParameters *events.SqsParameters) []map[string]interface{} { config := make(map[string]interface{}) - config["message_group_id"] = *sqsParameters.MessageGroupId + config["message_group_id"] = aws.StringValue(sqsParameters.MessageGroupId) result := []map[string]interface{}{config} return result } @@ -658,9 +656,9 @@ func flattenAwsCloudWatchInputTransformer(inputTransformer *events.InputTransfor config := make(map[string]interface{}) inputPathsMap := make(map[string]string) for k, v := range inputTransformer.InputPathsMap { - inputPathsMap[k] = *v + inputPathsMap[k] = aws.StringValue(v) } - config["input_template"] = *inputTransformer.InputTemplate + config["input_template"] = aws.StringValue(inputTransformer.InputTemplate) config["input_paths"] = inputPathsMap result := []map[string]interface{}{config} diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 15cbbc6f7fe..3fa2ba9aafe 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -97,7 +97,7 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { snsTopicName1 := acctest.RandomWithPrefix("tf-acc-topic") snsTopicName2 := acctest.RandomWithPrefix("tf-acc-topic-second") targetID1 := acctest.RandomWithPrefix("tf-acc-cw-target") - targetID2 := acctest.RandomWithPrefix("tf-acc-cw-target") + targetID2 := acctest.RandomWithPrefix("tf-acc-cw-target-second") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -380,6 +380,31 @@ func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) { }) } +func TestAccAWSCloudWatchEventTarget_disappears(t *testing.T) { + resourceName := "aws_cloudwatch_event_target.test" + + var target events.Target + ruleName := acctest.RandomWithPrefix("tf-acc-cw-event-rule-basic") + snsTopicName1 := acctest.RandomWithPrefix("tf-acc-topic") + targetID1 := acctest.RandomWithPrefix("tf-acc-cw-target") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchEventTargetConfig(ruleName, snsTopicName1, targetID1), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists(resourceName, &target), + testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudWatchEventTarget(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckCloudWatchEventTargetExists(n string, rule *events.Target) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] From e9cebc6f615baf1823da0cea136d7493b753b138 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 4 Sep 2020 14:11:21 +0300 Subject: [PATCH 5/5] add validations --- aws/resource_aws_cloudwatch_event_target.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index 55e13a78d00..4cf192c740d 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -100,13 +100,10 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { ValidateFunc: validation.StringLenBetween(1, 255), }, "launch_type": { - Type: schema.TypeString, - Optional: true, - Default: events.LaunchTypeEc2, - ValidateFunc: validation.StringInSlice([]string{ - events.LaunchTypeEc2, - events.LaunchTypeFargate, - }, true), + Type: schema.TypeString, + Optional: true, + Default: events.LaunchTypeEc2, + ValidateFunc: validation.StringInSlice(events.LaunchType_Values(), true), }, "network_configuration": { Type: schema.TypeList,