ISSUE-5702: Fixed a bug on target_id generation and related test case

This commit is contained in:
Andy Chan 2016-03-22 15:11:31 -07:00
parent 92590d1ac8
commit 0eee165a2b
2 changed files with 23 additions and 19 deletions

View File

@ -32,7 +32,6 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
ValidateFunc: validateCloudWatchEventTargetId,
}, },
"arn": &schema.Schema{ "arn": &schema.Schema{
@ -62,14 +61,7 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa
rule := d.Get("rule").(string) rule := d.Get("rule").(string)
var targetId string input, targetId := buildPutTargetInputStruct(d)
if v, ok := d.GetOk("target_id"); ok {
targetId = v.(string)
} else {
targetId = resource.UniqueId()
}
input := buildPutTargetInputStruct(d)
log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input) log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input)
out, err := conn.PutTargets(input) out, err := conn.PutTargets(input)
if err != nil { if err != nil {
@ -152,7 +144,7 @@ func findEventTargetById(id, rule string, nextToken *string, conn *events.CloudW
func resourceAwsCloudWatchEventTargetUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsCloudWatchEventTargetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn conn := meta.(*AWSClient).cloudwatcheventsconn
input := buildPutTargetInputStruct(d) input, _ := buildPutTargetInputStruct(d)
log.Printf("[DEBUG] Updating CloudWatch Event Target: %s", input) log.Printf("[DEBUG] Updating CloudWatch Event Target: %s", input)
_, err := conn.PutTargets(input) _, err := conn.PutTargets(input)
if err != nil { if err != nil {
@ -181,10 +173,17 @@ func resourceAwsCloudWatchEventTargetDelete(d *schema.ResourceData, meta interfa
return nil return nil
} }
func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput { func buildPutTargetInputStruct(d *schema.ResourceData) (*events.PutTargetsInput, string) {
var targetId string
if v, ok := d.GetOk("target_id"); ok {
targetId = v.(string)
} else {
targetId = resource.UniqueId()
}
e := &events.Target{ e := &events.Target{
Arn: aws.String(d.Get("arn").(string)), Arn: aws.String(d.Get("arn").(string)),
Id: aws.String(d.Get("target_id").(string)), Id: aws.String(targetId),
} }
if v, ok := d.GetOk("input"); ok { if v, ok := d.GetOk("input"); ok {
@ -199,5 +198,5 @@ func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput {
Targets: []*events.Target{e}, Targets: []*events.Target{e},
} }
return &input return &input, targetId
} }

View File

@ -43,6 +43,8 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) {
} }
func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) {
var target events.Target
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
@ -51,7 +53,10 @@ func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccAWSCloudWatchEventTargetConfigMissingTargetId, Config: testAccAWSCloudWatchEventTargetConfigMissingTargetId,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckTargetIdExists("aws_cloudwatch_event_target.target_id"), testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.moobar", &target),
resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "rule", "tf-acc-cw-event-rule-missing-target-id"),
resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn",
regexp.MustCompile(":tf-acc-moon$")),
), ),
}, },
}, },
@ -151,7 +156,7 @@ resource "aws_sns_topic" "moon" {
var testAccAWSCloudWatchEventTargetConfigMissingTargetId = ` var testAccAWSCloudWatchEventTargetConfigMissingTargetId = `
resource "aws_cloudwatch_event_rule" "foo" { resource "aws_cloudwatch_event_rule" "foo" {
name = "tf-acc-cw-event-rule-basic" name = "tf-acc-cw-event-rule-missing-target-id"
schedule_expression = "rate(1 hour)" schedule_expression = "rate(1 hour)"
} }
@ -161,7 +166,7 @@ resource "aws_cloudwatch_event_target" "moobar" {
} }
resource "aws_sns_topic" "moon" { resource "aws_sns_topic" "moon" {
name = "tf-acc-moon" name = "tf-acc-moon-1"
} }
` `