Skip to content

Commit

Permalink
Merge pull request #36427 from alexknez/feature-migrate-ecr-public-sd…
Browse files Browse the repository at this point in the history
…k-v2

ecrpublic migrated to aws-sdk-v2
  • Loading branch information
johnsonaj committed Mar 20, 2024
2 parents 6ec1be6 + 4dc3cb1 commit 0bb9fba
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 166 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.30.5
github.com/aws/aws-sdk-go-v2/service/ec2 v1.151.1
github.com/aws/aws-sdk-go-v2/service/ecr v1.27.3
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.23.3
github.com/aws/aws-sdk-go-v2/service/ecs v1.41.3
github.com/aws/aws-sdk-go-v2/service/eks v1.41.2
github.com/aws/aws-sdk-go-v2/service/elasticache v1.37.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ github.com/aws/aws-sdk-go-v2/service/ec2 v1.151.1 h1:Ky/RdoVNuWli0Qzvn2q7iXAPJ7L
github.com/aws/aws-sdk-go-v2/service/ec2 v1.151.1/go.mod h1:TeZ9dVQzGaLG+SBIgdLIDbJ6WmfFvksLeG3EHGnNfZM=
github.com/aws/aws-sdk-go-v2/service/ecr v1.27.3 h1:gfgt0D8MGL3gHrJPEv4rcWptA4Nz7uYn25ls8lLiANw=
github.com/aws/aws-sdk-go-v2/service/ecr v1.27.3/go.mod h1:O5Fvd41s5KfDG093xLM7FhGiH6EmhmEli5D5MQH3TWw=
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.23.3 h1:gaq/4fd2/bQeJ33m4csgL7DJHrrmvGhqnrsxchNr46c=
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.23.3/go.mod h1:vn+Rz9fAFGJtDXbBmYdTc71Q8iF/W/uK1/ec93hinD8=
github.com/aws/aws-sdk-go-v2/service/ecs v1.41.3 h1:lMtV6j7HE9vpJ+rCXbjfKYuM0lVQVWOYGn6zxy0OvEQ=
github.com/aws/aws-sdk-go-v2/service/ecs v1.41.3/go.mod h1:7b5ZXNyT7SjZhy+MOuXwL2XtsrFDl1bOL4Mqrgr5c3k=
github.com/aws/aws-sdk-go-v2/service/eks v1.41.2 h1:0X5g5H8YyW9QVtlp6j+ZGHl/h0ZS58jiLRXabyiB5uw=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

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

13 changes: 7 additions & 6 deletions internal/service/ecrpublic/authorization_token_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecrpublic"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
"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 @@ -47,19 +47,20 @@ func DataSourceAuthorizationToken() *schema.Resource {

func dataSourceAuthorizationTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECRPublicConn(ctx)
conn := meta.(*conns.AWSClient).ECRPublicClient(ctx)
params := &ecrpublic.GetAuthorizationTokenInput{}

out, err := conn.GetAuthorizationTokenWithContext(ctx, params)
out, err := conn.GetAuthorizationToken(ctx, params)

if err != nil {
return sdkdiag.AppendErrorf(diags, "getting ECR Public authorization token: %s", err)
}

authorizationData := out.AuthorizationData
authorizationToken := aws.StringValue(authorizationData.AuthorizationToken)
expiresAt := aws.TimeValue(authorizationData.ExpiresAt).Format(time.RFC3339)
authorizationToken := aws.ToString(authorizationData.AuthorizationToken)
expiresAt := aws.ToTime(authorizationData.ExpiresAt).Format(time.RFC3339)
authBytes, err := itypes.Base64Decode(authorizationToken)

