Skip to content

Commit 0fd9ebd

Browse files
committed
feat(workspace): add new data source to query associated desktop pools for users
1 parent bc71493 commit 0fd9ebd

File tree

4 files changed

+391
-0
lines changed

4 files changed

+391
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_user_desktop_pool_associations"
5+
description: |-
6+
Use this data source to query the desktop pools associated with users within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_workspace_user_desktop_pool_associations
10+
11+
Use this data source to query the desktop pools associated with users within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "user_ids" {
17+
type = list(string)
18+
}
19+
20+
data "huaweicloud_workspace_user_desktop_pool_associations" "test" {
21+
user_ids = var.user_ids
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
* `region` - (Optional, String) Specifies the region where the user desktop pool associations are located.
30+
If omitted, the provider-level region will be used.
31+
32+
* `user_ids` - (Required, List) Specifies the list of user IDs to be queried.
33+
34+
## Attribute Reference
35+
36+
In addition to all arguments above, the following attributes are exported:
37+
38+
* `id` - The data source ID.
39+
40+
* `associations` - The list of user associations with desktop pools.
41+
The [associations](#workspace_user_desktop_pool_associations) structure is documented below.
42+
43+
<a name="workspace_user_desktop_pool_associations"></a>
44+
The `associations` block supports:
45+
46+
* `user_id` - The ID of the user.
47+
48+
* `desktop_pools` - The list of desktop pools associated with the user.
49+
The [desktop_pools](#workspace_user_desktop_pool_associations_desktop_pools) structure is documented below.
50+
51+
<a name="workspace_user_desktop_pool_associations_desktop_pools"></a>
52+
The `desktop_pools` block supports:
53+
54+
* `desktop_pool_id` - The ID of the desktop pool.
55+
56+
* `desktop_pool_name` - The name of the desktop pool.
57+
58+
* `is_attached` - Whether a desktop is assigned.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,7 @@ func Provider() *schema.Provider {
22432243
"huaweicloud_workspace_tags": workspace.DataSourceTags(),
22442244
"huaweicloud_workspace_timezones": workspace.DataSourceTimeZones(),
22452245
"huaweicloud_workspace_users": workspace.DataSourceUsers(),
2246+
"huaweicloud_workspace_user_desktop_pool_associations": workspace.DataSourceUserDesktopPoolAssociations(),
22462247
"huaweicloud_workspace_volume_products": workspace.DataSourceVolumeProducts(),
22472248

22482249
// Workspace APP
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package workspace
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
10+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
11+
)
12+
13+
func TestAccDataUserDesktopPoolAssociations_basic(t *testing.T) {
14+
var (
15+
name = acceptance.RandomAccResourceNameWithDash()
16+
17+
dcName = "data.huaweicloud_workspace_user_desktop_pool_associations.test"
18+
dc = acceptance.InitDataSourceCheck(dcName)
19+
)
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() {
23+
acceptance.TestAccPreCheck(t)
24+
acceptance.TestAccPreCheckWorkspaceDesktopPoolImageId(t)
25+
},
26+
ProviderFactories: acceptance.TestAccProviderFactories,
27+
Steps: []resource.TestStep{
28+
{
29+
Config: testAccDataUserDesktopPoolAssociations_basic(name),
30+
Check: resource.ComposeTestCheckFunc(
31+
dc.CheckResourceExists(),
32+
resource.TestMatchResourceAttr(dcName, "associations.#", regexp.MustCompile(`^[0-9]+$`)),
33+
resource.TestCheckResourceAttrSet(dcName, "associations.0.user_id"),
34+
resource.TestMatchResourceAttr(dcName, "associations.0.desktop_pools.#", regexp.MustCompile(`^[0-9]+$`)),
35+
resource.TestCheckResourceAttrSet(dcName, "associations.0.desktop_pools.0.desktop_pool_id"),
36+
resource.TestCheckResourceAttrSet(dcName, "associations.0.desktop_pools.0.desktop_pool_name"),
37+
resource.TestCheckResourceAttrSet(dcName, "associations.0.desktop_pools.0.is_attached"),
38+
resource.TestCheckOutput("is_user_ids_filter_useful", "true"),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
func testAccDataUserDesktopPoolAssociations_base(name string) string {
46+
return fmt.Sprintf(`
47+
data "huaweicloud_workspace_service" "test" {}
48+
49+
data "huaweicloud_workspace_flavors" "test" {
50+
os_type = "Windows"
51+
}
52+
53+
locals {
54+
cpu_flavors = [for v in data.huaweicloud_workspace_flavors.test.flavors : v if v.is_gpu == false]
55+
}
56+
57+
data "huaweicloud_availability_zones" "test" {}
58+
59+
resource "huaweicloud_workspace_user" "test" {
60+
count = 2
61+
62+
name = "%[1]s${count.index}"
63+
email = "www.user${count.index}@test.com"
64+
}
65+
66+
resource "huaweicloud_workspace_desktop_pool" "test" {
67+
count = 2
68+
name = "%[1]s-${count.index}"
69+
type = "DYNAMIC"
70+
size = 1
71+
product_id = try(local.cpu_flavors[0].id, "")
72+
image_type = "gold"
73+
image_id = "%[2]s"
74+
subnet_ids = data.huaweicloud_workspace_service.test.network_ids
75+
vpc_id = data.huaweicloud_workspace_service.test.vpc_id
76+
availability_zone = data.huaweicloud_availability_zones.test.names[0]
77+
disconnected_retention_period = 10
78+
enable_autoscale = false
79+
80+
root_volume {
81+
type = "SAS"
82+
size = 80
83+
}
84+
85+
security_groups {
86+
id = data.huaweicloud_workspace_service.test.desktop_security_group[0].id
87+
}
88+
89+
dynamic "authorized_objects" {
90+
for_each = huaweicloud_workspace_user.test
91+
92+
content {
93+
object_type = "USER"
94+
object_id = authorized_objects.value.id
95+
object_name = authorized_objects.value.name
96+
user_group = "administrators"
97+
}
98+
}
99+
}`, name, acceptance.HW_WORKSPACE_DESKTOP_POOL_IMAGE_ID)
100+
}
101+
102+
func testAccDataUserDesktopPoolAssociations_basic(name string) string {
103+
return fmt.Sprintf(`
104+
%[1]s
105+
106+
locals {
107+
user_ids = huaweicloud_workspace_user.test[*].id
108+
}
109+
110+
data "huaweicloud_workspace_user_desktop_pool_associations" "test" {
111+
user_ids = local.user_ids
112+
113+
depends_on = [
114+
huaweicloud_workspace_desktop_pool.test
115+
]
116+
}
117+
118+
locals {
119+
query_result = [
120+
for v in data.huaweicloud_workspace_user_desktop_pool_associations.test.associations[*].user_id :
121+
contains(local.user_ids, v)
122+
]
123+
}
124+
125+
output "is_user_ids_filter_useful" {
126+
value = length(local.query_result) > 0 && alltrue(local.query_result)
127+
}`, testAccDataUserDesktopPoolAssociations_base(name))
128+
}

0 commit comments

Comments
 (0)