Merge pull request #5787 from iceycake/ISSUE-5702

ISSUE-5702: Making the Cloudwatch Event Rule Target target_id optional
This commit is contained in:
Paul Stack 2016-03-30 19:17:32 -05:00
commit 3dbedc6407
3 changed files with 60 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"regexp" "regexp"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -29,7 +30,8 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
"target_id": &schema.Schema{ "target_id": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
Computed: true,
ForceNew: true, ForceNew: true,
ValidateFunc: validateCloudWatchEventTargetId, ValidateFunc: validateCloudWatchEventTargetId,
}, },
@ -60,7 +62,14 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa
conn := meta.(*AWSClient).cloudwatcheventsconn conn := meta.(*AWSClient).cloudwatcheventsconn
rule := d.Get("rule").(string) 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()
d.Set("target_id", targetId)
}
input := buildPutTargetInputStruct(d) input := buildPutTargetInputStruct(d)
log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input) 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-missing-target-id"),
resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn",
regexp.MustCompile(":tf-acc-moon$")),
),
},
},
})
}
func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { func TestAccAWSCloudWatchEventTarget_full(t *testing.T) {
var target events.Target var target events.Target
@ -105,6 +126,17 @@ func testAccCheckAWSCloudWatchEventTargetDestroy(s *terraform.State) error {
return nil 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 = ` var testAccAWSCloudWatchEventTargetConfig = `
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-basic"
@ -122,6 +154,22 @@ resource "aws_sns_topic" "moon" {
} }
` `
var testAccAWSCloudWatchEventTargetConfigMissingTargetId = `
resource "aws_cloudwatch_event_rule" "foo" {
name = "tf-acc-cw-event-rule-missing-target-id"
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 = ` var testAccAWSCloudWatchEventTargetConfigModified = `
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-basic"

View File

@ -50,7 +50,7 @@ resource "aws_kinesis_stream" "test_stream" {
The following arguments are supported: The following arguments are supported:
* `rule` - (Required) The name of the rule you want to add targets to. * `rule` - (Required) The name of the rule you want to add targets to.
* `target_id` - (Required) The unique target assignment ID. * `target_id` - (Optional) The unique target assignment ID. If missing, will generate a random, unique id.
* `arn` - (Required) The Amazon Resource Name (ARN) associated of the target. * `arn` - (Required) The Amazon Resource Name (ARN) associated of the target.
* `input` - (Optional) Valid JSON text passed to the target. * `input` - (Optional) Valid JSON text passed to the target.
* `input_path` - (Optional) The value of the [JSONPath](http://goessner.net/articles/JsonPath/) * `input_path` - (Optional) The value of the [JSONPath](http://goessner.net/articles/JsonPath/)