if err != nil {
return sdkdiag.AppendErrorf(diags, "decoding ECR Public authorization token: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
Expand All @@ -18,7 +17,7 @@ func TestAccECRPublicAuthorizationTokenDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_ecrpublic_authorization_token.repo"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckRegion(t, endpoints.UsEast1RegionID) },
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckRegion(t, names.USEast1RegionID) },
ErrorCheck: acctest.ErrorCheck(t, names.ECRServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
Expand Down
2 changes: 1 addition & 1 deletion internal/service/ecrpublic/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

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

Expand Down
85 changes: 48 additions & 37 deletions internal/service/ecrpublic/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"time"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecrpublic"
"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/ecrpublic"
awstypes "github.com/aws/aws-sdk-go-v2/service/ecrpublic/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"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
Expand Down Expand Up @@ -126,7 +126,7 @@ func ResourceRepository() *schema.Resource {

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

input := ecrpublic.CreateRepositoryInput{
RepositoryName: aws.String(d.Get("repository_name").(string)),
Expand All @@ -137,7 +137,7 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta
input.CatalogData = expandRepositoryCatalogData(v.([]interface{})[0].(map[string]interface{}))
}

out, err := conn.CreateRepositoryWithContext(ctx, &input)
out, err := conn.CreateRepository(ctx, &input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating ECR Public repository: %s", err)
}
Expand All @@ -148,40 +148,41 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta

repository := out.Repository

log.Printf("[DEBUG] ECR Public repository created: %q", aws.StringValue(repository.RepositoryArn))
log.Printf("[DEBUG] ECR Public repository created: %q", aws.ToString(repository.RepositoryArn))

d.SetId(aws.StringValue(repository.RepositoryName))
d.SetId(aws.ToString(repository.RepositoryName))

return append(diags, resourceRepositoryRead(ctx, d, meta)...)
}

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

log.Printf("[DEBUG] Reading ECR Public repository %s", d.Id())
var out *ecrpublic.DescribeRepositoriesOutput
input := &ecrpublic.DescribeRepositoriesInput{
RepositoryNames: aws.StringSlice([]string{d.Id()}),
RepositoryNames: []string{d.Id()},
}

var err error
err = retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
out, err = conn.DescribeRepositoriesWithContext(ctx, input)
if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException) {
out, err = conn.DescribeRepositories(ctx, input)
if d.IsNewResource() && errs.IsA[*awstypes.RepositoryNotFoundException](err) {
return retry.RetryableError(err)
}

if err != nil {
return retry.NonRetryableError(err)
}
return nil
})

if tfresource.TimedOut(err) {
out, err = conn.DescribeRepositoriesWithContext(ctx, input)
out, err = conn.DescribeRepositories(ctx, input)
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException) {
if !d.IsNewResource() && errs.IsA[*awstypes.RepositoryNotFoundException](err) {
log.Printf("[WARN] ECR Public Repository (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
Expand All @@ -191,7 +192,7 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in
return sdkdiag.AppendErrorf(diags, "reading ECR Public repository: %s", err)
}

if out == nil || len(out.Repositories) == 0 || out.Repositories[0] == nil {
if out == nil || len(out.Repositories) == 0 {
return sdkdiag.AppendErrorf(diags, "reading ECR Public Repository (%s): empty response", d.Id())
}

Expand All @@ -214,7 +215,7 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in
RegistryId: repository.RegistryId,
}

catalogOut, err = conn.GetRepositoryCatalogDataWithContext(ctx, catalogInput)
catalogOut, err = conn.GetRepositoryCatalogData(ctx, catalogInput)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading catalog data for ECR Public repository: %s", err)
Expand All @@ -238,47 +239,49 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in

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

deleteInput := &ecrpublic.DeleteRepositoryInput{
RepositoryName: aws.String(d.Id()),
RegistryId: aws.String(d.Get("registry_id").(string)),
}

if v, ok := d.GetOk("force_destroy"); ok {
deleteInput.Force = aws.Bool(v.(bool))
force := v.(bool)
deleteInput.Force = aws.ToBool(&force)
}

log.Printf("[DEBUG] Deleting ECR Public Repository: (%s)", d.Id())
_, err := conn.DeleteRepositoryWithContext(ctx, deleteInput)
_, err := conn.DeleteRepository(ctx, deleteInput)

if err != nil {
if tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException) {
if errs.IsA[*awstypes.RepositoryNotFoundException](err) {
return diags
}
return sdkdiag.AppendErrorf(diags, "deleting ECR Public repository: %s", err)
}

log.Printf("[DEBUG] Waiting for ECR Public Repository %q to be deleted", d.Id())
input := &ecrpublic.DescribeRepositoriesInput{
RepositoryNames: aws.StringSlice([]string{d.Id()}),
RepositoryNames: []string{d.Id()},
}
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
_, err = conn.DescribeRepositoriesWithContext(ctx, input)
_, err = conn.DescribeRepositories(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException) {
if errs.IsA[*awstypes.RepositoryNotFoundException](err) {
return nil
}
return retry.NonRetryableError(err)
}

return retry.RetryableError(fmt.Errorf("%q: Timeout while waiting for the ECR Public Repository to be deleted", d.Id()))
})

