Skip to content

Commit

Permalink
Merge pull request hashicorp#36549 from mattburgess/appstream-awssdkv…
Browse files Browse the repository at this point in the history
…2-migration

appstream: Migrate to AWS SDK v2
  • Loading branch information
johnsonaj authored May 15, 2024
2 parents abe6f30 + 16738b8 commit 382faaf
Show file tree
Hide file tree
Showing 28 changed files with 526 additions and 541 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/appintegrations v1.25.5
github.com/aws/aws-sdk-go-v2/service/applicationautoscaling v1.27.5
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.5
github.com/aws/aws-sdk-go-v2/service/appstream v1.34.3
github.com/aws/aws-sdk-go-v2/service/athena v1.40.5
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.5
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/aws/aws-sdk-go-v2/service/applicationautoscaling v1.27.5 h1:QXpYXqAD3
github.com/aws/aws-sdk-go-v2/service/applicationautoscaling v1.27.5/go.mod h1:g7O+8ghAn49ysZShSpeOxIRiI0/BgPoqHwZFNKnykco=
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.5 h1:uFhS3KcdZ/9+dN/0W/smIhtoCZP6cdQqiXsR0RzCgWE=
github.com/aws/aws-sdk-go-v2/service/apprunner v1.28.5/go.mod h1:HBEDVCiXAhDxrCJ8meNd1ao+PSQkkB02RfXaEuwyp6U=
github.com/aws/aws-sdk-go-v2/service/appstream v1.34.3 h1:YT1ehKTbyBd+ohzIdyHazuPXqmA7ShWugDxc+0IwmPM=
github.com/aws/aws-sdk-go-v2/service/appstream v1.34.3/go.mod h1:1QriwjVVSPeHKMgUioJigusztofBe096LqQ1i8pGI1A=
github.com/aws/aws-sdk-go-v2/service/athena v1.40.5 h1:NA0i0OP0EDQqmnI9zhF1zE4/MT9ZdAii2pFJdyDvvvc=
github.com/aws/aws-sdk-go-v2/service/athena v1.40.5/go.mod h1:6OHesqDfYPNzYI+VaXtmylYLyppuUy9SwRk4CH/pQA4=
github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.5 h1:H3SAxT2VpRwaNW09lRkXyadqYCcfXoZOg36nRQ8/Byg=
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.

45 changes: 23 additions & 22 deletions internal/service/appstream/directory_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appstream"
"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/appstream"
awstypes "github.com/aws/aws-sdk-go-v2/service/appstream/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"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/names"
Expand Down Expand Up @@ -74,16 +75,16 @@ func ResourceDirectoryConfig() *schema.Resource {
func resourceDirectoryConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).AppStreamConn(ctx)
conn := meta.(*conns.AWSClient).AppStreamClient(ctx)

directoryName := d.Get("directory_name").(string)
input := &appstream.CreateDirectoryConfigInput{
DirectoryName: aws.String(directoryName),
OrganizationalUnitDistinguishedNames: flex.ExpandStringSet(d.Get("organizational_unit_distinguished_names").(*schema.Set)),
OrganizationalUnitDistinguishedNames: flex.ExpandStringValueSet(d.Get("organizational_unit_distinguished_names").(*schema.Set)),
ServiceAccountCredentials: expandServiceAccountCredentials(d.Get("service_account_credentials").([]interface{})),
}

output, err := conn.CreateDirectoryConfigWithContext(ctx, input)
output, err := conn.CreateDirectoryConfig(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating AppStream Directory Config (%s): %s", directoryName, err)
}
Expand All @@ -92,19 +93,19 @@ func resourceDirectoryConfigCreate(ctx context.Context, d *schema.ResourceData,
return sdkdiag.AppendErrorf(diags, "creating AppStream Directory Config (%s): empty response", directoryName)
}

d.SetId(aws.StringValue(output.DirectoryConfig.DirectoryName))
d.SetId(aws.ToString(output.DirectoryConfig.DirectoryName))

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

func resourceDirectoryConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).AppStreamConn(ctx)
conn := meta.(*conns.AWSClient).AppStreamClient(ctx)

