Skip to content

Commit

Permalink
feat(batch): add container_properties to aws_batch_job_definition
Browse files Browse the repository at this point in the history
… data source.
  • Loading branch information
Steven Kalt committed Nov 12, 2024
1 parent ab85962 commit 5171b46
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 15 deletions.
72 changes: 57 additions & 15 deletions internal/service/batch/job_definition_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (d *jobDefinitionDataSource) Schema(ctx context.Context, request datasource
"container_orchestration_type": schema.StringAttribute{
Computed: true,
},
"container_properties": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[containerPropertiesModel](ctx),
Computed: true,
},
"eks_properties": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[eksPropertiesModel](ctx),
Computed: true,
Expand Down Expand Up @@ -138,7 +142,6 @@ func (d *jobDefinitionDataSource) Read(ctx context.Context, request datasource.R
}

output, err := findJobDefinition(ctx, conn, input)

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("reading Batch Job Definition (%s)", arn), err.Error())

Expand Down Expand Up @@ -192,6 +195,44 @@ func (d *jobDefinitionDataSource) Read(ctx context.Context, request datasource.R
}
}

if cProps := jd.ContainerProperties; cProps != nil {
// HACK: populate .ResourceRequirements using the .Memory and .Vcpus fields.
// Currently, findJobDefinition() returns the deprecated fields and doesn't
// populate the preferred .ResourceRequirements field.
if cProps.Memory != nil {
found := false
for _, r := range cProps.ResourceRequirements {
if r.Type == awstypes.ResourceTypeMemory {
found = true
break
}
}
if !found {
val := fmt.Sprintf("%d", aws.ToInt32(cProps.Memory))
cProps.ResourceRequirements = append(cProps.ResourceRequirements, awstypes.ResourceRequirement{
Type: awstypes.ResourceTypeMemory,
Value: &val,
})
}
}
if cProps.Vcpus != nil {
found := false
for _, r := range cProps.ResourceRequirements {
if r.Type == awstypes.ResourceTypeVcpu {
found = true
break
}
}
if !found {
val := fmt.Sprintf("%d", aws.ToInt32(cProps.Vcpus))
cProps.ResourceRequirements = append(cProps.ResourceRequirements, awstypes.ResourceRequirement{
Type: awstypes.ResourceTypeVcpu,
Value: &val,
})
}
}
}

response.Diagnostics.Append(fwflex.Flatten(ctx, jd, &data)...)
if response.Diagnostics.HasError() {
return
Expand All @@ -215,20 +256,21 @@ func (d *jobDefinitionDataSource) ConfigValidators(context.Context) []resource.C
}

type jobDefinitionDataSourceModel struct {
ARNPrefix types.String `tfsdk:"arn_prefix"`
ContainerOrchestrationType types.String `tfsdk:"container_orchestration_type"`
EKSProperties fwtypes.ListNestedObjectValueOf[eksPropertiesModel] `tfsdk:"eks_properties"`
ID types.String `tfsdk:"id"`
JobDefinitionARN fwtypes.ARN `tfsdk:"arn"`
JobDefinitionName types.String `tfsdk:"name"`
NodeProperties fwtypes.ListNestedObjectValueOf[nodePropertiesModel] `tfsdk:"node_properties"`
RetryStrategy fwtypes.ListNestedObjectValueOf[retryStrategyModel] `tfsdk:"retry_strategy"`
Revision types.Int64 `tfsdk:"revision"`
SchedulingPriority types.Int64 `tfsdk:"scheduling_priority"`
Status types.String `tfsdk:"status"`
Tags tftags.Map `tfsdk:"tags"`
Timeout fwtypes.ListNestedObjectValueOf[jobTimeoutModel] `tfsdk:"timeout"`
Type types.String `tfsdk:"type"`
ARNPrefix types.String `tfsdk:"arn_prefix"`
ContainerOrchestrationType types.String `tfsdk:"container_orchestration_type"`
ContainerProperties fwtypes.ListNestedObjectValueOf[containerPropertiesModel] `tfsdk:"container_properties"`
EKSProperties fwtypes.ListNestedObjectValueOf[eksPropertiesModel] `tfsdk:"eks_properties"`
ID types.String `tfsdk:"id"`
JobDefinitionARN fwtypes.ARN `tfsdk:"arn"`
JobDefinitionName types.String `tfsdk:"name"`
NodeProperties fwtypes.ListNestedObjectValueOf[nodePropertiesModel] `tfsdk:"node_properties"`
RetryStrategy fwtypes.ListNestedObjectValueOf[retryStrategyModel] `tfsdk:"retry_strategy"`
Revision types.Int64 `tfsdk:"revision"`
SchedulingPriority types.Int64 `tfsdk:"scheduling_priority"`
Status types.String `tfsdk:"status"`
Tags tftags.Map `tfsdk:"tags"`
Timeout fwtypes.ListNestedObjectValueOf[jobTimeoutModel] `tfsdk:"timeout"`
Type types.String `tfsdk:"type"`
}

type eksPropertiesModel struct {
Expand Down
37 changes: 37 additions & 0 deletions internal/service/batch/job_definition_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package batch_test

import (
"fmt"
"os"
"testing"

"github.com/YakDriver/regexache"
Expand Down Expand Up @@ -205,3 +206,39 @@ data "aws_batch_job_definition" "test" {
}
`)
}

func TestAccJobDefinitionDataSourceConfig_basicARN_ContainerProperties(t *testing.T) {
env := os.Getenv("TF_ACC")
fmt.Println(env)
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_batch_job_definition.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.BatchEndpointID)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccJobDefinitionDataSourceConfig_basicARN(rName, "1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.image", "busybox"),

resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.command.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.command.0", "echo"),
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.command.1", "test1"),

resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.environment.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.volumes.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.mount_points.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.ulimits.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "container_properties.0.resource_requirements.#", "2"),
),
},
},
})
}
1 change: 1 addition & 0 deletions website/docs/d/batch_job_definition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The following arguments are optional:
This data source exports the following attributes in addition to the arguments above:

* `container_orchestration_type` - The orchestration type of the compute environment.
* `container_properties` - An [object](#container) describing the container properties of AWS ECS-based jobs.
* `scheduling_priority` - The scheduling priority for jobs that are submitted with this 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.
* `id` - The ARN
* `eks_properties` - An [object](#eks_properties) with various properties that are specific to Amazon EKS based jobs. This must not be specified for Amazon ECS based job definitions.
Expand Down

0 comments on commit 5171b46

Please sign in to comment.