diff --git a/.changelog/38784.txt b/.changelog/38784.txt new file mode 100644 index 000000000000..c631fda6f1a6 --- /dev/null +++ b/.changelog/38784.txt @@ -0,0 +1,7 @@ +```release-note:enhancement +resource/aws_batch_job_queue: Add `job_state_time_limit_action` argument +``` + +```release-note:enhancement +data-source/aws_batch_job_queue: Add `job_state_time_limit_action` attribute +``` diff --git a/internal/service/batch/job_queue.go b/internal/service/batch/job_queue.go index 6a3347c16cb3..1e958241e430 100644 --- a/internal/service/batch/job_queue.go +++ b/internal/service/batch/job_queue.go @@ -15,6 +15,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/batch" awstypes "github.com/aws/aws-sdk-go-v2/service/batch/types" "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -117,6 +118,30 @@ func (r *jobQueueResource) Schema(ctx context.Context, request resource.SchemaRe }, }, }, + "job_state_time_limit_action": schema.ListNestedBlock{ + CustomType: fwtypes.NewListNestedObjectTypeOf[jobStateTimeLimitActionModel](ctx), + NestedObject: schema.NestedBlockObject{ + Attributes: map[string]schema.Attribute{ + names.AttrAction: schema.StringAttribute{ + CustomType: fwtypes.StringEnumType[awstypes.JobStateTimeLimitActionsAction](), + Required: true, + }, + "max_time_seconds": schema.Int64Attribute{ + Required: true, + Validators: []validator.Int64{ + int64validator.Between(600, 86400), + }, + }, + "reason": schema.StringAttribute{ + Required: true, + }, + names.AttrState: schema.StringAttribute{ + CustomType: fwtypes.StringEnumType[awstypes.JobStateTimeLimitActionsState](), + Required: true, + }, + }, + }, + }, }, } } @@ -146,6 +171,10 @@ func (r *jobQueueResource) Create(ctx context.Context, request resource.CreateRe } else { input.ComputeEnvironmentOrder = expandComputeEnvironments(ctx, data.ComputeEnvironments) } + response.Diagnostics.Append(fwflex.Expand(ctx, data.JobStateTimeLimitActions, &input.JobStateTimeLimitActions)...) + if response.Diagnostics.HasError() { + return + } if !data.SchedulingPolicyARN.IsNull() { input.SchedulingPolicyArn = fwflex.StringFromFramework(ctx, data.SchedulingPolicyARN) } @@ -211,8 +240,13 @@ func (r *jobQueueResource) Read(ctx context.Context, request resource.ReadReques } else { data.ComputeEnvironments = flattenComputeEnvironments(ctx, jobQueue.ComputeEnvironmentOrder) } + data.JobQueueARN = fwflex.StringToFrameworkLegacy(ctx, jobQueue.JobQueueArn) data.JobQueueName = fwflex.StringToFramework(ctx, jobQueue.JobQueueName) + response.Diagnostics.Append(fwflex.Flatten(ctx, jobQueue.JobStateTimeLimitActions, &data.JobStateTimeLimitActions)...) + if response.Diagnostics.HasError() { + return + } data.Priority = fwflex.Int32ToFrameworkLegacy(ctx, jobQueue.Priority) data.SchedulingPolicyARN = fwflex.StringToFrameworkARN(ctx, jobQueue.SchedulingPolicyArn) data.State = fwflex.StringValueToFramework(ctx, jobQueue.State) @@ -252,6 +286,14 @@ func (r *jobQueueResource) Update(ctx context.Context, request resource.UpdateRe update = true } } + + if !new.JobStateTimeLimitActions.Equal(old.JobStateTimeLimitActions) { + response.Diagnostics.Append(fwflex.Expand(ctx, new.JobStateTimeLimitActions, &input.JobStateTimeLimitActions)...) + if response.Diagnostics.HasError() { + return + } + update = true + } if !new.Priority.Equal(old.Priority) { input.Priority = fwflex.Int32FromFramework(ctx, new.Priority) update = true @@ -497,17 +539,18 @@ func waitJobQueueDeleted(ctx context.Context, conn *batch.Client, id string, tim } type jobQueueResourceModel struct { - ComputeEnvironments types.List `tfsdk:"compute_environments"` - ComputeEnvironmentOrder fwtypes.ListNestedObjectValueOf[computeEnvironmentOrderModel] `tfsdk:"compute_environment_order"` - ID types.String `tfsdk:"id"` - JobQueueARN types.String `tfsdk:"arn"` - JobQueueName types.String `tfsdk:"name"` - Priority types.Int64 `tfsdk:"priority"` - SchedulingPolicyARN fwtypes.ARN `tfsdk:"scheduling_policy_arn"` - State types.String `tfsdk:"state"` - Tags types.Map `tfsdk:"tags"` - TagsAll types.Map `tfsdk:"tags_all"` - Timeouts timeouts.Value `tfsdk:"timeouts"` + ComputeEnvironments types.List `tfsdk:"compute_environments"` + ComputeEnvironmentOrder fwtypes.ListNestedObjectValueOf[computeEnvironmentOrderModel] `tfsdk:"compute_environment_order"` + ID types.String `tfsdk:"id"` + JobQueueARN types.String `tfsdk:"arn"` + JobQueueName types.String `tfsdk:"name"` + JobStateTimeLimitActions fwtypes.ListNestedObjectValueOf[jobStateTimeLimitActionModel] `tfsdk:"job_state_time_limit_action"` + Priority types.Int64 `tfsdk:"priority"` + SchedulingPolicyARN fwtypes.ARN `tfsdk:"scheduling_policy_arn"` + State types.String `tfsdk:"state"` + Tags types.Map `tfsdk:"tags"` + TagsAll types.Map `tfsdk:"tags_all"` + Timeouts timeouts.Value `tfsdk:"timeouts"` } func (model *jobQueueResourceModel) InitFromID() error { @@ -525,6 +568,13 @@ type computeEnvironmentOrderModel struct { Order types.Int64 `tfsdk:"order"` } +type jobStateTimeLimitActionModel struct { + Action fwtypes.StringEnum[awstypes.JobStateTimeLimitActionsAction] `tfsdk:"action"` + MaxTimeSeconds types.Int64 `tfsdk:"max_time_seconds"` + Reason types.String `tfsdk:"reason"` + State fwtypes.StringEnum[awstypes.JobStateTimeLimitActionsState] `tfsdk:"state"` +} + func expandComputeEnvironments(ctx context.Context, tfList types.List) []awstypes.ComputeEnvironmentOrder { var apiObjects []awstypes.ComputeEnvironmentOrder diff --git a/internal/service/batch/job_queue_data_source.go b/internal/service/batch/job_queue_data_source.go index c79e05a0a548..310e81b54f2e 100644 --- a/internal/service/batch/job_queue_data_source.go +++ b/internal/service/batch/job_queue_data_source.go @@ -43,6 +43,30 @@ func dataSourceJobQueue() *schema.Resource { }, }, }, + "job_state_time_limit_action": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + names.AttrAction: { + Type: schema.TypeString, + Computed: true, + }, + "max_time_seconds": { + Type: schema.TypeInt, + Computed: true, + }, + "reason": { + Type: schema.TypeString, + Computed: true, + }, + names.AttrState: { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, names.AttrName: { Type: schema.TypeString, Required: true, @@ -103,6 +127,19 @@ func dataSourceJobQueueRead(ctx context.Context, d *schema.ResourceData, meta in return sdkdiag.AppendErrorf(diags, "setting compute_environment_order: %s", err) } + tfList = make([]interface{}, 0) + for _, apiObject := range jobQueue.JobStateTimeLimitActions { + tfMap := map[string]interface{}{} + tfMap[names.AttrAction] = apiObject.Action + tfMap["max_time_seconds"] = aws.ToInt32(apiObject.MaxTimeSeconds) + tfMap["reason"] = aws.ToString(apiObject.Reason) + tfMap[names.AttrState] = apiObject.State + tfList = append(tfList, tfMap) + } + if err := d.Set("job_state_time_limit_action", tfList); err != nil { + return sdkdiag.AppendErrorf(diags, "setting job_state_time_limit_action: %s", err) + } + setTagsOut(ctx, jobQueue.Tags) return diags diff --git a/internal/service/batch/job_queue_data_source_test.go b/internal/service/batch/job_queue_data_source_test.go index dd9467c94bba..dd11bf1bde53 100644 --- a/internal/service/batch/job_queue_data_source_test.go +++ b/internal/service/batch/job_queue_data_source_test.go @@ -63,6 +63,7 @@ func TestAccBatchJobQueueDataSource_schedulingPolicy(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(dataSourceName, names.AttrARN, resourceName, names.AttrARN), resource.TestCheckResourceAttrPair(dataSourceName, "compute_environment_order.#", resourceName, "compute_environments.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "job_state_time_limit_action.#", resourceName, "job_state_time_limit_action.#"), resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, names.AttrName), resource.TestCheckResourceAttrPair(dataSourceName, names.AttrPriority, resourceName, names.AttrPriority), resource.TestCheckResourceAttrPair(dataSourceName, "scheduling_policy_arn", resourceName, "scheduling_policy_arn"), @@ -76,123 +77,22 @@ func TestAccBatchJobQueueDataSource_schedulingPolicy(t *testing.T) { }) } -func testAccJobQueueDataSourceConfigBase(rName string) string { - return fmt.Sprintf(` -data "aws_partition" "current" {} - -resource "aws_iam_role" "ecs_instance_role" { - name = "ecs_%[1]s" - - assume_role_policy = <