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

ISSUE-5702: Making the Cloudwatch Event Rule Target target_id optional #5787

Merged
merged 6 commits into from
Mar 31, 2016
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
13 changes: 11 additions & 2 deletions builtin/providers/aws/resource_aws_cloudwatch_event_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +30,8 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {

"target_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
Computed: true,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @iceycake do you still want to force a new resource if the target_id changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be a new resource if the target_id changed (in order to guarantee the uniqueness of the rule target)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

ValidateFunc: validateCloudWatchEventTargetId,
},
Expand Down Expand Up @@ -60,7 +62,14 @@ 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()
d.Set("target_id", targetId)
}

input := buildPutTargetInputStruct(d)
log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input)
Expand Down
48 changes: 48 additions & 0 deletions builtin/providers/aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
var target events.Target

Expand Down Expand Up @@ -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"
Expand All @@ -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 = `
resource "aws_cloudwatch_event_rule" "foo" {
name = "tf-acc-cw-event-rule-basic"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ resource "aws_kinesis_stream" "test_stream" {
The following arguments are supported:

* `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.
* `input` - (Optional) Valid JSON text passed to the target.
* `input_path` - (Optional) The value of the [JSONPath](http://goessner.net/articles/JsonPath/)
Expand Down