diff --git a/.changelog/33577.txt b/.changelog/33577.txt new file mode 100644 index 00000000000..cfa7d0bff6e --- /dev/null +++ b/.changelog/33577.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_batch_job_queue: Correctly validates elements of `compute_environments` as ARNs +``` diff --git a/internal/service/batch/job_queue.go b/internal/service/batch/job_queue.go index 245721c2260..0d922f48aad 100644 --- a/internal/service/batch/job_queue.go +++ b/internal/service/batch/job_queue.go @@ -65,7 +65,7 @@ func (r *resourceJobQueue) Schema(ctx context.Context, request resource.SchemaRe Attributes: map[string]schema.Attribute{ "arn": framework.ARNAttributeComputedOnly(), "compute_environments": schema.ListAttribute{ - ElementType: types.StringType, + ElementType: fwtypes.ARNType, Required: true, }, "id": framework.IDAttribute(), @@ -365,6 +365,7 @@ func (r *resourceJobQueueData) refreshFromOutput(ctx context.Context, out *batch return diags } + func expandComputeEnvironmentOrder(order []string) (envs []*batch.ComputeEnvironmentOrder) { for i, env := range order { envs = append(envs, &batch.ComputeEnvironmentOrder{ diff --git a/internal/service/batch/job_queue_test.go b/internal/service/batch/job_queue_test.go index 4d665de3b2a..53c98bd1918 100644 --- a/internal/service/batch/job_queue_test.go +++ b/internal/service/batch/job_queue_test.go @@ -39,6 +39,7 @@ func TestAccBatchJobQueue_basic(t *testing.T) { testAccCheckJobQueueExists(ctx, resourceName, &jobQueue1), acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "batch", fmt.Sprintf("job-queue/%s", rName)), resource.TestCheckResourceAttr(resourceName, "compute_environments.#", "1"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.0", "aws_batch_compute_environment.test", "arn"), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "priority", "1"), resource.TestCheckResourceAttr(resourceName, "state", batch.JQStateEnabled), @@ -111,6 +112,52 @@ func TestAccBatchJobQueue_MigrateFromPluginSDK(t *testing.T) { }) } +func TestAccBatchJobQueue_ComputeEnvironments_multiple(t *testing.T) { + ctx := acctest.Context(t) + var jobQueue1 batch.JobQueueDetail + resourceName := "aws_batch_job_queue.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, batch.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckJobQueueDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccJobQueueConfig_ComputeEnvironments_multiple(rName, batch.JQStateEnabled), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckJobQueueExists(ctx, resourceName, &jobQueue1), + resource.TestCheckResourceAttr(resourceName, "compute_environments.#", "3"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.0", "aws_batch_compute_environment.test", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.1", "aws_batch_compute_environment.more.0", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.2", "aws_batch_compute_environment.more.1", "arn"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccJobQueueConfig_ComputeEnvironments_multipleReorder(rName, batch.JQStateEnabled), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckJobQueueExists(ctx, resourceName, &jobQueue1), + resource.TestCheckResourceAttr(resourceName, "compute_environments.#", "3"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.0", "aws_batch_compute_environment.more.0", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.1", "aws_batch_compute_environment.test", "arn"), + resource.TestCheckResourceAttrPair(resourceName, "compute_environments.2", "aws_batch_compute_environment.more.1", "arn"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + // Reference: https://github.com/hashicorp/terraform-provider-aws/issues/8083 func TestAccBatchJobQueue_ComputeEnvironments_externalOrderUpdate(t *testing.T) { ctx := acctest.Context(t) @@ -550,6 +597,79 @@ resource "aws_batch_job_queue" "test" { `, rName, state)) } +func testAccJobQueueConfig_ComputeEnvironments_multiple(rName string, state string) string { + return acctest.ConfigCompose( + testAccJobQueueConfigBase(rName), + fmt.Sprintf(` +resource "aws_batch_job_queue" "test" { + compute_environments = concat( + [aws_batch_compute_environment.test.arn], + aws_batch_compute_environment.more[*].arn, + ) + name = %[1]q + priority = 1 + state = %[2]q +} + +resource "aws_batch_compute_environment" "more" { + count = 2 + + compute_environment_name = "%[1]s-${count.index + 1}" + service_role = aws_iam_role.test.arn + type = "MANAGED" + + compute_resources { + instance_role = aws_iam_instance_profile.ecs_instance_role.arn + instance_type = ["c5", "m5", "r5"] + max_vcpus = 1 + min_vcpus = 0 + security_group_ids = [aws_security_group.test.id] + subnets = [aws_subnet.test.id] + type = "EC2" + } + + depends_on = [aws_iam_role_policy_attachment.test] +} +`, rName, state)) +} + +func testAccJobQueueConfig_ComputeEnvironments_multipleReorder(rName string, state string) string { + return acctest.ConfigCompose( + testAccJobQueueConfigBase(rName), + fmt.Sprintf(` +resource "aws_batch_job_queue" "test" { + compute_environments = [ + aws_batch_compute_environment.more[0].arn, + aws_batch_compute_environment.test.arn, + aws_batch_compute_environment.more[1].arn, + ] + name = %[1]q + priority = 1 + state = %[2]q +} + +resource "aws_batch_compute_environment" "more" { + count = 2 + + compute_environment_name = "%[1]s-${count.index + 1}" + service_role = aws_iam_role.test.arn + type = "MANAGED" + + compute_resources { + instance_role = aws_iam_instance_profile.ecs_instance_role.arn + instance_type = ["c5", "m5", "r5"] + max_vcpus = 1 + min_vcpus = 0 + security_group_ids = [aws_security_group.test.id] + subnets = [aws_subnet.test.id] + type = "EC2" + } + + depends_on = [aws_iam_role_policy_attachment.test] +} +`, rName, state)) +} + func testAccJobQueueConfig_tags1(rName, tagKey1, tagValue1 string) string { return acctest.ConfigCompose( testAccJobQueueConfigBase(rName), diff --git a/website/docs/r/batch_job_queue.html.markdown b/website/docs/r/batch_job_queue.html.markdown index 40871cc32bf..af63e39fe28 100644 --- a/website/docs/r/batch_job_queue.html.markdown +++ b/website/docs/r/batch_job_queue.html.markdown @@ -62,9 +62,8 @@ resource "aws_batch_job_queue" "example" { This resource supports the following arguments: * `name` - (Required) Specifies the name of the job queue. -* `compute_environments` - (Required) Specifies the set of compute environments - mapped to a job queue and their order. The position of the compute environments - in the list will dictate the order. +* `compute_environments` - (Required) List of compute environment ARNs mapped to a job queue. + The position of the compute environments in the list will dictate the order. * `priority` - (Required) The priority of the job queue. Job queues with a higher priority are evaluated first when associated with the same compute environment. * `scheduling_policy_arn` - (Optional) The ARN of the fair share scheduling policy. If this parameter is specified, the job queue uses a fair share scheduling policy. If this parameter isn't specified, the job queue uses a first in, first out (FIFO) scheduling policy. After a job queue is created, you can replace but can't remove the fair share scheduling policy.