From 7ab83666467d8f8deb640f56fceb3a91699de2ae Mon Sep 17 00:00:00 2001 From: Drew Mullen Date: Tue, 19 Dec 2023 15:18:53 -0500 Subject: [PATCH 1/7] include arn_prefix and scheduling_priority --- internal/service/batch/job_definition.go | 28 +++++++++++++++++++ internal/service/batch/job_definition_test.go | 4 +++ .../docs/r/batch_job_definition.html.markdown | 4 ++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/service/batch/job_definition.go b/internal/service/batch/job_definition.go index a3c7afb5726..fe0a98f80bd 100644 --- a/internal/service/batch/job_definition.go +++ b/internal/service/batch/job_definition.go @@ -47,6 +47,12 @@ func ResourceJobDefinition() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "arn_prefix": { + Type: schema.TypeString, + Computed: true, + }, + "container_properties": { Type: schema.TypeString, Optional: true, @@ -63,12 +69,14 @@ func ResourceJobDefinition() *schema.Resource { }, ValidateFunc: validJobContainerProperties, }, + "name": { Type: schema.TypeString, Required: true, ForceNew: true, ValidateFunc: validName, }, + "node_properties": { Type: schema.TypeString, Optional: true, @@ -84,6 +92,7 @@ func ResourceJobDefinition() *schema.Resource { }, ValidateFunc: validJobNodeProperties, }, + "eks_properties": { Type: schema.TypeList, MaxItems: 1, @@ -309,12 +318,14 @@ func ResourceJobDefinition() *schema.Resource { }, }, }, + "parameters": { Type: schema.TypeMap, Optional: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "platform_capabilities": { Type: schema.TypeSet, Optional: true, @@ -324,12 +335,14 @@ func ResourceJobDefinition() *schema.Resource { ValidateFunc: validation.StringInSlice(batch.PlatformCapability_Values(), false), }, }, + "propagate_tags": { Type: schema.TypeBool, Optional: true, ForceNew: true, Default: false, }, + "retry_strategy": { Type: schema.TypeList, Optional: true, @@ -393,12 +406,20 @@ func ResourceJobDefinition() *schema.Resource { }, }, }, + "revision": { Type: schema.TypeInt, Computed: true, }, + + "scheduling_priority": { + Type: schema.TypeInt, + Optional: true, + }, + names.AttrTags: tftags.TagsSchema(), names.AttrTagsAll: tftags.TagsSchemaComputed(), + "timeout": { Type: schema.TypeList, Optional: true, @@ -415,6 +436,7 @@ func ResourceJobDefinition() *schema.Resource { }, }, }, + "type": { Type: schema.TypeString, Required: true, @@ -507,6 +529,10 @@ func resourceJobDefinitionCreate(ctx context.Context, d *schema.ResourceData, me input.Timeout = expandJobTimeout(v.([]interface{})[0].(map[string]interface{})) } + if v, ok := d.GetOk("scheduling_priority"); ok { + input.SchedulingPriority = aws.Int64(int64(v.(int))) + } + output, err := conn.RegisterJobDefinitionWithContext(ctx, input) if err != nil { @@ -535,6 +561,7 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta } d.Set("arn", jobDefinition.JobDefinitionArn) + d.Set("arn_prefix", strings.TrimSuffix(*jobDefinition.JobDefinitionArn, fmt.Sprintf(":%d", *jobDefinition.Revision))) containerProperties, err := flattenContainerProperties(jobDefinition.ContainerProperties) @@ -585,6 +612,7 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta d.Set("revision", jobDefinition.Revision) d.Set("type", jobDefinition.Type) + d.Set("scheduling_priority", jobDefinition.SchedulingPriority) return diags } diff --git a/internal/service/batch/job_definition_test.go b/internal/service/batch/job_definition_test.go index 35d348b26f8..5e77d4bab6a 100644 --- a/internal/service/batch/job_definition_test.go +++ b/internal/service/batch/job_definition_test.go @@ -39,6 +39,7 @@ func TestAccBatchJobDefinition_basic(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckJobDefinitionExists(ctx, resourceName, &jd), acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "batch", regexache.MustCompile(fmt.Sprintf(`job-definition/%s:\d+`, rName))), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn_prefix", "batch", regexache.MustCompile(fmt.Sprintf(`job-definition/%s`, rName))), acctest.CheckResourceAttrEquivalentJSON(resourceName, "container_properties", `{ "command": ["echo", "test"], "image": "busybox", @@ -61,6 +62,7 @@ func TestAccBatchJobDefinition_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "tags_all.%", "0"), resource.TestCheckResourceAttr(resourceName, "timeout.#", "0"), resource.TestCheckResourceAttr(resourceName, "type", "container"), + resource.TestCheckResourceAttr(resourceName, "scheduling_priority", "2"), ), }, { @@ -876,6 +878,8 @@ resource "aws_batch_job_definition" "test" { }) name = %[1]q type = "container" + + scheduling_priority = 2 } `, rName) } diff --git a/website/docs/r/batch_job_definition.html.markdown b/website/docs/r/batch_job_definition.html.markdown index 512adbf6646..c5204b0c7c0 100644 --- a/website/docs/r/batch_job_definition.html.markdown +++ b/website/docs/r/batch_job_definition.html.markdown @@ -210,6 +210,7 @@ The following arguments are optional: * `propagate_tags` - (Optional) Specifies whether to propagate the tags from the job definition to the corresponding Amazon ECS task. Default is `false`. * `retry_strategy` - (Optional) Specifies the retry strategy to use for failed jobs that are submitted with this job definition. Maximum number of `retry_strategy` is `1`. Defined below. +* `scheduling_priority` - (Optional) The scheduling priority of the job definition. This only affects jobs in job queues with a fair share policy. Jobs with a higher scheduling priority are scheduled before jobs with a lower scheduling priority. Allowed values `0` through `9999`. * `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. * `timeout` - (Optional) Specifies the timeout for jobs so that if a job runs longer, AWS Batch terminates the job. Maximum number of `timeout` is `1`. Defined below. @@ -277,7 +278,8 @@ The following arguments are optional: This resource exports the following attributes in addition to the arguments above: -* `arn` - The Amazon Resource Name of the job definition. +* `arn` - The Amazon Resource Name of the job definition, includes revision (`:#`). +* `arn_prefix` - The ARN without the revision number. * `revision` - The revision of the job definition. * `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block). From 1d72e2822bfd4fd447a3c471753866bef12c89f0 Mon Sep 17 00:00:00 2001 From: Drew Mullen Date: Tue, 19 Dec 2023 15:22:55 -0500 Subject: [PATCH 2/7] changelog --- .changelog/34997.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/34997.txt diff --git a/.changelog/34997.txt b/.changelog/34997.txt new file mode 100644 index 00000000000..8da300f0b85 --- /dev/null +++ b/.changelog/34997.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_batch_job_definition: Included new parameter `scheduling_priority` and attribute `arn_prefix`. +``` \ No newline at end of file From 09c54919e8c79877435ac0b3d0165179645e8b0e Mon Sep 17 00:00:00 2001 From: Drew Mullen Date: Tue, 19 Dec 2023 15:30:24 -0500 Subject: [PATCH 3/7] linting --- internal/service/batch/job_definition_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/service/batch/job_definition_test.go b/internal/service/batch/job_definition_test.go index 5e77d4bab6a..582edebf8e2 100644 --- a/internal/service/batch/job_definition_test.go +++ b/internal/service/batch/job_definition_test.go @@ -876,9 +876,8 @@ resource "aws_batch_job_definition" "test" { memory = 128 vcpus = 1 }) - name = %[1]q - type = "container" - + name = %[1]q + type = "container" scheduling_priority = 2 } `, rName) From 46e2994bc491d46b315aaa05c688fa8666bf9908 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 3 Jan 2024 10:17:40 -0500 Subject: [PATCH 4/7] Tweak CHANGELOG entry. --- .changelog/34997.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/34997.txt b/.changelog/34997.txt index 8da300f0b85..f556796ff21 100644 --- a/.changelog/34997.txt +++ b/.changelog/34997.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -resource/aws_batch_job_definition: Included new parameter `scheduling_priority` and attribute `arn_prefix`. +resource/aws_batch_job_definition: Add `scheduling_priority` argument and `arn_prefix` attribute ``` \ No newline at end of file From 5e60d6fa9e9a82b7391a715ace4fb1d2d96efa60 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 3 Jan 2024 10:23:15 -0500 Subject: [PATCH 5/7] r/aws_batch_job_definition: Cosmetics. --- internal/service/batch/job_definition.go | 33 ++++++++++++++---------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/internal/service/batch/job_definition.go b/internal/service/batch/job_definition.go index fe0a98f80bd..34d64193a15 100644 --- a/internal/service/batch/job_definition.go +++ b/internal/service/batch/job_definition.go @@ -38,6 +38,7 @@ func ResourceJobDefinition() *schema.Resource { ReadWithoutTimeout: resourceJobDefinitionRead, UpdateWithoutTimeout: resourceJobDefinitionUpdate, DeleteWithoutTimeout: resourceJobDefinitionDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -415,6 +416,7 @@ func ResourceJobDefinition() *schema.Resource { "scheduling_priority": { Type: schema.TypeInt, Optional: true, + ForceNew: true, }, names.AttrTags: tftags.TagsSchema(), @@ -525,14 +527,14 @@ func resourceJobDefinitionCreate(ctx context.Context, d *schema.ResourceData, me input.RetryStrategy = expandRetryStrategy(v.([]interface{})[0].(map[string]interface{})) } - if v, ok := d.GetOk("timeout"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { - input.Timeout = expandJobTimeout(v.([]interface{})[0].(map[string]interface{})) - } - if v, ok := d.GetOk("scheduling_priority"); ok { input.SchedulingPriority = aws.Int64(int64(v.(int))) } + if v, ok := d.GetOk("timeout"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { + input.Timeout = expandJobTimeout(v.([]interface{})[0].(map[string]interface{})) + } + output, err := conn.RegisterJobDefinitionWithContext(ctx, input) if err != nil { @@ -560,8 +562,9 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta return sdkdiag.AppendErrorf(diags, "reading Batch Job Definition (%s): %s", d.Id(), err) } - d.Set("arn", jobDefinition.JobDefinitionArn) - d.Set("arn_prefix", strings.TrimSuffix(*jobDefinition.JobDefinitionArn, fmt.Sprintf(":%d", *jobDefinition.Revision))) + arn, revision := aws.StringValue(jobDefinition.JobDefinitionArn), aws.Int64Value(jobDefinition.Revision) + d.Set("arn", arn) + d.Set("arn_prefix", strings.TrimSuffix(arn, fmt.Sprintf(":%d", revision))) containerProperties, err := flattenContainerProperties(jobDefinition.ContainerProperties) @@ -573,6 +576,12 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta return sdkdiag.AppendErrorf(diags, "setting container_properties: %s", err) } + if err := d.Set("eks_properties", flattenEKSProperties(jobDefinition.EksProperties)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting eks_properties: %s", err) + } + + d.Set("name", jobDefinition.JobDefinitionName) + nodeProperties, err := flattenNodeProperties(jobDefinition.NodeProperties) if err != nil { @@ -583,11 +592,6 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta return sdkdiag.AppendErrorf(diags, "setting node_properties: %s", err) } - if err := d.Set("eks_properties", flattenEKSProperties(jobDefinition.EksProperties)); err != nil { - return sdkdiag.AppendErrorf(diags, "setting eks_properties: %s", err) - } - - d.Set("name", jobDefinition.JobDefinitionName) d.Set("parameters", aws.StringValueMap(jobDefinition.Parameters)) d.Set("platform_capabilities", aws.StringValueSlice(jobDefinition.PlatformCapabilities)) d.Set("propagate_tags", jobDefinition.PropagateTags) @@ -600,7 +604,8 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta d.Set("retry_strategy", nil) } - setTagsOut(ctx, jobDefinition.Tags) + d.Set("revision", revision) + d.Set("scheduling_priority", jobDefinition.SchedulingPriority) if jobDefinition.Timeout != nil { if err := d.Set("timeout", []interface{}{flattenJobTimeout(jobDefinition.Timeout)}); err != nil { @@ -610,9 +615,9 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta d.Set("timeout", nil) } - d.Set("revision", jobDefinition.Revision) d.Set("type", jobDefinition.Type) - d.Set("scheduling_priority", jobDefinition.SchedulingPriority) + + setTagsOut(ctx, jobDefinition.Tags) return diags } From a9672655172c285e44dfce8f183e85e1042ea42d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 3 Jan 2024 10:29:11 -0500 Subject: [PATCH 6/7] Add 'TestAccBatchJobDefinition_schedulingPriority'. --- internal/service/batch/job_definition_test.go | 70 ++++++++++++++----- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/internal/service/batch/job_definition_test.go b/internal/service/batch/job_definition_test.go index 582edebf8e2..928f34a7654 100644 --- a/internal/service/batch/job_definition_test.go +++ b/internal/service/batch/job_definition_test.go @@ -58,11 +58,10 @@ func TestAccBatchJobDefinition_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "propagate_tags", "false"), resource.TestCheckResourceAttr(resourceName, "retry_strategy.#", "0"), resource.TestCheckResourceAttr(resourceName, "revision", "1"), + resource.TestCheckResourceAttr(resourceName, "scheduling_priority", "0"), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), - resource.TestCheckResourceAttr(resourceName, "tags_all.%", "0"), resource.TestCheckResourceAttr(resourceName, "timeout.#", "0"), resource.TestCheckResourceAttr(resourceName, "type", "container"), - resource.TestCheckResourceAttr(resourceName, "scheduling_priority", "2"), ), }, { @@ -673,6 +672,34 @@ func TestAccBatchJobDefinition_createTypeMultiNodeWithContainerProperties(t *tes }) } +func TestAccBatchJobDefinition_schedulingPriority(t *testing.T) { + ctx := acctest.Context(t) + var jd batch.JobDefinition + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_batch_job_definition.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, batch.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckJobDefinitionDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccJobDefinitionConfig_schedulingPriority(rName, 2), + Check: resource.ComposeTestCheckFunc( + testAccCheckJobDefinitionExists(ctx, resourceName, &jd), + resource.TestCheckResourceAttr(resourceName, "scheduling_priority", "2"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckJobDefinitionExists(ctx context.Context, n string, jd *batch.JobDefinition) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -819,7 +846,6 @@ resource "aws_batch_job_definition" "test" { ] } CONTAINER_PROPERTIES - } `, rName) } @@ -862,7 +888,6 @@ resource "aws_batch_job_definition" "test" { ] } CONTAINER_PROPERTIES - } `, rName) } @@ -878,7 +903,6 @@ resource "aws_batch_job_definition" "test" { }) name = %[1]q type = "container" - scheduling_priority = 2 } `, rName) } @@ -1121,8 +1145,6 @@ resource "aws_batch_job_definition" "test" { func testAccJobDefinitionConfig_nodePropertiesAdvanced(rName string) string { return fmt.Sprintf(` - - resource "aws_batch_job_definition" "test" { name = %[1]q type = "multinode" @@ -1195,8 +1217,6 @@ resource "aws_batch_job_definition" "test" { func testAccJobDefinitionConfig_nodePropertiesAdvancedUpdate(rName string) string { return fmt.Sprintf(` - - resource "aws_batch_job_definition" "test" { name = %[1]q type = "multinode" @@ -1237,7 +1257,7 @@ resource "aws_batch_job_definition" "test" { numNodes = 4 }) } - `, rName) +`, rName) } func testAccJobDefinitionConfig_EKSProperties_basic(rName string) string { @@ -1269,7 +1289,8 @@ resource "aws_batch_job_definition" "test" { } } } -}`, rName) +} +`, rName) } func testAccJobDefinitionConfig_EKSProperties_advancedUpdate(rName string) string { @@ -1340,13 +1361,12 @@ resource "aws_batch_job_definition" "test" { timeout { attempt_duration_seconds = 60 } -}`, rName) +} +`, rName) } func testAccJobDefinitionConfig_createTypeContainerWithNodeProperties(rName string) string { return fmt.Sprintf(` - - resource "aws_batch_job_definition" "test" { name = %[1]q type = "container" @@ -1386,15 +1406,12 @@ resource "aws_batch_job_definition" "test" { ] numNodes = 4 }) - } - `, rName) +`, rName) } func testAccJobDefinitionConfig_createTypeMultiNodeWithContainerProperties(rName string) string { return fmt.Sprintf(` - - resource "aws_batch_job_definition" "test" { name = %[1]q type = "multinode" @@ -1412,7 +1429,22 @@ resource "aws_batch_job_definition" "test" { memory = 128 vcpus = 1 }) +} +`, rName) +} +func testAccJobDefinitionConfig_schedulingPriority(rName string, priority int) string { + return fmt.Sprintf(` +resource "aws_batch_job_definition" "test" { + container_properties = jsonencode({ + command = ["echo", "test"] + image = "busybox" + memory = 128 + vcpus = 1 + }) + name = %[1]q + type = "container" + scheduling_priority = %[2]d } - `, rName) +`, rName, priority) } From 45b4f9663eb322d69d50f4ef8a5deab507d9e274 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 3 Jan 2024 10:31:25 -0500 Subject: [PATCH 7/7] Fix terrafmt error. --- internal/service/batch/job_definition_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/batch/job_definition_test.go b/internal/service/batch/job_definition_test.go index 928f34a7654..e0ecc5108b2 100644 --- a/internal/service/batch/job_definition_test.go +++ b/internal/service/batch/job_definition_test.go @@ -901,8 +901,8 @@ resource "aws_batch_job_definition" "test" { memory = 128 vcpus = 1 }) - name = %[1]q - type = "container" + name = %[1]q + type = "container" } `, rName) }