Skip to content

Commit 724f799

Browse files
authored
Merge pull request #45401 from hashicorp/add-batch-job-definition-list-resource
New list resource: `aws_batch_job_definition`
2 parents ebeef46 + 0faac4a commit 724f799

File tree

7 files changed

+237
-0
lines changed

7 files changed

+237
-0
lines changed

.changelog/45401.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-list-resource
2+
aws_batch_job_definition
3+
```

internal/service/batch/job_definition.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import (
1515
"github.com/aws/aws-sdk-go-v2/service/batch"
1616
awstypes "github.com/aws/aws-sdk-go-v2/service/batch/types"
1717
"github.com/hashicorp/go-cty/cty"
18+
"github.com/hashicorp/terraform-plugin-framework/list"
19+
listschema "github.com/hashicorp/terraform-plugin-framework/list/schema"
20+
"github.com/hashicorp/terraform-plugin-log/tflog"
1821
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1922
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2023
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -23,12 +26,16 @@ import (
2326
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2427
"github.com/hashicorp/terraform-provider-aws/internal/enum"
2528
"github.com/hashicorp/terraform-provider-aws/internal/errs"
29+
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
2630
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
2731
"github.com/hashicorp/terraform-provider-aws/internal/flex"
32+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
33+
"github.com/hashicorp/terraform-provider-aws/internal/logging"
2834
"github.com/hashicorp/terraform-provider-aws/internal/provider/sdkv2/importer"
2935
"github.com/hashicorp/terraform-provider-aws/internal/sdkv2"
3036
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
3137
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
38+
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
3239
"github.com/hashicorp/terraform-provider-aws/names"
3340
)
3441

@@ -1759,3 +1766,92 @@ func flattenEKSVolumes(apiObjects []awstypes.EksVolume) []any {
17591766

17601767
return tfList
17611768
}
1769+
1770+
// @SDKListResource("aws_batch_job_definition")
1771+
func jobDefinitionResourceAsListResource() inttypes.ListResourceForSDK {
1772+
l := jobDefinitionListResource{}
1773+
l.SetResourceSchema(resourceJobDefinition())
1774+
return &l
1775+
}
1776+
1777+
type jobDefinitionListResource struct {
1778+
framework.ResourceWithConfigure
1779+
framework.ListResourceWithSDKv2Resource
1780+
framework.ListResourceWithSDKv2Tags
1781+
}
1782+
1783+
type jobDefinitionListResourceModel struct {
1784+
framework.WithRegionModel
1785+
}
1786+
1787+
func (l *jobDefinitionListResource) ListResourceConfigSchema(ctx context.Context, request list.ListResourceSchemaRequest, response *list.ListResourceSchemaResponse) {
1788+
response.Schema = listschema.Schema{
1789+
Attributes: map[string]listschema.Attribute{},
1790+
Blocks: map[string]listschema.Block{},
1791+
}
1792+
}
1793+
1794+
func (l *jobDefinitionListResource) List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream) {
1795+
awsClient := l.Meta()
1796+
conn := awsClient.BatchClient(ctx)
1797+
1798+
var query jobDefinitionListResourceModel
1799+
if request.Config.Raw.IsKnown() && !request.Config.Raw.IsNull() {
1800+
if diags := request.Config.Get(ctx, &query); diags.HasError() {
1801+
stream.Results = list.ListResultsStreamDiagnostics(diags)
1802+
return
1803+
}
1804+
}
1805+
1806+
var input batch.DescribeJobDefinitionsInput
1807+
1808+
tflog.Info(ctx, "Listing Batch job definitions")
1809+
1810+
stream.Results = func(yield func(list.ListResult) bool) {
1811+
pages := batch.NewDescribeJobDefinitionsPaginator(conn, &input)
1812+
for pages.HasMorePages() {
1813+
page, err := pages.NextPage(ctx)
1814+
if err != nil {
1815+
result := fwdiag.NewListResultErrorDiagnostic(err)
1816+
yield(result)
1817+
return
1818+
}
1819+
1820+
for _, jobDef := range page.JobDefinitions {
1821+
arn := aws.ToString(jobDef.JobDefinitionArn)
1822+
ctx := tflog.SetField(ctx, logging.ResourceAttributeKey(names.AttrID), arn)
1823+
1824+
result := request.NewListResult(ctx)
1825+
rd := l.ResourceData()
1826+
rd.SetId(arn)
1827+
1828+
tflog.Info(ctx, "Reading Batch job definition")
1829+
diags := resourceJobDefinitionRead(ctx, rd, awsClient)
1830+
if diags.HasError() {
1831+
result = fwdiag.NewListResultErrorDiagnostic(fmt.Errorf("reading Batch job definition %s", arn))
1832+
yield(result)
1833+
return
1834+
}
1835+
1836+
err = l.SetTags(ctx, awsClient, rd)
1837+
if err != nil {
1838+
result = fwdiag.NewListResultErrorDiagnostic(err)
1839+
yield(result)
1840+
return
1841+
}
1842+
1843+
result.DisplayName = aws.ToString(jobDef.JobDefinitionName)
1844+
1845+
l.SetResult(ctx, awsClient, request.IncludeResource, &result, rd)
1846+
if result.Diagnostics.HasError() {
1847+
yield(result)
1848+
return
1849+
}
1850+
1851+
if !yield(result) {
1852+
return
1853+
}
1854+
}
1855+
}
1856+
}
1857+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package batch_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/config"
10+
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
13+
"github.com/hashicorp/terraform-plugin-testing/querycheck"
14+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
15+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
16+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
17+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
18+
tfknownvalue "github.com/hashicorp/terraform-provider-aws/internal/acctest/knownvalue"
19+
"github.com/hashicorp/terraform-provider-aws/names"
20+
)
21+
22+
func TestAccBatchJobDefinition_List_basic(t *testing.T) {
23+
ctx := acctest.Context(t)
24+
25+
resourceName1 := "aws_batch_job_definition.test[0]"
26+
resourceName2 := "aws_batch_job_definition.test[1]"
27+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
28+
29+
acctest.ParallelTest(ctx, t, resource.TestCase{
30+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
31+
tfversion.SkipBelow(tfversion.Version1_14_0),
32+
},
33+
PreCheck: func() { acctest.PreCheck(ctx, t) },
34+
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
35+
CheckDestroy: testAccCheckJobDefinitionDestroy(ctx),
36+
Steps: []resource.TestStep{
37+
// Step 1: Setup
38+
{
39+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
40+
ConfigDirectory: config.StaticDirectory("testdata/JobDefinition/list_basic/"),
41+
ConfigVariables: config.Variables{
42+
acctest.CtRName: config.StringVariable(rName),
43+
},
44+
ConfigStateChecks: []statecheck.StateCheck{
45+
statecheck.ExpectKnownValue(resourceName1, tfjsonpath.New(names.AttrARN), tfknownvalue.RegionalARNExact("batch", "job-definition/"+rName+"-0:1")),
46+
statecheck.ExpectKnownValue(resourceName2, tfjsonpath.New(names.AttrARN), tfknownvalue.RegionalARNExact("batch", "job-definition/"+rName+"-1:1")),
47+
},
48+
},
49+
50+
// Step 2: Query
51+
{
52+
Query: true,
53+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
54+
ConfigDirectory: config.StaticDirectory("testdata/JobDefinition/list_basic/"),
55+
ConfigVariables: config.Variables{
56+
acctest.CtRName: config.StringVariable(rName),
57+
},
58+
QueryResultChecks: []querycheck.QueryResultCheck{
59+
querycheck.ExpectIdentity("aws_batch_job_definition.test", map[string]knownvalue.Check{
60+
names.AttrARN: knownvalue.NotNull(),
61+
}),
62+
63+
querycheck.ExpectIdentity("aws_batch_job_definition.test", map[string]knownvalue.Check{
64+
names.AttrARN: knownvalue.NotNull(),
65+
}),
66+
},
67+
},
68+
},
69+
})
70+
}

internal/service/batch/service_package_gen.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
list "aws_batch_job_definition" "test" {
5+
provider = aws
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "aws_batch_job_definition" "test" {
5+
count = 2
6+
7+
name = "${var.rName}-${count.index}"
8+
type = "container"
9+
container_properties = jsonencode({
10+
image = "busybox"
11+
vcpus = 1
12+
memory = 128
13+
})
14+
}
15+
16+
variable "rName" {
17+
description = "Name for resource"
18+
type = string
19+
nullable = false
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
subcategory: "Batch"
3+
layout: "aws"
4+
page_title: "AWS: aws_batch_job_definition"
5+
description: |-
6+
Lists Batch Job Definition resources.
7+
---
8+
9+
# List Resource: aws_batch_job_definition
10+
11+
Lists Batch Job Definition resources.
12+
13+
## Example Usage
14+
15+
```terraform
16+
list "aws_batch_job_definition" "example" {
17+
provider = aws
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
This list resource supports the following arguments:
24+
25+
* `region` - (Optional) Region to query. Defaults to provider region.

0 commit comments

Comments
 (0)