Skip to content

Commit

Permalink
refactor error
Browse files Browse the repository at this point in the history
add plan time validation to `arn`, `role_arn`, `launch_type`, `task_definition_arn`
refactor tests
  • Loading branch information
DrFaust92 committed Jan 21, 2020
1 parent 96840ca commit 97b9f3f
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 144 deletions.
45 changes: 24 additions & 21 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/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/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 @@ -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,
Expand Down Expand Up @@ -143,7 +148,7 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
"task_definition_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1600),
ValidateFunc: validateArn,
},
},
},
Expand Down Expand Up @@ -275,22 +280,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
Loading

0 comments on commit 97b9f3f

Please sign in to comment.