if tfresource.TimedOut(err) {
_, err = conn.DescribeRepositoriesWithContext(ctx, input)
_, err = conn.DescribeRepositories(ctx, input)
}

if tfawserr.ErrCodeEquals(err, ecrpublic.ErrCodeRepositoryNotFoundException) {
if errs.IsA[*awstypes.RepositoryNotFoundException](err) {
return diags
}

Expand All @@ -293,7 +296,7 @@ func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta

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

if d.HasChange("catalog_data") {
if err := resourceRepositoryUpdateCatalogData(ctx, conn, d); err != nil {
Expand All @@ -314,41 +317,45 @@ func flattenRepositoryCatalogData(apiObject *ecrpublic.GetRepositoryCatalogDataO
tfMap := map[string]interface{}{}

if v := catalogData.AboutText; v != nil {
tfMap["about_text"] = aws.StringValue(v)
tfMap["about_text"] = aws.ToString(v)
}

if v := catalogData.Architectures; v != nil {
tfMap["architectures"] = aws.StringValueSlice(v)
tfMap["architectures"] = v
}

if v := catalogData.Description; v != nil {
tfMap["description"] = aws.StringValue(v)
tfMap["description"] = aws.ToString(v)
}

if v := catalogData.OperatingSystems; v != nil {
tfMap["operating_systems"] = aws.StringValueSlice(v)
tfMap["operating_systems"] = v
}

if v := catalogData.UsageText; v != nil {
tfMap["usage_text"] = aws.StringValue(v)
tfMap["usage_text"] = aws.ToString(v)
}

return tfMap
}

func expandRepositoryCatalogData(tfMap map[string]interface{}) *ecrpublic.RepositoryCatalogDataInput {
func expandRepositoryCatalogData(tfMap map[string]interface{}) *awstypes.RepositoryCatalogDataInput {
if tfMap == nil {
return nil
}

repositoryCatalogDataInput := &ecrpublic.RepositoryCatalogDataInput{}
repositoryCatalogDataInput := &awstypes.RepositoryCatalogDataInput{}

if v, ok := tfMap["about_text"].(string); ok && v != "" {
repositoryCatalogDataInput.AboutText = aws.String(v)
}

if v, ok := tfMap["architectures"].(*schema.Set); ok {
repositoryCatalogDataInput.Architectures = flex.ExpandStringSet(v)
architectures := make([]string, v.Len())
for i, val := range v.List() {
architectures[i] = val.(string)
}
repositoryCatalogDataInput.Architectures = architectures
}

if v, ok := tfMap["description"].(string); ok && v != "" {
Expand All @@ -360,7 +367,11 @@ func expandRepositoryCatalogData(tfMap map[string]interface{}) *ecrpublic.Reposi
}

if v, ok := tfMap["operating_systems"].(*schema.Set); ok {
repositoryCatalogDataInput.OperatingSystems = flex.ExpandStringSet(v)
operatingSystems := make([]string, v.Len())
for i, val := range v.List() {
operatingSystems[i] = val.(string)
}
repositoryCatalogDataInput.OperatingSystems = operatingSystems
}

if v, ok := tfMap["usage_text"].(string); ok && v != "" {
Expand All @@ -370,7 +381,7 @@ func expandRepositoryCatalogData(tfMap map[string]interface{}) *ecrpublic.Reposi
return repositoryCatalogDataInput
}

func resourceRepositoryUpdateCatalogData(ctx context.Context, conn *ecrpublic.ECRPublic, d *schema.ResourceData) error {
func resourceRepositoryUpdateCatalogData(ctx context.Context, conn *ecrpublic.Client, d *schema.ResourceData) error {
if d.HasChange("catalog_data") {
if v, ok := d.GetOk("catalog_data"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input := ecrpublic.PutRepositoryCatalogDataInput{
Expand All @@ -379,7 +390,7 @@ func resourceRepositoryUpdateCatalogData(ctx context.Context, conn *ecrpublic.EC
CatalogData: expandRepositoryCatalogData(v.([]interface{})[0].(map[string]interface{})),
}

_, err := conn.PutRepositoryCatalogDataWithContext(ctx, &input)
_, err := conn.PutRepositoryCatalogData(ctx, &input)

if err != nil {
return fmt.Errorf("updating catalog data for repository(%s): %s", d.Id(), err)
Expand Down
Loading

0 comments on commit 0bb9fba

Please sign in to comment.