Skip to content

Commit

Permalink
Merge pull request #39702 from alexbacchin/b-aws_workspaces_directory…
Browse files Browse the repository at this point in the history
…-ip_groups_ids

b/aws_workspaces_directory fixed error when update ip_group_ids from empty set
  • Loading branch information
ewbankkit authored Oct 15, 2024
2 parents 8434bd5 + 2da1a22 commit b15da28
Show file tree
Hide file tree
Showing 22 changed files with 1,138 additions and 1,527 deletions.
2 changes: 2 additions & 0 deletions .changelog/39702.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```release-note:bug
resource/aws_workspaces_directory : Fix `InvalidParameterValuesException: Provided set of IP Group IDs are not valid` errors on Update
158 changes: 79 additions & 79 deletions internal/service/workspaces/bundle_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package workspaces

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/workspaces"
Expand All @@ -14,11 +13,13 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_workspaces_bundle")
func DataSourceBundle() *schema.Resource {
// @SDKDataSource("aws_workspaces_bundle", name="Bundle")
func dataSourceBundle() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceWorkspaceBundleRead,

Expand All @@ -28,20 +29,6 @@ func DataSourceBundle() *schema.Resource {
Optional: true,
ConflictsWith: []string{names.AttrOwner, names.AttrName},
},
names.AttrName: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"bundle_id"},
},
names.AttrOwner: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"bundle_id"},
},
names.AttrDescription: {
Type: schema.TypeString,
Computed: true,
},
"compute_type": {
Type: schema.TypeList,
Computed: true,
Expand All @@ -54,7 +41,21 @@ func DataSourceBundle() *schema.Resource {
},
},
},
"user_storage": {
names.AttrDescription: {
Type: schema.TypeString,
Computed: true,
},
names.AttrName: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"bundle_id"},
},
names.AttrOwner: {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"bundle_id"},
},
"root_storage": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Expand All @@ -66,7 +67,7 @@ func DataSourceBundle() *schema.Resource {
},
},
},
"root_storage": {
"user_storage": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Expand All @@ -86,96 +87,95 @@ func dataSourceWorkspaceBundleRead(ctx context.Context, d *schema.ResourceData,
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WorkSpacesClient(ctx)

var bundle types.WorkspaceBundle

if bundleID, ok := d.GetOk("bundle_id"); ok {
resp, err := conn.DescribeWorkspaceBundles(ctx, &workspaces.DescribeWorkspaceBundlesInput{
BundleIds: []string{bundleID.(string)},
})
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading WorkSpaces Workspace Bundle (%s): %s", bundleID, err)
}

if len(resp.Bundles) != 1 {
return sdkdiag.AppendErrorf(diags, "expected 1 result for WorkSpaces Workspace Bundle %q, found %d", bundleID, len(resp.Bundles))
}
var bundle *types.WorkspaceBundle
var err error

if len(resp.Bundles) == 0 {
return sdkdiag.AppendErrorf(diags, "no WorkSpaces Workspace Bundle with ID %q found", bundleID)
if v, ok := d.GetOk("bundle_id"); ok {
input := &workspaces.DescribeWorkspaceBundlesInput{
BundleIds: []string{v.(string)},
}

bundle = resp.Bundles[0]
bundle, err = findBundle(ctx, conn, input, tfslices.PredicateTrue[*types.WorkspaceBundle]())
}

if name, ok := d.GetOk(names.AttrName); ok {
id := name
if v, ok := d.GetOk(names.AttrName); ok {
name := v.(string)
input := &workspaces.DescribeWorkspaceBundlesInput{}

if owner, ok := d.GetOk(names.AttrOwner); ok {
id = fmt.Sprintf("%s:%s", owner, id)
input.Owner = aws.String(owner.(string))
if v, ok := d.GetOk(names.AttrOwner); ok {
input.Owner = aws.String(v.(string))
}

name := name.(string)

paginator := workspaces.NewDescribeWorkspaceBundlesPaginator(conn, input, func(out *workspaces.DescribeWorkspaceBundlesPaginatorOptions) {})

entryNotFound := true
for paginator.HasMorePages() && entryNotFound {
out, err := paginator.NextPage(ctx)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading WorkSpaces Workspace Bundle (%s): %s", id, err)
}

for _, b := range out.Bundles {
if aws.ToString(b.Name) == name {
bundle = b
entryNotFound = false
}
}
}
bundle, err = findBundle(ctx, conn, input, func(v *types.WorkspaceBundle) bool {
return aws.ToString(v.Name) == name
})
}

if entryNotFound {
return sdkdiag.AppendErrorf(diags, "no WorkSpaces Workspace Bundle with name %q found", name)
}
if err != nil {
return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("WorkSpaces Bundle", err))
}

d.SetId(aws.ToString(bundle.BundleId))
d.Set("bundle_id", bundle.BundleId)
d.Set(names.AttrDescription, bundle.Description)
d.Set(names.AttrName, bundle.Name)
d.Set(names.AttrOwner, bundle.Owner)

computeType := make([]map[string]interface{}, 1)
tfMap := make([]map[string]interface{}, 1)
if bundle.ComputeType != nil {
computeType[0] = map[string]interface{}{
tfMap[0] = map[string]interface{}{
names.AttrName: string(bundle.ComputeType.Name),
}
}
if err := d.Set("compute_type", computeType); err != nil {
if err := d.Set("compute_type", tfMap); err != nil {
return sdkdiag.AppendErrorf(diags, "setting compute_type: %s", err)
}

rootStorage := make([]map[string]interface{}, 1)
d.Set(names.AttrDescription, bundle.Description)
d.Set(names.AttrName, bundle.Name)
d.Set(names.AttrOwner, bundle.Owner)
tfMap = make([]map[string]interface{}, 1)
if bundle.RootStorage != nil {
rootStorage[0] = map[string]interface{}{
tfMap[0] = map[string]interface{}{
"capacity": aws.ToString(bundle.RootStorage.Capacity),
}
}
if err := d.Set("root_storage", rootStorage); err != nil {
if err := d.Set("root_storage", tfMap); err != nil {
return sdkdiag.AppendErrorf(diags, "setting root_storage: %s", err)
}

userStorage := make([]map[string]interface{}, 1)
tfMap = make([]map[string]interface{}, 1)
if bundle.UserStorage != nil {
userStorage[0] = map[string]interface{}{
tfMap[0] = map[string]interface{}{
"capacity": aws.ToString(bundle.UserStorage.Capacity),
}
}
if err := d.Set("user_storage", userStorage); err != nil {
if err := d.Set("user_storage", tfMap); err != nil {
return sdkdiag.AppendErrorf(diags, "setting user_storage: %s", err)
}

return diags
}

func findBundle(ctx context.Context, conn *workspaces.Client, input *workspaces.DescribeWorkspaceBundlesInput, filter tfslices.Predicate[*types.WorkspaceBundle]) (*types.WorkspaceBundle, error) {
output, err := findBundles(ctx, conn, input, filter)

if err != nil {
return nil, err
}

return tfresource.AssertSingleValueResult(output)
}

func findBundles(ctx context.Context, conn *workspaces.Client, input *workspaces.DescribeWorkspaceBundlesInput, filter tfslices.Predicate[*types.WorkspaceBundle]) ([]types.WorkspaceBundle, error) {
var output []types.WorkspaceBundle

pages := workspaces.NewDescribeWorkspaceBundlesPaginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
return nil, err
}

for _, v := range page.Bundles {
if filter(&v) {
output = append(output, v)
}
}
}

return output, nil
}
24 changes: 12 additions & 12 deletions internal/service/workspaces/bundle_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func testAccWorkspaceBundleDataSource_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccBundleDataSourceConfig_basic("wsb-b0s22j3d7"),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "bundle_id", "wsb-b0s22j3d7"),
resource.TestCheckResourceAttr(dataSourceName, "compute_type.#", acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, "compute_type.0.name", "PERFORMANCE"),
Expand All @@ -54,13 +54,13 @@ func testAccWorkspaceBundleDataSource_byOwnerName(t *testing.T) {
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccBundleDataSourceConfig_byOwnerName("AMAZON", "Value with Windows 10 and Office 2016"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "bundle_id", "wsb-df76rqys9"),
Config: testAccBundleDataSourceConfig_byOwnerName("Amazon", "Value with Ubuntu 22.04"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "bundle_id"),
resource.TestCheckResourceAttr(dataSourceName, "compute_type.#", acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, "compute_type.0.name", "VALUE"),
resource.TestCheckResourceAttrSet(dataSourceName, names.AttrDescription),
resource.TestCheckResourceAttr(dataSourceName, names.AttrName, "Value with Windows 10 and Office 2016"),
resource.TestCheckResourceAttr(dataSourceName, names.AttrName, "Value with Ubuntu 22.04"),
resource.TestCheckResourceAttr(dataSourceName, names.AttrOwner, "Amazon"),
resource.TestCheckResourceAttr(dataSourceName, "root_storage.#", acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, "root_storage.0.capacity", "80"),
Expand Down Expand Up @@ -119,34 +119,34 @@ func testAccBundlePreCheck(t *testing.T) {
func testAccBundleDataSourceConfig_basic(bundleID string) string {
return fmt.Sprintf(`
data "aws_workspaces_bundle" "test" {
bundle_id = %q
bundle_id = %[1]q
}
`, bundleID)
}

func testAccBundleDataSourceConfig_byOwnerName(owner, name string) string {
return fmt.Sprintf(`
data "aws_workspaces_bundle" "test" {
owner = %q
name = %q
owner = %[1]q
name = %[2]q
}
`, owner, name)
}

func testAccBundleDataSourceConfig_idAndOwnerNameConflict(bundleID, owner, name string) string {
return fmt.Sprintf(`
data "aws_workspaces_bundle" "test" {
bundle_id = %q
owner = %q
name = %q
bundle_id = %[1]q
owner = %[2]q
name = %[3]q
}
`, bundleID, owner, name)
}

func testAccBundleDataSourceConfig_privateOwner(name string) string {
return fmt.Sprintf(`
data "aws_workspaces_bundle" "test" {
name = %q
name = %[1]q
}
`, name)
}
Loading

0 comments on commit b15da28

Please sign in to comment.