Skip to content

Commit

Permalink
Merge pull request #39777 from hashicorp/b-aws_workspace_bundle-regre…
Browse files Browse the repository at this point in the history
…ssion

d/aws_workspaces_bundle: Fix `multiple WorkSpaces Bundles matched; use additional constraints to reduce matches to a single WorkSpaces Bundle` regression
  • Loading branch information
ewbankkit authored Oct 17, 2024
2 parents 08f131c + a4dacde commit f8d4fe8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/39777.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_workspaces_bundle: Return the first matching bundle when searching by `name`. This fixes a regression introduced in [v5.72.0](https://github.com/hashicorp/terraform-provider-aws/blob/main/CHANGELOG.md#5720-october-15-2024) causing `multiple WorkSpaces Bundles matched; use additional constraints to reduce matches to a single WorkSpaces Bundle` errors
```
22 changes: 17 additions & 5 deletions internal/service/workspaces/bundle_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ func dataSourceWorkspaceBundleRead(ctx context.Context, d *schema.ResourceData,
var err error

if v, ok := d.GetOk("bundle_id"); ok {
input := &workspaces.DescribeWorkspaceBundlesInput{
BundleIds: []string{v.(string)},
}
bundle, err = findBundle(ctx, conn, input, tfslices.PredicateTrue[*types.WorkspaceBundle]())
bundle, err = findBundleByID(ctx, conn, v.(string))
}

if v, ok := d.GetOk(names.AttrName); ok {
Expand Down Expand Up @@ -149,14 +146,29 @@ func dataSourceWorkspaceBundleRead(ctx context.Context, d *schema.ResourceData,
return diags
}

func findBundleByID(ctx context.Context, conn *workspaces.Client, id string) (*types.WorkspaceBundle, error) {
input := &workspaces.DescribeWorkspaceBundlesInput{
BundleIds: []string{id},
}

output, err := findBundles(ctx, conn, input, tfslices.PredicateTrue[*types.WorkspaceBundle]())

if err != nil {
return nil, err
}

return tfresource.AssertSingleValueResult(output)
}

// findBundle returns the first bundle that matches the filter.
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)
return tfresource.AssertFirstValueResult(output)
}

func findBundles(ctx context.Context, conn *workspaces.Client, input *workspaces.DescribeWorkspaceBundlesInput, filter tfslices.Predicate[*types.WorkspaceBundle]) ([]types.WorkspaceBundle, error) {
Expand Down
28 changes: 28 additions & 0 deletions internal/service/workspaces/bundle_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ func testAccWorkspaceBundleDataSource_byOwnerName(t *testing.T) {
})
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, strings.ToLower(workspaces.ServiceID)),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccBundleDataSourceConfig_byOwnerName("AMAZON", "Performance with Windows 10 and Office 2019 Pro Plus (Server 2019 based)"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "bundle_id"),
resource.TestCheckResourceAttr(dataSourceName, "compute_type.#", acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, "compute_type.0.name", "PERFORMANCE"),
resource.TestCheckResourceAttrSet(dataSourceName, names.AttrDescription),
resource.TestCheckResourceAttr(dataSourceName, names.AttrName, "Performance with Windows 10 and Office 2019 Pro Plus (Server 2019 based)"),
resource.TestCheckResourceAttr(dataSourceName, names.AttrOwner, "Amazon"),
resource.TestCheckResourceAttr(dataSourceName, "root_storage.#", acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, "root_storage.0.capacity", "80"),
resource.TestCheckResourceAttr(dataSourceName, "user_storage.#", acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, "user_storage.0.capacity", "100"),
),
},
},
})
}

func testAccWorkspaceBundleDataSource_bundleIDAndNameConflict(t *testing.T) {
ctx := acctest.Context(t)
resource.Test(t, resource.TestCase{
Expand Down
1 change: 1 addition & 0 deletions internal/service/workspaces/workspaces_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAccWorkSpacesDataSource_serial(t *testing.T) {
acctest.CtBasic: testAccWorkspaceBundleDataSource_basic,
"bundleIDAndNameConflict": testAccWorkspaceBundleDataSource_bundleIDAndNameConflict,
"byOwnerName": testAccWorkspaceBundleDataSource_byOwnerName,
"byOwnerNameMultiple": testAccWorkspaceBundleDataSource_byOwnerNameMultiple,
"privateOwner": testAccWorkspaceBundleDataSource_privateOwner,
},
"Directory": {
Expand Down

0 comments on commit f8d4fe8

Please sign in to comment.