Skip to content

Commit

Permalink
fix: Add missing aws_ecr_lifecycle_policy tagPatternList change
Browse files Browse the repository at this point in the history
detection
  • Loading branch information
acwwat committed Jan 10, 2024
1 parent 6b3bbf6 commit e551dea
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
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)
}

0 comments on commit e551dea

Please sign in to comment.