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

Add support for cloudwatch event target import #9431

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 21 additions & 0 deletions 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"
"math"
"regexp"
"strings"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -22,6 +23,10 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
Update: resourceAwsCloudWatchEventTargetUpdate,
Delete: resourceAwsCloudWatchEventTargetDelete,

Importer: &schema.ResourceImporter{
State: resourceAwsCloudWatchEventTargetImport,
},

Schema: map[string]*schema.Schema{
"rule": {
Type: schema.TypeString,
Expand Down Expand Up @@ -651,3 +656,19 @@ func flattenAwsCloudWatchInputTransformer(inputTransformer *events.InputTransfor
result := []map[string]interface{}{config}
return result
}

func resourceAwsCloudWatchEventTargetImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.SplitN(d.Id(), "/", 2)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format (%q), expected <rule-name>/<target-id>", d.Id())
}

ruleName := idParts[0]
targetName := idParts[1]

d.Set("target_id", targetName)
d.Set("rule", ruleName)
d.SetId(ruleName + "-" + targetName)

return []*schema.ResourceData{d}, nil
}
17 changes: 17 additions & 0 deletions aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) {
regexp.MustCompile(fmt.Sprintf(":%s$", snsTopicName2))),
),
},
{
ResourceName: "aws_cloudwatch_event_target.moobar",
ImportState: true,
ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.moobar"),
ImportStateVerify: 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 @chaspy - thanks so much for this contribution! Overall, this looks great, but for the sake of completeness, could you add this block to all of the TestAccAWSCloudWatchEventTarget acceptance tests in this file?

{
				ResourceName:      "aws_cloudwatch_event_target.moobar",
				ImportState:       true,
				ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc("aws_cloudwatch_event_target.moobar"),
				ImportStateVerify: true,
			},

You'll want to make sure that ResourceName matches for each test since it looks like they vary a bit from test to test. If you're able to add these and re-run the acceptance tests for us, we can go ahead and get this merged in. Thanks! 👍

},
})
}
Expand Down Expand Up @@ -332,6 +338,17 @@ func testAccCheckAWSCloudWatchEventTargetDestroy(s *terraform.State) error {
return nil
}

func testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s", rs.Primary.Attributes["rule"], rs.Primary.Attributes["target_id"]), nil
}
}

func testAccAWSCloudWatchEventTargetConfig(ruleName, snsTopicName, targetID string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_rule" "foo" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/cloudwatch_event_target.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,11 @@ For more information, see [Task Networking](https://docs.aws.amazon.com/AmazonEC

* `input_paths` - (Optional) Key value pairs specified in the form of JSONPath (for example, time = $.time)
* `input_template` - (Required) Structure containing the template body.

## Import

Cloud Watch Event Target can be imported using the role event_rule and target_id separated by `/`.

```
$ terraform import aws_cloudwatch_event_target.test-event-target rule-name/target-id
```