Skip to content

Commit

Permalink
Merge pull request #35540 from hashicorp/td-migrate-codeartifact-to-a…
Browse files Browse the repository at this point in the history
…ws-sdk-v2

Tech debt: Migrate `codeartifact` resources to AWS SDK for Go v2
  • Loading branch information
ewbankkit authored Jan 29, 2024
2 parents 60b3b69 + 74f6621 commit 4d52fa5
Show file tree
Hide file tree
Showing 23 changed files with 705 additions and 670 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/cleanrooms v1.8.6
github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.15.7
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.31.0
github.com/aws/aws-sdk-go-v2/service/codeartifact v1.23.6
github.com/aws/aws-sdk-go-v2/service/codebuild v1.28.0
github.com/aws/aws-sdk-go-v2/service/codecatalyst v1.10.6
github.com/aws/aws-sdk-go-v2/service/codecommit v1.20.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.15.7 h1:8sBfx7QkDZ6dgfUNXWH
github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.15.7/go.mod h1:P1EMD13hrBE2KUw030w482Eyk2NmOFIvGqmgNi4XRDc=
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.31.0 h1:Rk+Ft0Mu/eiNt2iJ2oS8Gf1h5m6q5crwS8cmlTylnvM=
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.31.0/go.mod h1:jZNaJEtn9TLi3pfxycLz79HVkKxP8ZdYm92iaNFgBsA=
github.com/aws/aws-sdk-go-v2/service/codeartifact v1.23.6 h1:QuI+Nh9lQ9EgCMhLzIDEp95cLnNd1vFHyODD0he0oQs=
github.com/aws/aws-sdk-go-v2/service/codeartifact v1.23.6/go.mod h1:xQr2tI0JWOW/8rl8VWjzaiwGR7VfjCta6L/cLKCzN7E=
github.com/aws/aws-sdk-go-v2/service/codebuild v1.28.0 h1:ueiaIwSz96QKCX+xfb4dWC681xycSLybAFOz3X11U7s=
github.com/aws/aws-sdk-go-v2/service/codebuild v1.28.0/go.mod h1:kHgLQLdLEqmcxQxDuGjz1naXvaUvBMMKolAJECBwYtw=
github.com/aws/aws-sdk-go-v2/service/codecatalyst v1.10.6 h1:WLVD5wFI3yC1u/8L9bNeZ9+VURSdKjGA1Q+n+F1355Y=
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.

