Skip to content

Commit f059ebe

Browse files
authored
feat(workspace/app): add new data source to query policy groups (#8473)
1 parent e7c6a6b commit f059ebe

File tree

4 files changed

+496
-0
lines changed

4 files changed

+496
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_app_policy_groups"
5+
description: |-
6+
Use this data source to get policy group list of the Workspace APP within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_workspace_app_policy_groups
10+
11+
Use this data source to get policy group list of the Workspace APP within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
### Query all policy groups
16+
17+
```hcl
18+
data "huaweicloud_workspace_app_policy_groups" "test" {}
19+
```
20+
21+
### Query policy groups by type
22+
23+
```hcl
24+
data "huaweicloud_workspace_app_policy_groups" "test" {
25+
policy_group_type = 4
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
The following arguments are supported:
32+
33+
* `region` - (Optional, String) Specifies the region where the policy groups are located.
34+
If omitted, the provider-level region will be used.
35+
36+
* `policy_group_name` - (Optional, String) Specifies the name of the policy group.
37+
Fuzzy search is supported.
38+
39+
* `policy_group_type` - (Optional, Int) Specifies the type of the policy group.
40+
The valid values are as follows:
41+
+ **0**: VM class.
42+
+ **4**: Custom policy template.
43+
44+
Defaults to **0**.
45+
46+
## Attribute Reference
47+
48+
In addition to all arguments above, the following attributes are exported:
49+
50+
* `id` - The data source ID.
51+
52+
* `policy_groups` - The list of policy groups that match the filter parameters.
53+
The [policy_groups](#app_policy_groups) structure is documented below.
54+
55+
<a name="app_policy_groups"></a>
56+
The `policy_groups` block supports:
57+
58+
* `id` - The ID of the policy group.
59+
60+
* `name` - The name of the policy group.
61+
62+
* `priority` - The priority of the policy group.
63+
64+
* `description` - The description of the policy group.
65+
66+
* `targets` - The list of target objects.
67+
The [targets](#app_policy_groups_targets) structure is documented below.
68+
69+
* `policies` - The policies of the policy group, in JSON format.
70+
71+
* `created_at` - The creation time of the policy group, in RFC3339 format.
72+
73+
* `updated_at` - The latest update time of the policy group, in RFC3339 format.
74+
75+
<a name="app_policy_groups_targets"></a>
76+
The `targets` block supports:
77+
78+
* `id` - The ID of the target object.
79+
80+
* `name` - The name of the target object.
81+
82+
* `type` - The type of the target object.
83+
+ **USER**
84+
+ **USERGROUP**
85+
+ **APPGROUP**
86+
+ **OU**
87+
+ **ALL**

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,7 @@ func Provider() *schema.Provider {
22822282
"huaweicloud_workspace_app_image_servers": workspace.DataSourceWorkspaceAppImageServers(),
22832283
"huaweicloud_workspace_app_latest_attached_applications": workspace.DataSourceLatestAttachedApplications(),
22842284
"huaweicloud_workspace_app_nas_storages": workspace.DataSourceAppNasStorages(),
2285+
"huaweicloud_workspace_app_policy_groups": workspace.DataSourceAppPolicyGroups(),
22852286
"huaweicloud_workspace_app_publishable_applications": workspace.DataSourceAppPublishableApplications(),
22862287
"huaweicloud_workspace_app_schedule_tasks": workspace.DataSourceAppScheduleTasks(),
22872288
"huaweicloud_workspace_app_schedule_task_executions": workspace.DataSourceAppScheduleTaskExecutions(),
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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 TestAccDataAppPolicyGroups_basic(t *testing.T) {
14+
var (
15+
name = acceptance.RandomAccResourceName()
16+
17+
all = "data.huaweicloud_workspace_app_policy_groups.all"
18+
dc = acceptance.InitDataSourceCheck(all)
19+
20+
filterByPolicyGroupName = "data.huaweicloud_workspace_app_policy_groups.filter_by_policy_group_name"
21+
dcFilterByPolicyGroupName = acceptance.InitDataSourceCheck(filterByPolicyGroupName)
22+
23+
filterByPolicyGroupType = "data.huaweicloud_workspace_app_policy_groups.filter_by_policy_group_type"
24+
dcFilterByPolicyGroupType = acceptance.InitDataSourceCheck(filterByPolicyGroupType)
25+
)
26+
27+
resource.ParallelTest(t, resource.TestCase{
28+
PreCheck: func() {
29+
acceptance.TestAccPreCheck(t)
30+
acceptance.TestAccPreCheckWorkspaceAppServerGroup(t)
31+
},
32+
ProviderFactories: acceptance.TestAccProviderFactories,
33+
Steps: []resource.TestStep{
34+
{
35+
Config: testAccDataAppPolicyGroups_basic(name),
36+
Check: resource.ComposeTestCheckFunc(
37+
// Without any filter parameter.
38+
dc.CheckResourceExists(),
39+
resource.TestMatchResourceAttr(all, "policy_groups.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
40+
// Filter by 'policy_group_name' parameter.
41+
dcFilterByPolicyGroupName.CheckResourceExists(),
42+
resource.TestCheckOutput("is_policy_group_name_filter_useful", "true"),
43+
// Filter by 'policy_group_type' parameter.
44+
dcFilterByPolicyGroupType.CheckResourceExists(),
45+
resource.TestCheckOutput("is_policy_group_type_filter_useful", "true"),
46+
// Check attributes.
47+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.id"),
48+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.name"),
49+
resource.TestMatchResourceAttr(filterByPolicyGroupName, "policy_groups.0.targets.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
50+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.targets.0.id"),
51+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.targets.0.name"),
52+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.targets.0.type"),
53+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.policies"),
54+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.priority"),
55+
resource.TestCheckResourceAttrSet(filterByPolicyGroupName, "policy_groups.0.description"),
56+
resource.TestMatchResourceAttr(filterByPolicyGroupName, "policy_groups.0.created_at",
57+
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
58+
resource.TestMatchResourceAttr(filterByPolicyGroupName, "policy_groups.0.updated_at",
59+
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
60+
),
61+
},
62+
},
63+
})
64+
}
65+
66+
func testAccDataAppPolicyGroups_base(name string) string {
67+
return fmt.Sprintf(`
68+
data "huaweicloud_workspace_service" "test" {}
69+
70+
resource "huaweicloud_workspace_app_server_group" "test" {
71+
name = "%[1]s"
72+
os_type = "Windows"
73+
app_type = "SESSION_DESKTOP_APP"
74+
flavor_id = "%[2]s"
75+
vpc_id = data.huaweicloud_workspace_service.test.vpc_id
76+
subnet_id = data.huaweicloud_workspace_service.test.network_ids[0]
77+
system_disk_type = "SAS"
78+
system_disk_size = 90
79+
is_vdi = true
80+
image_id = "%[3]s"
81+
image_type = "gold"
82+
image_product_id = "%[4]s"
83+
}
84+
85+
resource "huaweicloud_workspace_app_group" "test" {
86+
name = "%[1]s"
87+
type = "SESSION_DESKTOP_APP"
88+
server_group_id = huaweicloud_workspace_app_server_group.test.id
89+
}
90+
91+
resource "huaweicloud_workspace_app_policy_group" "test" {
92+
name = "%[1]s"
93+
description = "Created by terraform script"
94+
95+
targets {
96+
id = huaweicloud_workspace_app_group.test.id
97+
name = huaweicloud_workspace_app_group.test.name
98+
type = "APPGROUP"
99+
}
100+
101+
policies = jsonencode({
102+
client = {
103+
automatic_reconnection_interval = 10
104+
session_persistence_time = 120
105+
forbid_screen_capture = true
106+
}
107+
})
108+
}
109+
110+
resource "huaweicloud_workspace_app_policy_template" "test" {
111+
name = "%[1]s"
112+
113+
policies = jsonencode({
114+
client = {
115+
automatic_reconnection_interval = 10
116+
session_persistence_time = 120
117+
forbid_screen_capture = true
118+
}
119+
})
120+
}
121+
`, name, acceptance.HW_WORKSPACE_APP_SERVER_GROUP_FLAVOR_ID,
122+
acceptance.HW_WORKSPACE_APP_SERVER_GROUP_IMAGE_ID,
123+
acceptance.HW_WORKSPACE_APP_SERVER_GROUP_IMAGE_PRODUCT_ID)
124+
}
125+
126+
func testAccDataAppPolicyGroups_basic(name string) string {
127+
return fmt.Sprintf(`
128+
%[1]s
129+
130+
# Without any filter parameter.
131+
data "huaweicloud_workspace_app_policy_groups" "all" {
132+
depends_on = [huaweicloud_workspace_app_policy_group.test]
133+
}
134+
135+
# Filter by 'policy_group_name' parameter.
136+
locals {
137+
policy_group_name = huaweicloud_workspace_app_policy_group.test.name
138+
}
139+
140+
data "huaweicloud_workspace_app_policy_groups" "filter_by_policy_group_name" {
141+
policy_group_name = local.policy_group_name
142+
143+
depends_on = [huaweicloud_workspace_app_policy_group.test]
144+
}
145+
146+
locals {
147+
policy_group_name_filter_result = [
148+
for v in data.huaweicloud_workspace_app_policy_groups.filter_by_policy_group_name.policy_groups[*].name :
149+
strcontains(v, local.policy_group_name)
150+
]
151+
}
152+
153+
output "is_policy_group_name_filter_useful" {
154+
value = length(local.policy_group_name_filter_result) > 0 && alltrue(local.policy_group_name_filter_result)
155+
}
156+
157+
# Filter by 'policy_group_type' parameter.
158+
data "huaweicloud_workspace_app_policy_groups" "filter_by_policy_group_type" {
159+
policy_group_type = 4
160+
161+
depends_on = [huaweicloud_workspace_app_policy_template.test]
162+
}
163+
164+
locals {
165+
policy_group_type_filter_result = data.huaweicloud_workspace_app_policy_groups.filter_by_policy_group_type.policy_groups[*].name
166+
}
167+
168+
output "is_policy_group_type_filter_useful" {
169+
value = (
170+
length(local.policy_group_type_filter_result) > 0 &&
171+
contains(local.policy_group_type_filter_result, huaweicloud_workspace_app_policy_template.test.name)
172+
)
173+
}
174+
`, testAccDataAppPolicyGroups_base(name))
175+
}

0 commit comments

Comments
 (0)