Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_batch_job_definition: Ensure that any newly created revision is tagged correctly #39797

Merged
merged 9 commits into from
Oct 21, 2024
3 changes: 3 additions & 0 deletions .changelog/39797.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_batch_job_definition: Ensure that new revisions are created with tags
```
1 change: 1 addition & 0 deletions internal/service/batch/job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ func resourceJobDefinitionUpdate(ctx context.Context, d *schema.ResourceData, me
jobDefinitionType := awstypes.JobDefinitionType(d.Get(names.AttrType).(string))
input := &batch.RegisterJobDefinitionInput{
JobDefinitionName: aws.String(name),
Tags: getTagsIn(ctx),
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
Type: jobDefinitionType,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ resource "aws_batch_job_definition" "test" {

func testAccJobDefinitionDataSourceConfig_basicARNNode(rName string) string {
return acctest.ConfigCompose(
testAccJobDefinitionConfig_NodeProperties(rName), `
testAccJobDefinitionConfig_nodeProperties(rName), `
data "aws_batch_job_definition" "test" {
arn = aws_batch_job_definition.test.arn
}`)
Expand Down
140 changes: 120 additions & 20 deletions internal/service/batch/job_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
awstypes "github.com/aws/aws-sdk-go-v2/service/batch/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfbatch "github.com/hashicorp/terraform-provider-aws/internal/service/batch"
Expand All @@ -37,7 +41,7 @@ func TestAccBatchJobDefinition_basic(t *testing.T) {
CheckDestroy: testAccCheckJobDefinitionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccJobDefinitionConfig_name(rName),
Config: testAccJobDefinitionConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "batch", regexache.MustCompile(fmt.Sprintf(`job-definition/%s:\d+`, rName))),
Expand Down Expand Up @@ -122,7 +126,7 @@ func TestAccBatchJobDefinition_attributes(t *testing.T) {
),
},
{
Config: testAccJobDefinitionConfig_name(rName),
Config: testAccJobDefinitionConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
testAccCheckJobDefinitionPreviousDeregistered(ctx, resourceName),
Expand Down Expand Up @@ -185,7 +189,7 @@ func TestAccBatchJobDefinition_disappears(t *testing.T) {
CheckDestroy: testAccCheckJobDefinitionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccJobDefinitionConfig_name(rName),
Config: testAccJobDefinitionConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfbatch.ResourceJobDefinition(), resourceName),
Expand Down Expand Up @@ -589,7 +593,7 @@ func TestAccBatchJobDefinition_NodeProperties_basic(t *testing.T) {
CheckDestroy: testAccCheckJobDefinitionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccJobDefinitionConfig_NodeProperties(rName),
Config: testAccJobDefinitionConfig_nodeProperties(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "batch", regexache.MustCompile(fmt.Sprintf(`job-definition/%s:\d+`, rName))),
Expand Down Expand Up @@ -1014,6 +1018,64 @@ func TestAccBatchJobDefinition_ECSProperties_update(t *testing.T) {
})
}

func TestAccBatchJobDefinition_updateWithTags(t *testing.T) {
ctx := acctest.Context(t)
var jd awstypes.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, names.BatchServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckJobDefinitionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccJobDefinitionConfig_simpleWithTags(rName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrName), knownvalue.StringExact(rName)),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTags), knownvalue.MapExact(map[string]knownvalue.Check{
"Name": knownvalue.StringExact(rName),
})),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTagsAll), knownvalue.MapExact(map[string]knownvalue.Check{
"Name": knownvalue.StringExact(rName),
})),
},
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
),
},
// Ensure that tags are put on the new revision.
{
Config: testAccJobDefinitionConfig_simpleWithTagsUpdate(rName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectUnknownValue(resourceName, tfjsonpath.New("revision")),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrName), knownvalue.StringExact(rName)),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTags), knownvalue.MapExact(map[string]knownvalue.Check{
"Name": knownvalue.StringExact(rName),
})),
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTagsAll), knownvalue.MapExact(map[string]knownvalue.Check{
"Name": knownvalue.StringExact(rName),
})),
},
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
),
},
},
})
}

func testAccCheckJobDefinitionExists(ctx context.Context, n string, v *awstypes.JobDefinition) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1156,6 +1218,21 @@ func testAccCheckJobDefinitionDestroy(ctx context.Context) resource.TestCheckFun
}
}

func testAccJobDefinitionConfig_basic(rName string) 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"
}
`, rName)
}

func testAccJobDefinitionConfig_containerPropertiesAdvanced(rName, param string, retries, timeout int) string {
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
Expand Down Expand Up @@ -1332,21 +1409,6 @@ resource "aws_batch_job_definition" "test" {
`, rName, subcommand))
}

func testAccJobDefinitionConfig_name(rName string) 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"
}
`, rName)
}

func testAccJobDefinitionConfig_capabilitiesEC2(rName string) string {
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
Expand Down Expand Up @@ -1510,7 +1572,7 @@ resource "aws_batch_job_definition" "test" {
`, rName)
}

func testAccJobDefinitionConfig_NodeProperties(rName string) string {
func testAccJobDefinitionConfig_nodeProperties(rName string) string {
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
name = %[1]q
Expand Down Expand Up @@ -2144,3 +2206,41 @@ resource "aws_batch_job_definition" "test" {
}
`, rName, acctest.Region())
}

func testAccJobDefinitionConfig_simpleWithTags(rName string) string {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
container_properties = jsonencode({
command = ["echo", "test1"]
image = "busybox"
memory = 128
vcpus = 1
})
name = %[1]q
type = "container"

tags = {
Name = %[1]q
}
}
`, rName)
}

func testAccJobDefinitionConfig_simpleWithTagsUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
container_properties = jsonencode({
command = ["echo", "test2"]
image = "busybox"
memory = 128
vcpus = 1
})
name = %[1]q
type = "container"

tags = {
Name = %[1]q
}
}
`, rName)
}
Loading