From cde797a7cf1f854b89ee60eebf68424428ee9625 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Mon, 23 Apr 2018 10:28:06 +0100 Subject: [PATCH 1/4] Add batch job as a cloudwatch_event_target --- aws/resource_aws_cloudwatch_event_target.go | 72 ++++++++ ...source_aws_cloudwatch_event_target_test.go | 169 ++++++++++++++++++ .../r/cloudwatch_event_target.html.markdown | 8 + 3 files changed, 249 insertions(+) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index 15561a7609b..15023009a6d 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -102,6 +102,34 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { }, }, + "batch_target": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "job_definition": { + Type: schema.TypeString, + Required: true, + }, + "job_name": { + Type: schema.TypeString, + Required: true, + }, + "array_size": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(2, 10000), + }, + "job_attempts": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntBetween(1, 10), + }, + }, + }, + }, + "input_transformer": { Type: schema.TypeList, Optional: true, @@ -209,6 +237,12 @@ func resourceAwsCloudWatchEventTargetRead(d *schema.ResourceData, meta interface } } + if t.BatchParameters != nil { + if err := d.Set("batch_target", flattenAwsCloudWatchEventTargetBatchParameters(t.BatchParameters)); err != nil { + return fmt.Errorf("[DEBUG] Error setting batch_target error: %#v", err) + } + } + 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) @@ -299,6 +333,9 @@ func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput { if v, ok := d.GetOk("ecs_target"); ok { e.EcsParameters = expandAwsCloudWatchEventTargetEcsParameters(v.([]interface{})) } + if v, ok := d.GetOk("batch_target"); ok { + e.BatchParameters = expandAwsCloudWatchEventTargetBatchParameters(v.([]interface{})) + } if v, ok := d.GetOk("input_transformer"); ok { e.InputTransformer = expandAwsCloudWatchEventTransformerParameters(v.([]interface{})) @@ -344,6 +381,27 @@ func expandAwsCloudWatchEventTargetEcsParameters(config []interface{}) *events.E return ecsParameters } +func expandAwsCloudWatchEventTargetBatchParameters(config []interface{}) *events.BatchParameters { + batchParameters := &events.BatchParameters{} + for _, c := range config { + param := c.(map[string]interface{}) + batchParameters.JobDefinition = aws.String(param["job_definition"].(string)) + batchParameters.JobName = aws.String(param["job_name"].(string)) + if v := param["array_size"]; v != nil { + arrayProperties := &events.BatchArrayProperties{} + arrayProperties.Size = aws.Int64(int64(v.(int))) + batchParameters.ArrayProperties = arrayProperties + } + if v := param["job_attempts"]; v != nil { + retryStrategy := &events.BatchRetryStrategy{} + retryStrategy.Attempts = aws.Int64(int64(v.(int))) + batchParameters.RetryStrategy = retryStrategy + } + } + + return batchParameters +} + func expandAwsCloudWatchEventTransformerParameters(config []interface{}) *events.InputTransformer { transformerParameters := &events.InputTransformer{} @@ -385,6 +443,20 @@ func flattenAwsCloudWatchEventTargetEcsParameters(ecsParameters *events.EcsParam return result } +func flattenAwsCloudWatchEventTargetBatchParameters(batchParameters *events.BatchParameters) []map[string]interface{} { + config := make(map[string]interface{}) + config["job_definition"] = aws.StringValue(batchParameters.JobDefinition) + config["job_name"] = aws.StringValue(batchParameters.JobName) + if batchParameters.ArrayProperties != nil { + config["array_size"] = int(aws.Int64Value(batchParameters.ArrayProperties.Size)) + } + if batchParameters.RetryStrategy != nil { + config["job_attempts"] = int(aws.Int64Value(batchParameters.RetryStrategy.Attempts)) + } + 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) diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index ef69f943d1b..886c643158f 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -139,6 +139,26 @@ func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { }, }) } + +func TestAccAWSCloudWatchEventTarget_batch(t *testing.T) { + var target events.Target + rName := acctest.RandomWithPrefix("tf_batch_target") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchEventTargetConfigBatch(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + ), + }, + }, + }) +} + func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) { var target events.Target rName := acctest.RandomWithPrefix("tf_input_transformer") @@ -473,6 +493,155 @@ EOF }`, rName, rName, rName, rName, rName) } +func testAccAWSCloudWatchEventTargetConfigBatch(rName string) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_event_rule" "cloudwatch_event_rule" { + name = "%[1]s" + description = "schedule_batch_test" + schedule_expression = "rate(5 minutes)" +} + +resource "aws_cloudwatch_event_target" "test" { + arn = "${aws_batch_job_queue.batch_job_queue.arn}" + rule = "${aws_cloudwatch_event_rule.cloudwatch_event_rule.id}" + role_arn = "${aws_iam_role.event_iam_role.arn}" + + batch_target { + job_definition = "${aws_batch_job_definition.batch_job_definition}" + job_name = "%[1]s" + } +} + +resource "aws_iam_role" "event_iam_role" { + name = "event_%[1]s" + assume_role_policy = < Date: Mon, 23 Apr 2018 14:59:18 +0100 Subject: [PATCH 2/4] Mark job_attempts optional --- aws/resource_aws_cloudwatch_event_target.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index 15023009a6d..143c3a579a1 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -123,7 +123,7 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { }, "job_attempts": { Type: schema.TypeInt, - Required: true, + Optional: true, ValidateFunc: validation.IntBetween(1, 10), }, }, From 47b08e886f492798618beda96ee557b29fe7702a Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Mon, 23 Apr 2018 15:53:50 +0100 Subject: [PATCH 3/4] Test fixes: add missing ARNs, fix depends_on's --- aws/resource_aws_cloudwatch_event_target_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 886c643158f..cf0c2a00ea4 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -507,9 +507,15 @@ resource "aws_cloudwatch_event_target" "test" { role_arn = "${aws_iam_role.event_iam_role.arn}" batch_target { - job_definition = "${aws_batch_job_definition.batch_job_definition}" + job_definition = "${aws_batch_job_definition.batch_job_definition.arn}" job_name = "%[1]s" } + + depends_on = [ + "aws_batch_job_queue.batch_job_queue", + "aws_batch_job_definition.batch_job_definition", + "aws_iam_role.event_iam_role", + ] } resource "aws_iam_role" "event_iam_role" { @@ -613,14 +619,14 @@ resource "aws_batch_compute_environment" "batch_compute_environment" { } service_role = "${aws_iam_role.batch_iam_role.arn}" type = "MANAGED" - depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"] + depends_on = ["aws_iam_role_policy_attachment.batch_policy_attachment"] } resource "aws_batch_job_queue" "batch_job_queue" { name = "%[1]s" state = "ENABLED" priority = 1 - compute_environments = ["${aws_batch_compute_environment.batch_compute_environment}"] + compute_environments = ["${aws_batch_compute_environment.batch_compute_environment.arn}"] } resource "aws_batch_job_definition" "batch_job_definition" { From ac458d87df282009e946deb3762282cb2fc68707 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Mon, 23 Apr 2018 17:19:21 +0100 Subject: [PATCH 4/4] Check that array_size and job_attempts values are within range --- aws/resource_aws_cloudwatch_event_target.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index 143c3a579a1..73acb178740 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -387,14 +387,14 @@ func expandAwsCloudWatchEventTargetBatchParameters(config []interface{}) *events param := c.(map[string]interface{}) batchParameters.JobDefinition = aws.String(param["job_definition"].(string)) batchParameters.JobName = aws.String(param["job_name"].(string)) - if v := param["array_size"]; v != nil { + if v, ok := param["array_size"].(int); ok && v > 1 && v <= 10000 { arrayProperties := &events.BatchArrayProperties{} - arrayProperties.Size = aws.Int64(int64(v.(int))) + arrayProperties.Size = aws.Int64(int64(v)) batchParameters.ArrayProperties = arrayProperties } - if v := param["job_attempts"]; v != nil { + if v, ok := param["job_attempts"].(int); ok && v > 0 && v <= 10 { retryStrategy := &events.BatchRetryStrategy{} - retryStrategy.Attempts = aws.Int64(int64(v.(int))) + retryStrategy.Attempts = aws.Int64(int64(v)) batchParameters.RetryStrategy = retryStrategy } }