Skip to content

Commit

Permalink
migrated ecr
Browse files Browse the repository at this point in the history
  • Loading branch information
alexknez committed Mar 20, 2024
1 parent 0bb9fba commit a92b240
Show file tree
Hide file tree
Showing 28 changed files with 406 additions and 392 deletions.
5 changes: 0 additions & 5 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/generate/listpages/v2/function.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

func {{ .Name }}Pages(ctx context.Context, conn {{ .RecvType }}, input {{ .ParamType }}, fn func({{ .ResultType }}, bool) bool) error {
for {
output, err := conn.{{ .AWSName }}(ctx, input)
Expand Down
18 changes: 8 additions & 10 deletions internal/service/ecr/authorization_token_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -56,21 +56,19 @@ func DataSourceAuthorizationToken() *schema.Resource {

func dataSourceAuthorizationTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECRConn(ctx)
conn := meta.(*conns.AWSClient).ECRClient(ctx)
params := &ecr.GetAuthorizationTokenInput{}
if v, ok := d.GetOk("registry_id"); ok {
params.RegistryIds = []*string{aws.String(v.(string))}
}

log.Printf("[DEBUG] Getting ECR authorization token")
out, err := conn.GetAuthorizationTokenWithContext(ctx, params)
out, err := conn.GetAuthorizationToken(ctx, params)
if err != nil {
return sdkdiag.AppendErrorf(diags, "getting ECR authorization token: %s", err)
}
log.Printf("[DEBUG] Received ECR AuthorizationData %v", out.AuthorizationData)
authorizationData := out.AuthorizationData[0]
authorizationToken := aws.StringValue(authorizationData.AuthorizationToken)
expiresAt := aws.TimeValue(authorizationData.ExpiresAt).Format(time.RFC3339)
proxyEndpoint := aws.StringValue(authorizationData.ProxyEndpoint)
authorizationToken := aws.ToString(authorizationData.AuthorizationToken)
expiresAt := aws.ToTime(authorizationData.ExpiresAt).Format(time.RFC3339)
proxyEndpoint := aws.ToString(authorizationData.ProxyEndpoint)
authBytes, err := itypes.Base64Decode(authorizationToken)
if err != nil {
d.SetId("")
Expand Down
3 changes: 2 additions & 1 deletion internal/service/ecr/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsSlice -UpdateTags -CreateTags
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsSlice -UpdateTags -CreateTags -AWSSDKVersion=2
//go:generate go run ../../generate/listpages/main.go -AWSSDKVersion=2 -ListOps=DescribeImages,DescribeRepositories
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
42 changes: 21 additions & 21 deletions internal/service/ecr/image_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"fmt"
"slices"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecr"
awstypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
)

Expand Down Expand Up @@ -76,14 +77,14 @@ func DataSourceImage() *schema.Resource {

func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECRConn(ctx)
conn := meta.(*conns.AWSClient).ECRClient(ctx)

input := &ecr.DescribeImagesInput{
RepositoryName: aws.String(d.Get("repository_name").(string)),
}

if v, ok := d.GetOk("image_digest"); ok {
input.ImageIds = []*ecr.ImageIdentifier{
input.ImageIds = []awstypes.ImageIdentifier{
{
ImageDigest: aws.String(v.(string)),
},
Expand All @@ -92,7 +93,7 @@ func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta inter

if v, ok := d.GetOk("image_tag"); ok {
if len(input.ImageIds) == 0 {
input.ImageIds = []*ecr.ImageIdentifier{
input.ImageIds = []awstypes.ImageIdentifier{
{
ImageTag: aws.String(v.(string)),
},
Expand Down Expand Up @@ -121,11 +122,11 @@ func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta inter
return sdkdiag.AppendErrorf(diags, "Your query returned more than one result. Please try a more specific search criteria, or set `most_recent` attribute to true.")
}

slices.SortFunc(imageDetails, func(a, b *ecr.ImageDetail) int {
if aws.TimeValue(a.ImagePushedAt).After(aws.TimeValue(b.ImagePushedAt)) {
slices.SortFunc(imageDetails, func(a, b *awstypes.ImageDetail) int {
if aws.ToTime(a.ImagePushedAt).After(aws.ToTime(b.ImagePushedAt)) {
return -1
}
if aws.TimeValue(a.ImagePushedAt).Before(aws.TimeValue(b.ImagePushedAt)) {
if aws.ToTime(a.ImagePushedAt).Before(aws.ToTime(b.ImagePushedAt)) {
return 1
}
return 0
Expand All @@ -134,9 +135,9 @@ func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta inter

imageDetail := imageDetails[0]

repositoryName := aws.StringValue(imageDetail.RepositoryName)
repositoryName := aws.ToString(imageDetail.RepositoryName)
repositoryInput := &ecr.DescribeRepositoriesInput{
RepositoryNames: aws.StringSlice([]string{repositoryName}),
RepositoryNames: []string{repositoryName},
RegistryId: imageDetail.RegistryId,
}

Expand All @@ -146,36 +147,35 @@ func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta inter
return sdkdiag.AppendErrorf(diags, "reading ECR Images: %s", err)
}

d.SetId(aws.StringValue(imageDetail.ImageDigest))
d.SetId(aws.ToString(imageDetail.ImageDigest))
d.Set("image_digest", imageDetail.ImageDigest)
d.Set("image_pushed_at", imageDetail.ImagePushedAt.Unix())
d.Set("image_size_in_bytes", imageDetail.ImageSizeInBytes)
d.Set("image_tags", aws.StringValueSlice(imageDetail.ImageTags))
d.Set("image_uri", fmt.Sprintf("%s@%s", aws.StringValue(repository.RepositoryUri), aws.StringValue(imageDetail.ImageDigest)))
d.Set("image_tags", imageDetail.ImageTags)
d.Set("image_uri", fmt.Sprintf("%s@%s", aws.ToString(repository.RepositoryUri), aws.ToString(imageDetail.ImageDigest)))
d.Set("registry_id", imageDetail.RegistryId)
d.Set("repository_name", imageDetail.RepositoryName)

return diags
}

func FindImageDetails(ctx context.Context, conn *ecr.ECR, input *ecr.DescribeImagesInput) ([]*ecr.ImageDetail, error) {
var output []*ecr.ImageDetail
func FindImageDetails(ctx context.Context, conn *ecr.Client, input *ecr.DescribeImagesInput) ([]*awstypes.ImageDetail, error) {
var output []*awstypes.ImageDetail

err := conn.DescribeImagesPagesWithContext(ctx, input, func(page *ecr.DescribeImagesOutput, lastPage bool) bool {
err := describeImagesPages(ctx, conn, input, func(page *ecr.DescribeImagesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.ImageDetails {
if v != nil {
output = append(output, v)
}
imageDetail := v
output = append(output, &imageDetail)
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
if errs.IsA[*awstypes.RepositoryNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand Down
49 changes: 22 additions & 27 deletions internal/service/ecr/lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (
"sort"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecr"
awstypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func ResourceLifecyclePolicy() *schema.Resource {

func resourceLifecyclePolicyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECRConn(ctx)
conn := meta.(*conns.AWSClient).ECRClient(ctx)

policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

Expand All @@ -80,18 +81,18 @@ func resourceLifecyclePolicyCreate(ctx context.Context, d *schema.ResourceData,
LifecyclePolicyText: aws.String(policy),
}

resp, err := conn.PutLifecyclePolicyWithContext(ctx, input)
resp, err := conn.PutLifecyclePolicy(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating ECR Lifecycle Policy (%s): %s", d.Get("repository").(string), err)
}
d.SetId(aws.StringValue(resp.RepositoryName))
d.SetId(aws.ToString(resp.RepositoryName))
d.Set("registry_id", resp.RegistryId)
return append(diags, resourceLifecyclePolicyRead(ctx, d, meta)...)
}

func resourceLifecyclePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECRConn(ctx)
conn := meta.(*conns.AWSClient).ECRClient(ctx)

input := &ecr.GetLifecyclePolicyInput{
RepositoryName: aws.String(d.Id()),
Expand All @@ -102,13 +103,9 @@ func resourceLifecyclePolicyRead(ctx context.Context, d *schema.ResourceData, me
err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
var err error

resp, err = conn.GetLifecyclePolicyWithContext(ctx, input)
resp, err = conn.GetLifecyclePolicy(ctx, input)

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeLifecyclePolicyNotFoundException) {
return retry.RetryableError(err)
}

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
if d.IsNewResource() && (errs.IsA[*awstypes.LifecyclePolicyNotFoundException](err) || errs.IsA[*awstypes.RepositoryNotFoundException](err)) {
return retry.RetryableError(err)
}

Expand All @@ -120,16 +117,16 @@ func resourceLifecyclePolicyRead(ctx context.Context, d *schema.ResourceData, me
})

if tfresource.TimedOut(err) {
resp, err = conn.GetLifecyclePolicyWithContext(ctx, input)
resp, err = conn.GetLifecyclePolicy(ctx, input)
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeLifecyclePolicyNotFoundException) {
if d.IsNewResource() && errs.IsA[*awstypes.LifecyclePolicyNotFoundException](err) {
log.Printf("[WARN] ECR Lifecycle Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
if d.IsNewResource() && errs.IsA[*awstypes.RepositoryNotFoundException](err) {
log.Printf("[WARN] ECR Lifecycle Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
Expand All @@ -146,14 +143,14 @@ func resourceLifecyclePolicyRead(ctx context.Context, d *schema.ResourceData, me
d.Set("repository", resp.RepositoryName)
d.Set("registry_id", resp.RegistryId)

equivalent, err := equivalentLifecyclePolicyJSON(d.Get("policy").(string), aws.StringValue(resp.LifecyclePolicyText))
equivalent, err := equivalentLifecyclePolicyJSON(d.Get("policy").(string), aws.ToString(resp.LifecyclePolicyText))

if err != nil {
return sdkdiag.AppendErrorf(diags, "while comparing policy (state: %s) (from AWS: %s), encountered: %s", d.Get("policy").(string), aws.StringValue(resp.LifecyclePolicyText), err)
return sdkdiag.AppendErrorf(diags, "while comparing policy (state: %s) (from AWS: %s), encountered: %s", d.Get("policy").(string), aws.ToString(resp.LifecyclePolicyText), err)
}

if !equivalent {
policyToSet, err := structure.NormalizeJsonString(aws.StringValue(resp.LifecyclePolicyText))
policyToSet, err := structure.NormalizeJsonString(aws.ToString(resp.LifecyclePolicyText))

if err != nil {
return sdkdiag.AppendErrorf(diags, "policy (%s) is invalid JSON: %s", policyToSet, err)
Expand All @@ -167,20 +164,18 @@ func resourceLifecyclePolicyRead(ctx context.Context, d *schema.ResourceData, me

func resourceLifecyclePolicyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECRConn(ctx)
conn := meta.(*conns.AWSClient).ECRClient(ctx)

input := &ecr.DeleteLifecyclePolicyInput{
RepositoryName: aws.String(d.Id()),
}

_, err := conn.DeleteLifecyclePolicyWithContext(ctx, input)
_, err := conn.DeleteLifecyclePolicy(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
return diags
}
if tfawserr.ErrCodeEquals(err, ecr.ErrCodeLifecyclePolicyNotFoundException) {
if d.IsNewResource() && (errs.IsA[*awstypes.LifecyclePolicyNotFoundException](err) || errs.IsA[*awstypes.RepositoryNotFoundException](err)) {
return diags
}

return sdkdiag.AppendErrorf(diags, "deleting ECR Lifecycle Policy (%s): %s", d.Id(), err)
}

Expand Down Expand Up @@ -212,7 +207,7 @@ type lifecyclePolicy struct {

func (lp *lifecyclePolicy) reduce() {
sort.Slice(lp.Rules, func(i, j int) bool {
return aws.Int64Value(lp.Rules[i].RulePriority) < aws.Int64Value(lp.Rules[j].RulePriority)
return aws.ToInt64(lp.Rules[i].RulePriority) < aws.ToInt64(lp.Rules[j].RulePriority)
})

for _, rule := range lp.Rules {
Expand All @@ -222,7 +217,7 @@ func (lp *lifecyclePolicy) reduce() {

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

if len(lprs.TagPrefixList) == 0 {
Expand Down
20 changes: 9 additions & 11 deletions internal/service/ecr/lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecr"
awstypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func TestAccECRLifecyclePolicy_detectDiff(t *testing.T) {

func testAccCheckLifecyclePolicyDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ECRConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).ECRClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ecr_lifecycle_policy" {
Expand All @@ -110,12 +111,9 @@ func testAccCheckLifecyclePolicyDestroy(ctx context.Context) resource.TestCheckF
RepositoryName: aws.String(rs.Primary.ID),
}

_, err := conn.GetLifecyclePolicyWithContext(ctx, input)
_, err := conn.GetLifecyclePolicy(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
return nil
}
if tfawserr.ErrCodeEquals(err, ecr.ErrCodeLifecyclePolicyNotFoundException) {
if errs.IsA[*awstypes.RepositoryNotFoundException](err) || errs.IsA[*awstypes.LifecyclePolicyNotFoundException](err) {
return nil
}
return err
Expand All @@ -133,13 +131,13 @@ func testAccCheckLifecyclePolicyExists(ctx context.Context, name string) resourc
return fmt.Errorf("Not found: %s", name)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).ECRConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).ECRClient(ctx)

input := &ecr.GetLifecyclePolicyInput{
RepositoryName: aws.String(rs.Primary.ID),
}

_, err := conn.GetLifecyclePolicyWithContext(ctx, input)
_, err := conn.GetLifecyclePolicy(ctx, input)
return err
}
}
Expand Down
Loading

0 comments on commit a92b240

Please sign in to comment.