From c9483509c60b412daac9f3350d82834006552859 Mon Sep 17 00:00:00 2001 From: DaveBlooman Date: Thu, 27 Jul 2017 17:36:38 +0100 Subject: [PATCH] Add Input Transformer capability --- aws/resource_aws_cloudwatch_event_target.go | 64 ++++++++++++++- ...source_aws_cloudwatch_event_target_test.go | 78 +++++++++++++++++++ .../r/cloudwatch_event_target.html.markdown | 5 ++ 3 files changed, 145 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index 1678bc645ca..15561a7609b 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -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), + }, + }, + }, + }, }, } } @@ -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, @@ -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}, @@ -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) @@ -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 +} diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 3f8c6e370c5..2306c368d98 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -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 { @@ -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 = <, + "region": "eu-west-1", + "detail": {} +} +EOF + } +}`, rName) +} diff --git a/website/docs/r/cloudwatch_event_target.html.markdown b/website/docs/r/cloudwatch_event_target.html.markdown index 0c6b795b737..b72bfdaacc0 100644 --- a/website/docs/r/cloudwatch_event_target.html.markdown +++ b/website/docs/r/cloudwatch_event_target.html.markdown @@ -65,6 +65,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. * `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_parameters` support the following: @@ -76,3 +77,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.