Skip to content

Commit 2e66f2b

Browse files
author
greyworld
committed
feat(workspace): add new data source query policy groups
1 parent 6c1291e commit 2e66f2b

File tree

4 files changed

+678
-0
lines changed

4 files changed

+678
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_policy_groups"
5+
description: |-
6+
Use this data source to get the list of Workspace policy groups within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_workspace_policy_groups
10+
11+
Use this data source to get the list of Workspace policy groups within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
### Basic Usage
16+
17+
```hcl
18+
data "huaweicloud_workspace_policy_groups" "test" {}
19+
```
20+
21+
### Filter policy groups by priority
22+
23+
```hcl
24+
variable "policy_priority" {}
25+
26+
data "huaweicloud_workspace_policy_groups" "test" {
27+
priority = var.policy_priority
28+
}
29+
```
30+
31+
### Filter policy groups by name
32+
33+
```hcl
34+
variable "policy_group_name" {}
35+
36+
data "huaweicloud_workspace_policy_groups" "test" {
37+
policy_group_name = var.policy_group_name
38+
}
39+
```
40+
41+
## Argument Reference
42+
43+
The following arguments are supported:
44+
45+
* `region` - (Optional, String) Specifies the region in which to query the data source.
46+
If omitted, the provider-level region will be used.
47+
48+
* `policy_group_id` - (Optional, String) Specifies the ID of the policy group.
49+
50+
* `policy_group_name` - (Optional, String) Specifies the name of the policy group.
51+
The name support fuzzy match.
52+
53+
* `priority` - (Optional, Int) Specifies the priority of the policy group.
54+
Defaults to **0**.
55+
56+
* `description` - (Optional, String) Specifies the description of the policy group.
57+
The description support fuzzy match.
58+
59+
## Attribute Reference
60+
61+
In addition to all arguments above, the following attributes are exported:
62+
63+
* `id` - The data source ID.
64+
65+
* `policy_groups` - The list of policy groups that match the filter parameters.
66+
The [policy_groups](#workspace_policy_groups_attr) structure is documented below.
67+
68+
<a name="workspace_policy_groups_attr"></a>
69+
The `policy_groups` block supports:
70+
71+
* `policy_group_id` - The ID of the policy group.
72+
73+
* `policy_group_name` - The name of the policy group.
74+
75+
* `priority` - The priority of the policy group.
76+
77+
* `update_time` - The update time of the policy group, in RFC3339 format.
78+
79+
* `description` - The description of the policy group.
80+
81+
* `policies` - The list of policy configurations.
82+
The [policies](#workspace_policy_groups_policies) structure is documented below.
83+
84+
* `targets` - The list of target configurations.
85+
The [targets](#workspace_policy_groups_targets) structure is documented below.
86+
87+
<a name="workspace_policy_groups_policies"></a>
88+
The `policies` block supports:
89+
90+
* `peripherals` - The peripheral device policies, in JSON format.
91+
92+
* `audio` - The audio policies, in JSON format.
93+
94+
* `client` - The client policies, in JSON format.
95+
96+
* `display` - The display policies, in JSON format.
97+
98+
* `file_and_clipboard` - The file and clipboard policies, in JSON format.
99+
100+
* `session` - The session policies, in JSON format.
101+
102+
* `virtual_channel` - The virtual channel policies, in JSON format.
103+
104+
* `watermark` - The watermark policies, in JSON format.
105+
106+
* `keyboard_mouse` - The keyboard and mouse policies, in JSON format.
107+
108+
* `seamless` - The general audio and video bypass policies, in JSON format.
109+
110+
* `personalized_data_mgmt` - The personalized data management policies, in JSON format.
111+
112+
* `custom` - The custom policies, in JSON format.
113+
114+
* `record_audit` - The screen recording audit policies, in JSON format.
115+
116+
<a name="workspace_policy_groups_targets"></a>
117+
The `targets` block supports:
118+
119+
* `target_id` - The ID of the target.
120+
121+
* `target_type` - The type of the target.
122+
123+
* `target_name` - The name of the target.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,7 @@ func Provider() *schema.Provider {
15071507
"huaweicloud_workspace_desktops": workspace.DataSourceDesktops(),
15081508
"huaweicloud_workspace_desktop_tags": workspace.DataSourceDesktopTags(),
15091509
"huaweicloud_workspace_flavors": workspace.DataSourceWorkspaceFlavors(),
1510+
"huaweicloud_workspace_policy_groups": workspace.DataSourcePolicyGroups(),
15101511
"huaweicloud_workspace_service": workspace.DataSourceService(),
15111512
"huaweicloud_workspace_tags": workspace.DataSourceTags(),
15121513

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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 TestAccDataSourcePolicyGroups_basic(t *testing.T) {
14+
var (
15+
name = acceptance.RandomAccResourceName()
16+
17+
dcName = "data.huaweicloud_workspace_policy_groups.all"
18+
dc = acceptance.InitDataSourceCheck(dcName)
19+
20+
filterById = "data.huaweicloud_workspace_policy_groups.filter_by_policy_group_id"
21+
dcFilterById = acceptance.InitDataSourceCheck(filterById)
22+
23+
filterByName = "data.huaweicloud_workspace_policy_groups.filter_by_policy_group_name"
24+
dcFilterByName = acceptance.InitDataSourceCheck(filterByName)
25+
26+
filterByPriority = "data.huaweicloud_workspace_policy_groups.filter_by_priority"
27+
dcFilterByPriority = acceptance.InitDataSourceCheck(filterByPriority)
28+
29+
filterByDescription = "data.huaweicloud_workspace_policy_groups.filter_by_description"
30+
dcFilterByDescription = acceptance.InitDataSourceCheck(filterByDescription)
31+
)
32+
33+
resource.Test(t, resource.TestCase{
34+
PreCheck: func() {
35+
acceptance.TestAccPreCheck(t)
36+
},
37+
ProviderFactories: acceptance.TestAccProviderFactories,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: testAccDataSourcePolicyGroups_basic(name),
41+
Check: resource.ComposeTestCheckFunc(
42+
// Query policy groups without any filter parameter
43+
dc.CheckResourceExists(),
44+
resource.TestMatchResourceAttr(dcName, "policy_groups.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
45+
resource.TestCheckResourceAttrSet(dcName, "policy_groups.0.policy_group_id"),
46+
resource.TestCheckResourceAttrSet(dcName, "policy_groups.0.policy_group_name"),
47+
resource.TestCheckResourceAttrSet(dcName, "policy_groups.0.priority"),
48+
resource.TestCheckResourceAttrSet(dcName, "policy_groups.0.update_time"),
49+
// Filter by ID
50+
dcFilterById.CheckResourceExists(),
51+
resource.TestCheckOutput("is_id_filter_useful", "true"),
52+
// Filter by name
53+
dcFilterByName.CheckResourceExists(),
54+
resource.TestCheckOutput("is_name_filter_useful", "true"),
55+
// Filter by priority
56+
dcFilterByPriority.CheckResourceExists(),
57+
resource.TestCheckOutput("is_priority_filter_useful", "true"),
58+
// Filter by description
59+
dcFilterByDescription.CheckResourceExists(),
60+
resource.TestCheckOutput("is_description_filter_useful", "true"),
61+
),
62+
},
63+
},
64+
})
65+
}
66+
67+
func testAccDataSourcePolicyGroups_basic(name string) string {
68+
return fmt.Sprintf(`
69+
%[1]s
70+
71+
data "huaweicloud_workspace_policy_groups" "all" {
72+
depends_on = [
73+
huaweicloud_workspace_policy_group.test,
74+
huaweicloud_workspace_policy_group.nontest,
75+
]
76+
}
77+
78+
locals {
79+
policy_group_id = try(data.huaweicloud_workspace_policy_groups.all.policy_groups[0].policy_group_id, "NOT_FOUND")
80+
policy_group_name = try(data.huaweicloud_workspace_policy_groups.all.policy_groups[0].policy_group_name, "NOT_FOUND")
81+
priority = try(data.huaweicloud_workspace_policy_groups.all.policy_groups[0].priority, -1)
82+
update_time = try(data.huaweicloud_workspace_policy_groups.all.policy_groups[0].update_time, "1900-01-01T01:01:01Z")
83+
description = try(data.huaweicloud_workspace_policy_groups.all.policy_groups[0].description, "")
84+
}
85+
86+
# Filter by policy group id
87+
data "huaweicloud_workspace_policy_groups" "filter_by_policy_group_id" {
88+
policy_group_id = local.policy_group_id
89+
90+
depends_on = [
91+
huaweicloud_workspace_policy_group.test,
92+
huaweicloud_workspace_policy_group.nontest,
93+
]
94+
}
95+
96+
locals {
97+
id_filter_result = [
98+
for v in data.huaweicloud_workspace_policy_groups.filter_by_policy_group_id.policy_groups[*].policy_group_id :
99+
v == local.policy_group_id
100+
]
101+
}
102+
103+
output "is_id_filter_useful" {
104+
value = length(local.id_filter_result) < 2 && alltrue(local.id_filter_result)
105+
}
106+
107+
# Filter by policy group name
108+
data "huaweicloud_workspace_policy_groups" "filter_by_policy_group_name" {
109+
policy_group_name = local.policy_group_name
110+
111+
depends_on = [
112+
huaweicloud_workspace_policy_group.test,
113+
huaweicloud_workspace_policy_group.nontest,
114+
]
115+
}
116+
117+
output "is_name_filter_useful" {
118+
value = length(data.huaweicloud_workspace_policy_groups.filter_by_policy_group_name.policy_groups) > 0
119+
}
120+
121+
# Filter by priority
122+
data "huaweicloud_workspace_policy_groups" "filter_by_priority" {
123+
priority = local.priority
124+
125+
depends_on = [
126+
huaweicloud_workspace_policy_group.test,
127+
huaweicloud_workspace_policy_group.nontest,
128+
]
129+
}
130+
131+
locals {
132+
priority_filter_result = [
133+
for v in data.huaweicloud_workspace_policy_groups.filter_by_priority.policy_groups[*].priority :
134+
v == local.priority
135+
]
136+
}
137+
138+
output "is_priority_filter_useful" {
139+
value = length(local.priority_filter_result) > 0 && alltrue(local.priority_filter_result)
140+
}
141+
142+
# Filter by description
143+
data "huaweicloud_workspace_policy_groups" "filter_by_description" {
144+
description = local.description
145+
146+
depends_on = [
147+
huaweicloud_workspace_policy_group.test,
148+
huaweicloud_workspace_policy_group.nontest,
149+
]
150+
}
151+
152+
locals {
153+
description_filter_result = [
154+
for v in data.huaweicloud_workspace_policy_groups.filter_by_description.policy_groups[*].description :
155+
strcontains(v, local.description)
156+
]
157+
}
158+
159+
output "is_description_filter_useful" {
160+
value = length(local.description_filter_result) > 0 && alltrue(local.description_filter_result)
161+
}
162+
`, testAccDataSourcePolicyGroups_base(name))
163+
}
164+
165+
func testAccDataSourcePolicyGroups_base(name string) string {
166+
return fmt.Sprintf(`
167+
resource "huaweicloud_workspace_user" "test" {
168+
name = "%[1]s"
169+
email = "www.%[1][email protected]"
170+
}
171+
172+
// The priority will automatically increment with the creation of the resource, no need to specify it manually.
173+
resource "huaweicloud_workspace_policy_group" "test" {
174+
name = "%[1]s"
175+
description = "Created by terraform script"
176+
177+
targets {
178+
type = "USER"
179+
id = huaweicloud_workspace_user.test.id
180+
name = huaweicloud_workspace_user.test.name
181+
}
182+
183+
policy {
184+
access_control {
185+
ip_access_control = "112.20.53.2|255.255.240.0;112.20.53.3|255.255.240.0"
186+
}
187+
}
188+
}
189+
190+
resource "huaweicloud_workspace_policy_group" "nontest" {
191+
name = "non_%[1]s"
192+
193+
targets {
194+
type = "USER"
195+
id = huaweicloud_workspace_user.test.id
196+
name = huaweicloud_workspace_user.test.name
197+
}
198+
199+
policy {
200+
access_control {
201+
ip_access_control = "112.20.53.2|255.255.240.0;112.20.53.3|255.255.240.0"
202+
}
203+
}
204+
}
205+
`, name)
206+
}

0 commit comments

Comments
 (0)