Skip to content

Commit

Permalink
aws_servicequotas: update to aws sdk v2 (#33656)
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-b authored Sep 29, 2023
1 parent d91e495 commit a154e1b
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 113 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3control v1.33.0
github.com/aws/aws-sdk-go-v2/service/scheduler v1.3.0
github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0
github.com/aws/aws-sdk-go-v2/service/servicequotas v1.16.0
github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.1
github.com/aws/aws-sdk-go-v2/service/signer v1.16.5
github.com/aws/aws-sdk-go-v2/service/ssm v1.38.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ github.com/aws/aws-sdk-go-v2/service/scheduler v1.3.0 h1:uzCEL2ILopsOcWvbmeMmmy3
github.com/aws/aws-sdk-go-v2/service/scheduler v1.3.0/go.mod h1:cdpHC7Nd4Yvtf/rhRqyqqI0fzoCb0fpo2oOFVZ0HTeQ=
github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0 h1:Ou2rjk3siybv09bzpi5fs+9zOYZACxT1LT0KkcDAtIs=
github.com/aws/aws-sdk-go-v2/service/securitylake v1.7.0/go.mod h1:/MCawoN8Xib5q04k2HsIQ+K2cNtC3CHamrfLZXd6KmA=
github.com/aws/aws-sdk-go-v2/service/servicequotas v1.16.0 h1:dhp7Do5oaybdDdYGcdUNyzYFPsM4sNCvuPqph7MG5X0=
github.com/aws/aws-sdk-go-v2/service/servicequotas v1.16.0/go.mod h1:7+H2efEiOCrUl5EEsDFhe5BeI4gHGLUlisCyAJAcSvs=
github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.1 h1:QxzS/Hr5kixvMyPIXTfspnRUiKgFJSTPrhnglAi2YLI=
github.com/aws/aws-sdk-go-v2/service/sesv2 v1.20.1/go.mod h1:qpAr/ear7teIUoBd1gaPbvavdICoo1XyAIHPVlyawQc=
github.com/aws/aws-sdk-go-v2/service/signer v1.16.5 h1:nqZqDR44/ao9zQXyuCJI8L/C3QQIo4wtZyLtgwJfpEY=
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.

29 changes: 16 additions & 13 deletions internal/service/servicequotas/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package servicequotas_test

import (
"context"
"errors"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicequotas"
"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/servicequotas"
"github.com/aws/aws-sdk-go-v2/service/servicequotas/types"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)
Expand All @@ -29,11 +30,11 @@ const (
)

func testAccPreCheck(ctx context.Context, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasClient(ctx)

input := &servicequotas.ListServicesInput{}

_, err := conn.ListServicesWithContext(ctx, input)
_, err := conn.ListServices(ctx, input)

if acctest.PreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
Expand All @@ -46,15 +47,16 @@ func testAccPreCheck(ctx context.Context, t *testing.T) {

// nosemgrep:ci.servicequotas-in-func-name
func preCheckServiceQuotaSet(ctx context.Context, serviceCode, quotaCode string, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasClient(ctx)

input := &servicequotas.GetServiceQuotaInput{
QuotaCode: aws.String(quotaCode),
ServiceCode: aws.String(serviceCode),
}

_, err := conn.GetServiceQuotaWithContext(ctx, input)
if tfawserr.ErrCodeEquals(err, servicequotas.ErrCodeNoSuchResourceException) {
_, err := conn.GetServiceQuota(ctx, input)
var nsr *types.NoSuchResourceException
if errors.As(err, &nsr) {
t.Fatalf("The Service Quota (%s/%s) has never been set. This test can only be run with a quota that has previously been set. Please update the test to check a new quota.", serviceCode, quotaCode)
}
if err != nil {
Expand All @@ -63,31 +65,32 @@ func preCheckServiceQuotaSet(ctx context.Context, serviceCode, quotaCode string,
}

func preCheckServiceQuotaUnset(ctx context.Context, serviceCode, quotaCode string, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasClient(ctx)

input := &servicequotas.GetServiceQuotaInput{
QuotaCode: aws.String(quotaCode),
ServiceCode: aws.String(serviceCode),
}

_, err := conn.GetServiceQuotaWithContext(ctx, input)
_, err := conn.GetServiceQuota(ctx, input)
if err == nil {
t.Fatalf("The Service Quota (%s/%s) has been set. This test can only be run with a quota that has never been set. Please update the test to check a new quota.", serviceCode, quotaCode)
}
if !tfawserr.ErrCodeEquals(err, servicequotas.ErrCodeNoSuchResourceException) {
var nsr *types.NoSuchResourceException
if !errors.As(err, &nsr) {
t.Fatalf("unexpected PreCheck error getting Service Quota (%s/%s) : %s", serviceCode, quotaCode, err)
}
}

func preCheckServiceQuotaHasUsageMetric(ctx context.Context, serviceCode, quotaCode string, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).ServiceQuotasClient(ctx)

input := &servicequotas.GetAWSDefaultServiceQuotaInput{
QuotaCode: aws.String(quotaCode),
ServiceCode: aws.String(serviceCode),
}

quota, err := conn.GetAWSDefaultServiceQuotaWithContext(ctx, input)
quota, err := conn.GetAWSDefaultServiceQuota(ctx, input)
if err != nil {
t.Fatalf("unexpected PreCheck error getting Service Quota (%s/%s) : %s", serviceCode, quotaCode, err)
}
Expand Down
46 changes: 20 additions & 26 deletions internal/service/servicequotas/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ package servicequotas

import (
"context"
"errors"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicequotas"
"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/servicequotas"
"github.com/aws/aws-sdk-go-v2/service/servicequotas/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func findServiceQuotaDefaultByID(ctx context.Context, conn *servicequotas.ServiceQuotas, serviceCode, quotaCode string) (*servicequotas.ServiceQuota, error) {
func findServiceQuotaDefaultByID(ctx context.Context, conn *servicequotas.Client, serviceCode, quotaCode string) (*types.ServiceQuota, error) {
input := &servicequotas.GetAWSDefaultServiceQuotaInput{
ServiceCode: aws.String(serviceCode),
QuotaCode: aws.String(quotaCode),
}

output, err := conn.GetAWSDefaultServiceQuotaWithContext(ctx, input)
output, err := conn.GetAWSDefaultServiceQuota(ctx, input)

if err != nil {
return nil, err
Expand All @@ -32,45 +33,38 @@ func findServiceQuotaDefaultByID(ctx context.Context, conn *servicequotas.Servic
return output.Quota, nil
}

func findServiceQuotaDefaultByName(ctx context.Context, conn *servicequotas.ServiceQuotas, serviceCode, quotaName string) (*servicequotas.ServiceQuota, error) {
func findServiceQuotaDefaultByName(ctx context.Context, conn *servicequotas.Client, serviceCode, quotaName string) (*types.ServiceQuota, error) {
input := &servicequotas.ListAWSDefaultServiceQuotasInput{
ServiceCode: aws.String(serviceCode),
}

var defaultQuota *servicequotas.ServiceQuota
err := conn.ListAWSDefaultServiceQuotasPagesWithContext(ctx, input, func(page *servicequotas.ListAWSDefaultServiceQuotasOutput, lastPage bool) bool {
if page == nil {
return !lastPage
paginator := servicequotas.NewListAWSDefaultServiceQuotasPaginator(conn, input)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return nil, err
}

for _, q := range page.Quotas {
if aws.StringValue(q.QuotaName) == quotaName {
defaultQuota = q
return false
if aws.ToString(q.QuotaName) == quotaName {
return &q, nil
}
}

return !lastPage
})
if err != nil {
return nil, err
}
if defaultQuota == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return defaultQuota, nil
return nil, tfresource.NewEmptyResultError(input)
}

func findServiceQuotaByID(ctx context.Context, conn *servicequotas.ServiceQuotas, serviceCode, quotaCode string) (*servicequotas.ServiceQuota, error) {
func findServiceQuotaByID(ctx context.Context, conn *servicequotas.Client, serviceCode, quotaCode string) (*types.ServiceQuota, error) {
input := &servicequotas.GetServiceQuotaInput{
ServiceCode: aws.String(serviceCode),
QuotaCode: aws.String(quotaCode),
}

output, err := conn.GetServiceQuotaWithContext(ctx, input)
output, err := conn.GetServiceQuota(ctx, input)

if tfawserr.ErrCodeEquals(err, servicequotas.ErrCodeNoSuchResourceException) {
var nsr *types.NoSuchResourceException
if errors.As(err, &nsr) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand All @@ -86,7 +80,7 @@ func findServiceQuotaByID(ctx context.Context, conn *servicequotas.ServiceQuotas

if output.Quota.ErrorReason != nil {
return nil, &retry.NotFoundError{
Message: fmt.Sprintf("%s: %s", aws.StringValue(output.Quota.ErrorReason.ErrorCode), aws.StringValue(output.Quota.ErrorReason.ErrorMessage)),
Message: fmt.Sprintf("%s: %s", output.Quota.ErrorReason.ErrorCode, aws.ToString(output.Quota.ErrorReason.ErrorMessage)),
LastRequest: input,
}
}
Expand Down
32 changes: 19 additions & 13 deletions internal/service/servicequotas/service_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package servicequotas
import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicequotas"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/servicequotas"
"github.com/aws/aws-sdk-go-v2/service/servicequotas/types"
"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 All @@ -34,26 +35,31 @@ func DataSourceService() *schema.Resource {

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

serviceName := d.Get("service_name").(string)

input := &servicequotas.ListServicesInput{}

var service *servicequotas.ServiceInfo
err := conn.ListServicesPagesWithContext(ctx, input, func(page *servicequotas.ListServicesOutput, lastPage bool) bool {
var service *types.ServiceInfo
paginator := servicequotas.NewListServicesPaginator(conn, input)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return sdkdiag.AppendErrorf(diags, "listing Services: %s", err)
}

for _, s := range page.Services {
if aws.StringValue(s.ServiceName) == serviceName {
service = s
s := s
if aws.ToString(s.ServiceName) == serviceName {
service = &s
break
}
}

return !lastPage
})

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing Services: %s", err)
if service != nil {
break // stop paging once found
}
}

if service == nil {
Expand All @@ -62,7 +68,7 @@ func dataSourceServiceRead(ctx context.Context, d *schema.ResourceData, meta int

d.Set("service_code", service.ServiceCode)
d.Set("service_name", service.ServiceName)
d.SetId(aws.StringValue(service.ServiceCode))
d.SetId(aws.ToString(service.ServiceCode))

return diags
}
9 changes: 6 additions & 3 deletions internal/service/servicequotas/service_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import (
"fmt"
"testing"

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

func TestAccServiceQuotasServiceDataSource_serviceName(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_servicequotas_service.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, servicequotas.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, servicequotas.EndpointsID),
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.ServiceQuotasEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.ServiceQuotasEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down
17 changes: 10 additions & 7 deletions internal/service/servicequotas/service_package_gen.go

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

Loading

0 comments on commit a154e1b

Please sign in to comment.