ISSUE-5702: Making the CloudWatch Event Rule Target ID optional

This commit is contained in:
Andy Chan 2016-03-22 11:52:35 -07:00
parent 7080c876e2
commit 7dc5a272be
2 changed files with 57 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"regexp"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws"
@ -29,7 +30,7 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
"target_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validateCloudWatchEventTargetId,
},
@ -60,7 +61,13 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa
conn := meta.(*AWSClient).cloudwatcheventsconn
rule := d.Get("rule").(string)
targetId := d.Get("target_id").(string)
var targetId string
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)

View File

@ -42,6 +42,27 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) {
})
}
func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) {
var target events.Target
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCloudWatchEventTargetConfigMissingTargetId,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.moobar", &target),
resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "rule", "tf-acc-cw-event-rule-basic"),
resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn", regexp.MustCompile(":tf-acc-moon$")),
testAccCheckTargetIdExists("aws_cloudwatch_event_target.target_id"),
),
},
},
})
}
func TestAccAWSCloudWatchEventTarget_full(t *testing.T) {
var target events.Target
@ -105,6 +126,17 @@ func testAccCheckAWSCloudWatchEventTargetDestroy(s *terraform.State) error {
return nil
}
func testAccCheckTargetIdExists(targetId string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[targetId]
if !ok {
return fmt.Errorf("Not found: %s", targetId)
}
return nil
}
}
var testAccAWSCloudWatchEventTargetConfig = `
resource "aws_cloudwatch_event_rule" "foo" {
name = "tf-acc-cw-event-rule-basic"
@ -122,6 +154,22 @@ resource "aws_sns_topic" "moon" {
}
`
var testAccAWSCloudWatchEventTargetConfigMissingTargetId = `
resource "aws_cloudwatch_event_rule" "foo" {
name = "tf-acc-cw-event-rule-basic"
schedule_expression = "rate(1 hour)"
}
resource "aws_cloudwatch_event_target" "moobar" {
rule = "${aws_cloudwatch_event_rule.foo.name}"
arn = "${aws_sns_topic.moon.arn}"
}
resource "aws_sns_topic" "moon" {
name = "tf-acc-moon"
}
`
var testAccAWSCloudWatchEventTargetConfigModified = `
resource "aws_cloudwatch_event_rule" "foo" {
name = "tf-acc-cw-event-rule-basic"