Skip to content

Commit

Permalink
Merge pull request #36542 from mattburgess/appconfig-awssdkv2-migration
Browse files Browse the repository at this point in the history
appconfig: Migrate to AWS SDK v2
  • Loading branch information
johnsonaj authored Apr 2, 2024
2 parents 253cd51 + 12fbdba commit e72da85
Show file tree
Hide file tree
Showing 34 changed files with 499 additions and 553 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.

33 changes: 17 additions & 16 deletions internal/service/appconfig/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/appconfig"
"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/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/appconfig"
awstypes "github.com/aws/aws-sdk-go-v2/service/appconfig/types"
"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"
"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"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -58,7 +59,7 @@ func ResourceApplication() *schema.Resource {

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

applicationName := d.Get("name").(string)
input := &appconfig.CreateApplicationInput{
Expand All @@ -70,7 +71,7 @@ func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, meta
input.Description = aws.String(v.(string))
}

app, err := conn.CreateApplicationWithContext(ctx, input)
app, err := conn.CreateApplication(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating AppConfig Application (%s): %s", applicationName, err)
Expand All @@ -80,22 +81,22 @@ func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, meta
return sdkdiag.AppendErrorf(diags, "creating AppConfig Application (%s): empty response", applicationName)
}

d.SetId(aws.StringValue(app.Id))
d.SetId(aws.ToString(app.Id))

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

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

input := &appconfig.GetApplicationInput{
ApplicationId: aws.String(d.Id()),
}

output, err := conn.GetApplicationWithContext(ctx, input)
output, err := conn.GetApplication(ctx, input)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) {
if !d.IsNewResource() && errs.IsA[*awstypes.ResourceNotFoundException](err) {
log.Printf("[WARN] Appconfig Application (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
Expand All @@ -113,7 +114,7 @@ func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta i
AccountID: meta.(*conns.AWSClient).AccountID,
Partition: meta.(*conns.AWSClient).Partition,
Region: meta.(*conns.AWSClient).Region,
Resource: fmt.Sprintf("application/%s", aws.StringValue(output.Id)),
Resource: fmt.Sprintf("application/%s", aws.ToString(output.Id)),
Service: "appconfig",
}.String()

Expand All @@ -126,7 +127,7 @@ func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta i

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

if d.HasChangesExcept("tags", "tags_all") {
updateInput := &appconfig.UpdateApplicationInput{
Expand All @@ -141,7 +142,7 @@ func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta
updateInput.Name = aws.String(d.Get("name").(string))
}

_, err := conn.UpdateApplicationWithContext(ctx, updateInput)
_, err := conn.UpdateApplication(ctx, updateInput)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating AppConfig Application(%s): %s", d.Id(), err)
Expand All @@ -153,14 +154,14 @@ func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta

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

log.Printf("[INFO] Deleting AppConfig Application: %s", d.Id())
_, err := conn.DeleteApplicationWithContext(ctx, &appconfig.DeleteApplicationInput{
_, err := conn.DeleteApplication(ctx, &appconfig.DeleteApplicationInput{
ApplicationId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}

Expand Down
17 changes: 9 additions & 8 deletions internal/service/appconfig/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"testing"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appconfig"
"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/appconfig"
awstypes "github.com/aws/aws-sdk-go-v2/service/appconfig/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"
tfappconfig "github.com/hashicorp/terraform-provider-aws/internal/service/appconfig"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestAccAppConfigApplication_tags(t *testing.T) {

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_appconfig_application" {
Expand All @@ -212,9 +213,9 @@ func testAccCheckApplicationDestroy(ctx context.Context) resource.TestCheckFunc
ApplicationId: aws.String(rs.Primary.ID),
}

output, err := conn.GetApplicationWithContext(ctx, input)
output, err := conn.GetApplication(ctx, input)

if tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
continue
}

Expand Down Expand Up @@ -242,13 +243,13 @@ func testAccCheckApplicationExists(ctx context.Context, resourceName string) res
return fmt.Errorf("Resource (%s) ID not set", resourceName)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AppConfigConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).AppConfigClient(ctx)

input := &appconfig.GetApplicationInput{
ApplicationId: aws.String(rs.Primary.ID),
}

output, err := conn.GetApplicationWithContext(ctx, input)
output, err := conn.GetApplication(ctx, input)

if err != nil {
return fmt.Errorf("error reading AppConfig Application (%s): %w", rs.Primary.ID, err)
Expand Down
75 changes: 29 additions & 46 deletions internal/service/appconfig/configuration_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"strings"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/appconfig"
"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/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/appconfig"
awstypes "github.com/aws/aws-sdk-go-v2/service/appconfig/types"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -105,9 +107,9 @@ func ResourceConfigurationProfile() *schema.Resource {
DiffSuppressFunc: verify.SuppressEquivalentJSONDiffs,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(appconfig.ValidatorType_Values(), false),
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: enum.Validate[awstypes.ValidatorType](),
},
},
},
Expand All @@ -119,7 +121,7 @@ func ResourceConfigurationProfile() *schema.Resource {

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

appId := d.Get("application_id").(string)
name := d.Get("name").(string)
Expand Down Expand Up @@ -150,7 +152,7 @@ func resourceConfigurationProfileCreate(ctx context.Context, d *schema.ResourceD
input.Validators = expandValidators(v.(*schema.Set).List())
}

profile, err := conn.CreateConfigurationProfileWithContext(ctx, input)
profile, err := conn.CreateConfigurationProfile(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating AppConfig Configuration Profile (%s) for Application (%s): %s", name, appId, err)
Expand All @@ -160,14 +162,14 @@ func resourceConfigurationProfileCreate(ctx context.Context, d *schema.ResourceD
return sdkdiag.AppendErrorf(diags, "creating AppConfig Configuration Profile (%s) for Application (%s): empty response", name, appId)
}

d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(profile.Id), aws.StringValue(profile.ApplicationId)))
d.SetId(fmt.Sprintf("%s:%s", aws.ToString(profile.Id), aws.ToString(profile.ApplicationId)))

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

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

confProfID, appID, err := ConfigurationProfileParseID(d.Id())

Expand All @@ -180,9 +182,9 @@ func resourceConfigurationProfileRead(ctx context.Context, d *schema.ResourceDat
ConfigurationProfileId: aws.String(confProfID),
}

output, err := conn.GetConfigurationProfileWithContext(ctx, input)
output, err := conn.GetConfigurationProfile(ctx, input)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) {
if !d.IsNewResource() && errs.IsA[*awstypes.ResourceNotFoundException](err) {
log.Printf("[WARN] AppConfig Configuration Profile (%s) for Application (%s) not found, removing from state", confProfID, appID)
d.SetId("")
return diags
Expand Down Expand Up @@ -223,7 +225,7 @@ func resourceConfigurationProfileRead(ctx context.Context, d *schema.ResourceDat

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

if d.HasChangesExcept("tags", "tags_all") {
confProfID, appID, err := ConfigurationProfileParseID(d.Id())
Expand Down Expand Up @@ -257,7 +259,7 @@ func resourceConfigurationProfileUpdate(ctx context.Context, d *schema.ResourceD
updateInput.Validators = expandValidators(d.Get("validator").(*schema.Set).List())
}

_, err = conn.UpdateConfigurationProfileWithContext(ctx, updateInput)
_, err = conn.UpdateConfigurationProfile(ctx, updateInput)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating AppConfig Configuration Profile (%s) for Application (%s): %s", confProfID, appID, err)
Expand All @@ -269,20 +271,20 @@ func resourceConfigurationProfileUpdate(ctx context.Context, d *schema.ResourceD

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

confProfID, appID, err := ConfigurationProfileParseID(d.Id())
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}

log.Printf("[INFO] Deleting AppConfig Configuration Profile: %s", d.Id())
_, err = conn.DeleteConfigurationProfileWithContext(ctx, &appconfig.DeleteConfigurationProfileInput{
_, err = conn.DeleteConfigurationProfile(ctx, &appconfig.DeleteConfigurationProfileInput{
ApplicationId: aws.String(appID),
ConfigurationProfileId: aws.String(confProfID),
})

if tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}

Expand All @@ -303,29 +305,25 @@ func ConfigurationProfileParseID(id string) (string, string, error) {
return parts[0], parts[1], nil
}

func expandValidator(tfMap map[string]interface{}) *appconfig.Validator {
if tfMap == nil {
return nil
}

validator := &appconfig.Validator{}
func expandValidator(tfMap map[string]interface{}) awstypes.Validator {
validator := awstypes.Validator{}

// AppConfig API supports empty content
if v, ok := tfMap["content"].(string); ok {
validator.Content = aws.String(v)
}

if v, ok := tfMap["type"].(string); ok && v != "" {
validator.Type = aws.String(v)
validator.Type = awstypes.ValidatorType(v)
}

return validator
}

func expandValidators(tfList []interface{}) []*appconfig.Validator {
func expandValidators(tfList []interface{}) []awstypes.Validator {
// AppConfig API requires a 0 length slice instead of a nil value
// when updating from N validators to 0/nil validators
validators := make([]*appconfig.Validator, 0)
validators := make([]awstypes.Validator, 0)

for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]interface{})
Expand All @@ -335,47 +333,32 @@ func expandValidators(tfList []interface{}) []*appconfig.Validator {
}

validator := expandValidator(tfMap)

if validator == nil {
continue
}

validators = append(validators, validator)
}

return validators
}

func flattenValidator(validator *appconfig.Validator) map[string]interface{} {
if validator == nil {
return nil
}

func flattenValidator(validator awstypes.Validator) map[string]interface{} {
tfMap := map[string]interface{}{}

if v := validator.Content; v != nil {
tfMap["content"] = aws.StringValue(v)
tfMap["content"] = aws.ToString(v)
}

if v := validator.Type; v != nil {
tfMap["type"] = aws.StringValue(v)
}
tfMap["type"] = string(validator.Type)

return tfMap
}

func flattenValidators(validators []*appconfig.Validator) []interface{} {
func flattenValidators(validators []awstypes.Validator) []interface{} {
if len(validators) == 0 {
return nil
}

var tfList []interface{}

for _, validator := range validators {
if validator == nil {
continue
}

tfList = append(tfList, flattenValidator(validator))
}

Expand Down
Loading

0 comments on commit e72da85

Please sign in to comment.