resp, err := conn.DescribeDirectoryConfigsWithContext(ctx, &appstream.DescribeDirectoryConfigsInput{DirectoryNames: []*string{aws.String(d.Id())}})
resp, err := conn.DescribeDirectoryConfigs(ctx, &appstream.DescribeDirectoryConfigsInput{DirectoryNames: []string{d.Id()}})

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
if !d.IsNewResource() && errs.IsA[*awstypes.ResourceNotFoundException](err) {
log.Printf("[WARN] AppStream Directory Config (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
Expand All @@ -124,9 +125,9 @@ func resourceDirectoryConfigRead(ctx context.Context, d *schema.ResourceData, me

directoryConfig := resp.DirectoryConfigs[0]

d.Set(names.AttrCreatedTime, aws.TimeValue(directoryConfig.CreatedTime).Format(time.RFC3339))
d.Set(names.AttrCreatedTime, aws.ToTime(directoryConfig.CreatedTime).Format(time.RFC3339))
d.Set("directory_name", directoryConfig.DirectoryName)
d.Set("organizational_unit_distinguished_names", flex.FlattenStringSet(directoryConfig.OrganizationalUnitDistinguishedNames))
d.Set("organizational_unit_distinguished_names", flex.FlattenStringValueSet(directoryConfig.OrganizationalUnitDistinguishedNames))

if err = d.Set("service_account_credentials", flattenServiceAccountCredentials(directoryConfig.ServiceAccountCredentials, d)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting `%s` for AppStream Directory Config (%s): %s", "service_account_credentials", d.Id(), err)
Expand All @@ -138,20 +139,20 @@ func resourceDirectoryConfigRead(ctx context.Context, d *schema.ResourceData, me
func resourceDirectoryConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).AppStreamConn(ctx)
conn := meta.(*conns.AWSClient).AppStreamClient(ctx)
input := &appstream.UpdateDirectoryConfigInput{
DirectoryName: aws.String(d.Id()),
}

if d.HasChange("organizational_unit_distinguished_names") {
input.OrganizationalUnitDistinguishedNames = flex.ExpandStringSet(d.Get("organizational_unit_distinguished_names").(*schema.Set))
input.OrganizationalUnitDistinguishedNames = flex.ExpandStringValueSet(d.Get("organizational_unit_distinguished_names").(*schema.Set))
}

if d.HasChange("service_account_credentials") {
input.ServiceAccountCredentials = expandServiceAccountCredentials(d.Get("service_account_credentials").([]interface{}))
}

_, err := conn.UpdateDirectoryConfigWithContext(ctx, input)
_, err := conn.UpdateDirectoryConfig(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating AppStream Directory Config (%s): %s", d.Id(), err)
}
Expand All @@ -162,14 +163,14 @@ func resourceDirectoryConfigUpdate(ctx context.Context, d *schema.ResourceData,
func resourceDirectoryConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).AppStreamConn(ctx)
conn := meta.(*conns.AWSClient).AppStreamClient(ctx)

log.Printf("[DEBUG] Deleting AppStream Directory Config: (%s)", d.Id())
_, err := conn.DeleteDirectoryConfigWithContext(ctx, &appstream.DeleteDirectoryConfigInput{
_, err := conn.DeleteDirectoryConfig(ctx, &appstream.DeleteDirectoryConfigInput{
DirectoryName: aws.String(d.Id()),
})

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

Expand All @@ -180,28 +181,28 @@ func resourceDirectoryConfigDelete(ctx context.Context, d *schema.ResourceData,
return diags
}

func expandServiceAccountCredentials(tfList []interface{}) *appstream.ServiceAccountCredentials {
func expandServiceAccountCredentials(tfList []interface{}) *awstypes.ServiceAccountCredentials {
if len(tfList) == 0 {
return nil
}

attr := tfList[0].(map[string]interface{})

apiObject := &appstream.ServiceAccountCredentials{
apiObject := &awstypes.ServiceAccountCredentials{
AccountName: aws.String(attr["account_name"].(string)),
AccountPassword: aws.String(attr["account_password"].(string)),
}

return apiObject
}

func flattenServiceAccountCredentials(apiObject *appstream.ServiceAccountCredentials, d *schema.ResourceData) []interface{} {
func flattenServiceAccountCredentials(apiObject *awstypes.ServiceAccountCredentials, d *schema.ResourceData) []interface{} {
if apiObject == nil {
return nil
}

tfList := map[string]interface{}{}
tfList["account_name"] = aws.StringValue(apiObject.AccountName)
tfList["account_name"] = aws.ToString(apiObject.AccountName)
tfList["account_password"] = d.Get("service_account_credentials.0.account_password").(string)

return []interface{}{tfList}
Expand Down
31 changes: 16 additions & 15 deletions internal/service/appstream/directory_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appstream"
"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/appstream"
awstypes "github.com/aws/aws-sdk-go-v2/service/appstream/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"
tfappstream "github.com/hashicorp/terraform-provider-aws/internal/service/appstream"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccAppStreamDirectoryConfig_basic(t *testing.T) {
ctx := acctest.Context(t)
var v1, v2 appstream.DirectoryConfig
var v1, v2 awstypes.DirectoryConfig
resourceName := "aws_appstream_directory_config.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
domain := acctest.RandomDomainName()
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestAccAppStreamDirectoryConfig_basic(t *testing.T) {

func TestAccAppStreamDirectoryConfig_disappears(t *testing.T) {
ctx := acctest.Context(t)
var v appstream.DirectoryConfig
var v awstypes.DirectoryConfig
resourceName := "aws_appstream_directory_config.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
domain := acctest.RandomDomainName()
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestAccAppStreamDirectoryConfig_disappears(t *testing.T) {

func TestAccAppStreamDirectoryConfig_OrganizationalUnitDistinguishedNames(t *testing.T) {
ctx := acctest.Context(t)
var v1, v2, v3 appstream.DirectoryConfig
var v1, v2, v3 awstypes.DirectoryConfig
resourceName := "aws_appstream_directory_config.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
domain := acctest.RandomDomainName()
Expand Down Expand Up @@ -153,15 +154,15 @@ func TestAccAppStreamDirectoryConfig_OrganizationalUnitDistinguishedNames(t *tes
})
}

func testAccCheckDirectoryConfigExists(ctx context.Context, resourceName string, appStreamDirectoryConfig *appstream.DirectoryConfig) resource.TestCheckFunc {
func testAccCheckDirectoryConfigExists(ctx context.Context, resourceName string, appStreamDirectoryConfig *awstypes.DirectoryConfig) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AppStreamConn(ctx)
resp, err := conn.DescribeDirectoryConfigsWithContext(ctx, &appstream.DescribeDirectoryConfigsInput{DirectoryNames: []*string{aws.String(rs.Primary.ID)}})
conn := acctest.Provider.Meta().(*conns.AWSClient).AppStreamClient(ctx)
resp, err := conn.DescribeDirectoryConfigs(ctx, &appstream.DescribeDirectoryConfigsInput{DirectoryNames: []string{rs.Primary.ID}})

if err != nil {
return err
Expand All @@ -171,24 +172,24 @@ func testAccCheckDirectoryConfigExists(ctx context.Context, resourceName string,
return fmt.Errorf("AppStream Directory Config %q does not exist", rs.Primary.ID)
}

*appStreamDirectoryConfig = *resp.DirectoryConfigs[0]
*appStreamDirectoryConfig = resp.DirectoryConfigs[0]

return nil
}
}

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_appstream_directory_config" {
continue
}

resp, err := conn.DescribeDirectoryConfigsWithContext(ctx, &appstream.DescribeDirectoryConfigsInput{DirectoryNames: []*string{aws.String(rs.Primary.ID)}})
resp, err := conn.DescribeDirectoryConfigs(ctx, &appstream.DescribeDirectoryConfigsInput{DirectoryNames: []string{rs.Primary.ID}})

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

Expand All @@ -205,9 +206,9 @@ func testAccCheckDirectoryConfigDestroy(ctx context.Context) resource.TestCheckF
}
}

func testAccCheckDirectoryConfigNotRecreated(i, j *appstream.DirectoryConfig) resource.TestCheckFunc {
func testAccCheckDirectoryConfigNotRecreated(i, j *awstypes.DirectoryConfig) resource.TestCheckFunc {
return func(s *terraform.State) error {
if !aws.TimeValue(i.CreatedTime).Equal(aws.TimeValue(j.CreatedTime)) {
if !aws.ToTime(i.CreatedTime).Equal(aws.ToTime(j.CreatedTime)) {
return fmt.Errorf("AppStream Directory Config recreated")
}

Expand Down
Loading

0 comments on commit 382faaf

Please sign in to comment.