48 changes: 24 additions & 24 deletions internal/service/codeartifact/authorization_token_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package codeartifact
import (
"context"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codeartifact"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codeartifact"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -19,12 +18,16 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

// @SDKDataSource("aws_codeartifact_authorization_token")
func DataSourceAuthorizationToken() *schema.Resource {
// @SDKDataSource("aws_codeartifact_authorization_token", name="Authoiration Token")
func dataSourceAuthorizationToken() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceAuthorizationTokenRead,

Schema: map[string]*schema.Schema{
"authorization_token": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Required: true,
Expand All @@ -43,10 +46,6 @@ func DataSourceAuthorizationToken() *schema.Resource {
validation.IntInSlice([]int{0}),
),
},
"authorization_token": {
Type: schema.TypeString,
Computed: true,
},
"expiration": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -57,33 +56,34 @@ func DataSourceAuthorizationToken() *schema.Resource {

func dataSourceAuthorizationTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CodeArtifactConn(ctx)
domain := d.Get("domain").(string)
domainOwner := meta.(*conns.AWSClient).AccountID
params := &codeartifact.GetAuthorizationTokenInput{
Domain: aws.String(domain),
}
conn := meta.(*conns.AWSClient).CodeArtifactClient(ctx)

domainName := d.Get("domain").(string)
var domainOwner string
if v, ok := d.GetOk("domain_owner"); ok {
params.DomainOwner = aws.String(v.(string))
domainOwner = v.(string)
} else {
domainOwner = meta.(*conns.AWSClient).AccountID
}
input := &codeartifact.GetAuthorizationTokenInput{
Domain: aws.String(domainName),
DomainOwner: aws.String(domainOwner),
}

if v, ok := d.GetOkExists("duration_seconds"); ok {
params.DurationSeconds = aws.Int64(int64(v.(int)))
input.DurationSeconds = aws.Int64(int64(v.(int)))
}

log.Printf("[DEBUG] Getting CodeArtifact authorization token")
out, err := conn.GetAuthorizationTokenWithContext(ctx, params)
output, err := conn.GetAuthorizationToken(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "getting CodeArtifact authorization token: %s", err)
return sdkdiag.AppendErrorf(diags, "reading CodeArtifact Authorization Token: %s", err)
}
log.Printf("[DEBUG] CodeArtifact authorization token: %#v", out)

d.SetId(fmt.Sprintf("%s:%s", domainOwner, domain))
d.Set("authorization_token", out.AuthorizationToken)
d.Set("expiration", aws.TimeValue(out.Expiration).Format(time.RFC3339))
d.SetId(fmt.Sprintf("%s:%s", domainOwner, domainName))
d.Set("authorization_token", output.AuthorizationToken)
d.Set("domain_owner", domainOwner)
d.Set("expiration", aws.ToTime(output.Expiration).Format(time.RFC3339))

return diags
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/codeartifact"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func testAccAuthorizationTokenDataSource_basic(t *testing.T) {
Expand All @@ -19,8 +19,8 @@ func testAccAuthorizationTokenDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_codeartifact_authorization_token.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, codeartifact.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, codeartifact.EndpointsID),
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.CodeArtifactEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.CodeArtifactEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -41,8 +41,8 @@ func testAccAuthorizationTokenDataSource_owner(t *testing.T) {
dataSourceName := "data.aws_codeartifact_authorization_token.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, codeartifact.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, codeartifact.EndpointsID),
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.CodeArtifactEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.CodeArtifactEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -63,8 +63,8 @@ func testAccAuthorizationTokenDataSource_duration(t *testing.T) {
dataSourceName := "data.aws_codeartifact_authorization_token.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, codeartifact.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, codeartifact.EndpointsID),
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.CodeArtifactEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.CodeArtifactEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -80,7 +80,7 @@ func testAccAuthorizationTokenDataSource_duration(t *testing.T) {
})
}

func testAccCheckAuthorizationTokenBaseConfig(rName string) string {
func testAccCheckAuthorizationTokenConfig_base(rName string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = %[1]q
Expand All @@ -96,7 +96,7 @@ resource "aws_codeartifact_domain" "test" {

func testAccAuthorizationTokenDataSourceConfig_basic(rName string) string {
return acctest.ConfigCompose(
testAccCheckAuthorizationTokenBaseConfig(rName),
testAccCheckAuthorizationTokenConfig_base(rName),
`
data "aws_codeartifact_authorization_token" "test" {
domain = aws_codeartifact_domain.test.domain
Expand All @@ -106,7 +106,7 @@ data "aws_codeartifact_authorization_token" "test" {

func testAccAuthorizationTokenDataSourceConfig_owner(rName string) string {
return acctest.ConfigCompose(
testAccCheckAuthorizationTokenBaseConfig(rName),
testAccCheckAuthorizationTokenConfig_base(rName),
`
data "aws_codeartifact_authorization_token" "test" {
domain = aws_codeartifact_domain.test.domain
Expand All @@ -117,7 +117,7 @@ data "aws_codeartifact_authorization_token" "test" {

func testAccAuthorizationTokenDataSourceConfig_duration(rName string) string {
return acctest.ConfigCompose(
testAccCheckAuthorizationTokenBaseConfig(rName),
testAccCheckAuthorizationTokenConfig_base(rName),
`
data "aws_codeartifact_authorization_token" "test" {
domain = aws_codeartifact_domain.test.domain
Expand Down
9 changes: 5 additions & 4 deletions internal/service/codeartifact/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

package codeartifact

import (
"time"
)

const (
ResNameDomain = "Domain"
ResNameDomainPermissionsPolicy = "Domain Permissions Policy"
ResNameRepository = "Repository"
ResNameRepositoryPermissionsPolicy = "Repository Permissions Policy"
propagationTimeout = 2 * time.Minute
)
Loading

0 comments on commit 4d52fa5

Please sign in to comment.