Skip to content

Commit

Permalink
Merge pull request #1343 from DaveBlooman/input_transformer
Browse files Browse the repository at this point in the history
Add Input transformer for Cloudwatch Events
  • Loading branch information
Ninir authored Sep 21, 2017
2 parents ba4fc65 + c948350 commit 1443ae1
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
64 changes: 62 additions & 2 deletions aws/resource_aws_cloudwatch_event_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
},
},
},

"input_transformer": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"input_paths": {
Type: schema.TypeMap,
Optional: true,
},
"input_template": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 8192),
},
},
},
},
},
}
}
Expand Down Expand Up @@ -190,11 +209,16 @@ func resourceAwsCloudWatchEventTargetRead(d *schema.ResourceData, meta interface
}
}

if t.InputTransformer != nil {
if err := d.Set("input_transformer", flattenAwsCloudWatchInputTransformer(t.InputTransformer)); err != nil {
return fmt.Errorf("[DEBUG] Error setting input_transformer error: %#v", err)
}
}

return nil
}

func findEventTargetById(id, rule string, nextToken *string, conn *events.CloudWatchEvents) (
*events.Target, error) {
func findEventTargetById(id, rule string, nextToken *string, conn *events.CloudWatchEvents) (*events.Target, error) {
input := events.ListTargetsByRuleInput{
Rule: aws.String(rule),
NextToken: nextToken,
Expand Down Expand Up @@ -276,6 +300,10 @@ func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput {
e.EcsParameters = expandAwsCloudWatchEventTargetEcsParameters(v.([]interface{}))
}

if v, ok := d.GetOk("input_transformer"); ok {
e.InputTransformer = expandAwsCloudWatchEventTransformerParameters(v.([]interface{}))
}

input := events.PutTargetsInput{
Rule: aws.String(d.Get("rule").(string)),
Targets: []*events.Target{e},
Expand Down Expand Up @@ -316,6 +344,25 @@ func expandAwsCloudWatchEventTargetEcsParameters(config []interface{}) *events.E
return ecsParameters
}

func expandAwsCloudWatchEventTransformerParameters(config []interface{}) *events.InputTransformer {
transformerParameters := &events.InputTransformer{}

inputPathsMaps := map[string]*string{}

for _, c := range config {
param := c.(map[string]interface{})
inputPaths := param["input_paths"].(map[string]interface{})

for k, v := range inputPaths {
inputPathsMaps[k] = aws.String(v.(string))
}
transformerParameters.InputTemplate = aws.String(param["input_template"].(string))
}
transformerParameters.InputPathsMap = inputPathsMaps

return transformerParameters
}

func flattenAwsCloudWatchEventTargetRunParameters(runCommand *events.RunCommandParameters) []map[string]interface{} {
result := make([]map[string]interface{}, 0)

Expand All @@ -337,3 +384,16 @@ func flattenAwsCloudWatchEventTargetEcsParameters(ecsParameters *events.EcsParam
result := []map[string]interface{}{config}
return result
}

func flattenAwsCloudWatchInputTransformer(inputTransformer *events.InputTransformer) []map[string]interface{} {
config := make(map[string]interface{})
inputPathsMap := make(map[string]string)
for k, v := range inputTransformer.InputPathsMap {
inputPathsMap[k] = *v
}
config["input_template"] = *inputTransformer.InputTemplate
config["input_paths"] = inputPathsMap

result := []map[string]interface{}{config}
return result
}
78 changes: 78 additions & 0 deletions aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) {
},
})
}
func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) {
var target events.Target
rName := acctest.RandomWithPrefix("tf_input_transformer")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventTargetConfigInputTransformer(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target),
),
},
},
})
}

func testAccCheckCloudWatchEventTargetExists(n string, rule *events.Target) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -454,3 +472,63 @@ resource "aws_ecs_task_definition" "task" {
EOF
}`, rName, rName, rName, rName, rName)
}

func testAccAWSCloudWatchEventTargetConfigInputTransformer(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "iam_for_lambda" {
name = "tf_acc_input_transformer"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_lambda_function" "lambda" {
function_name = "tf_acc_input_transformer"
filename = "test-fixtures/lambdatest.zip"
source_code_hash = "${base64sha256(file("test-fixtures/lambdatest.zip"))}"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "nodejs4.3"
}
resource "aws_cloudwatch_event_rule" "schedule" {
name = "%s"
description = "test_input_transformer"
schedule_expression = "rate(5 minutes)"
}
resource "aws_cloudwatch_event_target" "test" {
arn = "${aws_lambda_function.lambda.arn}"
rule = "${aws_cloudwatch_event_rule.schedule.id}"
input_transformer {
input_paths {
time = "$.time"
}
input_template = <<EOF
{
"detail-type": "Scheduled Event",
"source": "aws.events",
"time": <time>,
"region": "eu-west-1",
"detail": {}
}
EOF
}
}`, rName)
}
5 changes: 5 additions & 0 deletions website/docs/r/cloudwatch_event_target.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The following arguments are supported:
* `role_arn` - (Optional) The Amazon Resource Name (ARN) of the IAM role to be used for this target when the rule is triggered. Required if `ecs_target` is used.
* `run_command_targets` - (Optional) Parameters used when you are using the rule to invoke Amazon EC2 Run Command. Documented below. A maximum of 5 are allowed.
* `ecs_target` - (Optional) Parameters used when you are using the rule to invoke Amazon ECS Task. Documented below. A maximum of 1 are allowed.
* `input_transformer` - (Optional) Parameters used when you are providing a custom input to a target based on certain event data.

`run_command_targets` support the following:

Expand All @@ -86,3 +87,7 @@ The following arguments are supported:
* `task_count` - (Optional) The number of tasks to create based on the TaskDefinition. The default is 1.
* `task_definition_arn` - (Required) The ARN of the task definition to use if the event target is an Amazon ECS cluster.

`input_transformer` support the following:

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

0 comments on commit 1443ae1

Please sign in to comment.