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

fix: Add missing aws_ecr_lifecycle_policy tagPatternList change detection #35231

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/35231.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ecr_lifecycle_policy: Add missing `tagPatternList` change detection in policy JSON
```
19 changes: 14 additions & 5 deletions internal/service/ecr/lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ func resourceLifecyclePolicyDelete(ctx context.Context, d *schema.ResourceData,
}

type lifecyclePolicyRuleSelection struct {
TagStatus *string `locationName:"tagStatus" type:"string" enum:"tagStatus" required:"true"`
TagPrefixList []*string `locationName:"tagPrefixList" type:"list"`
CountType *string `locationName:"countType" type:"string" enum:"countType" required:"true"`
CountUnit *string `locationName:"countUnit" type:"string" enum:"countType"`
CountNumber *int64 `locationName:"countNumber" min:"1" type:"integer"`
TagStatus *string `locationName:"tagStatus" type:"string" enum:"tagStatus" required:"true"`
TagPatternList []*string `locationName:"tagPatternList" type:"list"`
TagPrefixList []*string `locationName:"tagPrefixList" type:"list"`
CountType *string `locationName:"countType" type:"string" enum:"countType" required:"true"`
CountUnit *string `locationName:"countUnit" type:"string" enum:"countType"`
CountNumber *int64 `locationName:"countNumber" min:"1" type:"integer"`
}

type lifecyclePolicyRuleAction struct {
Expand Down Expand Up @@ -221,6 +222,14 @@ func (lp *lifecyclePolicy) reduce() {
}

func (lprs *lifecyclePolicyRuleSelection) reduce() {
sort.Slice(lprs.TagPatternList, func(i, j int) bool {
return aws.StringValue(lprs.TagPatternList[i]) < aws.StringValue(lprs.TagPatternList[j])
})

if len(lprs.TagPatternList) == 0 {
lprs.TagPatternList = nil
}

sort.Slice(lprs.TagPrefixList, func(i, j int) bool {
return aws.StringValue(lprs.TagPrefixList[i]) < aws.StringValue(lprs.TagPrefixList[j])
})
Expand Down
92 changes: 92 additions & 0 deletions internal/service/ecr/lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ func testAccCheckLifecyclePolicyExists(ctx context.Context, name string) resourc
}
}

func TestAccECRLifecyclePolicy_detectTagPatternListDiff(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_ecr_lifecycle_policy.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ecr.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckLifecyclePolicyDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccLifecyclePolicyConfig_tagPatternList(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLifecyclePolicyExists(ctx, resourceName),
),
},
{
Config: testAccLifecyclePolicyConfig_tagPatternListChanged(rName),
ExpectNonEmptyPlan: true,
PlanOnly: true,
},
},
})
}

func testAccLifecyclePolicyConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_ecr_repository" "test" {
Expand Down Expand Up @@ -302,3 +328,69 @@ resource "aws_ecr_lifecycle_policy" "test" {
}
`, rName)
}

func testAccLifecyclePolicyConfig_tagPatternList(rName string) string {
return fmt.Sprintf(`
resource "aws_ecr_repository" "test" {
name = "%s"
}

resource "aws_ecr_lifecycle_policy" "test" {
repository = aws_ecr_repository.test.name

policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Expire tagged images older than 14 days"
selection = {
tagStatus = "tagged"
tagPatternList = [
"alpha-*"
]
countType = "sinceImagePushed"
countUnit = "days"
countNumber = 14
}
action = {
type = "expire"
}
}
]
})
}
`, rName)
}

func testAccLifecyclePolicyConfig_tagPatternListChanged(rName string) string {
return fmt.Sprintf(`
resource "aws_ecr_repository" "test" {
name = "%s"
}

resource "aws_ecr_lifecycle_policy" "test" {
repository = aws_ecr_repository.test.name

policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Expire tagged images older than 14 days"
selection = {
tagStatus = "tagged"
tagPatternList = [
"beta-*"
]
countType = "sinceImagePushed"
countUnit = "days"
countNumber = 14
}
action = {
type = "expire"
}
}
]
})
}
`, rName)
}
Loading