Skip to content

Commit

Permalink
Merge pull request #34997 from drewmullen/f-batch_job_def-arn-prefix-…
Browse files Browse the repository at this point in the history
…schedule-prio

Feat `aws_batch_job_definition`: include `arn_prefix` and `scheduling_priority`
  • Loading branch information
ewbankkit authored Jan 3, 2024
2 parents a471f11 + 45b4f96 commit 56d40c5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .changelog/34997.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_batch_job_definition: Add `scheduling_priority` argument and `arn_prefix` attribute
```
49 changes: 41 additions & 8 deletions internal/service/batch/job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func ResourceJobDefinition() *schema.Resource {
ReadWithoutTimeout: resourceJobDefinitionRead,
UpdateWithoutTimeout: resourceJobDefinitionUpdate,
DeleteWithoutTimeout: resourceJobDefinitionDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -47,6 +48,12 @@ func ResourceJobDefinition() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"arn_prefix": {
Type: schema.TypeString,
Computed: true,
},

"container_properties": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -63,12 +70,14 @@ func ResourceJobDefinition() *schema.Resource {
},
ValidateFunc: validJobContainerProperties,
},

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validName,
},

"node_properties": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -84,6 +93,7 @@ func ResourceJobDefinition() *schema.Resource {
},
ValidateFunc: validJobNodeProperties,
},

"eks_properties": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -309,12 +319,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,
Expand All @@ -324,12 +336,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,
Expand Down Expand Up @@ -393,12 +407,21 @@ func ResourceJobDefinition() *schema.Resource {
},
},
},

"revision": {
Type: schema.TypeInt,
Computed: true,
},

"scheduling_priority": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},

names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),

"timeout": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -415,6 +438,7 @@ func ResourceJobDefinition() *schema.Resource {
},
},
},

"type": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -503,6 +527,10 @@ func resourceJobDefinitionCreate(ctx context.Context, d *schema.ResourceData, me
input.RetryStrategy = expandRetryStrategy(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{}))
}
Expand Down Expand Up @@ -534,7 +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)
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)

Expand All @@ -546,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 {
Expand All @@ -556,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)
Expand All @@ -573,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 {
Expand All @@ -583,9 +615,10 @@ func resourceJobDefinitionRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("timeout", nil)
}

d.Set("revision", jobDefinition.Revision)
d.Set("type", jobDefinition.Type)

setTagsOut(ctx, jobDefinition.Tags)

return diags
}

Expand Down
69 changes: 52 additions & 17 deletions internal/service/batch/job_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -57,8 +58,8 @@ 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"),
),
Expand Down Expand Up @@ -671,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]
Expand Down Expand Up @@ -817,7 +846,6 @@ resource "aws_batch_job_definition" "test" {
]
}
CONTAINER_PROPERTIES
}
`, rName)
}
Expand Down Expand Up @@ -860,7 +888,6 @@ resource "aws_batch_job_definition" "test" {
]
}
CONTAINER_PROPERTIES
}
`, rName)
}
Expand Down Expand Up @@ -1118,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"
Expand Down Expand Up @@ -1192,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"
Expand Down Expand Up @@ -1234,7 +1257,7 @@ resource "aws_batch_job_definition" "test" {
numNodes = 4
})
}
`, rName)
`, rName)
}

func testAccJobDefinitionConfig_EKSProperties_basic(rName string) string {
Expand Down Expand Up @@ -1266,7 +1289,8 @@ resource "aws_batch_job_definition" "test" {
}
}
}
}`, rName)
}
`, rName)
}

func testAccJobDefinitionConfig_EKSProperties_advancedUpdate(rName string) string {
Expand Down Expand Up @@ -1337,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"
Expand Down Expand Up @@ -1383,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"
Expand All @@ -1409,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)
}
4 changes: 3 additions & 1 deletion website/docs/r/batch_job_definition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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).

Expand Down

0 comments on commit 56d40c5

Please sign in to comment.