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

r/cloudwatch_event_target - add validations + refactor tests #11685

Merged
merged 5 commits into from
Oct 30, 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
76 changes: 37 additions & 39 deletions aws/resource_aws_cloudwatch_event_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -44,8 +43,9 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
},

"arn": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},

"input": {
Expand All @@ -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": {
Expand Down Expand Up @@ -99,9 +100,10 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
ValidateFunc: validation.StringLenBetween(1, 255),
},
"launch_type": {
Type: schema.TypeString,
Optional: true,
Default: "EC2",
Type: schema.TypeString,
Optional: true,
Default: events.LaunchTypeEc2,
ValidateFunc: validation.StringInSlice(events.LaunchType_Values(), true),
},
"network_configuration": {
Type: schema.TypeList,
Expand All @@ -113,13 +115,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,
Expand All @@ -143,7 +143,7 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
"task_definition_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1600),
ValidateFunc: validateArn,
},
},
},
Expand Down Expand Up @@ -276,22 +276,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)
Expand Down Expand Up @@ -354,7 +352,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
}
}
Expand Down Expand Up @@ -582,7 +580,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)
Expand All @@ -593,17 +591,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
}
Expand All @@ -613,11 +611,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}
Expand All @@ -639,14 +637,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
}
Expand All @@ -655,9 +653,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}
Expand Down
Loading