Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_workspaces_bundle: Fix multiple WorkSpaces Bundles matched; use additional constraints to reduce matches to a single WorkSpaces Bundle regression #39777

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading