Skip to content

Commit ea90cfb

Browse files
committed
feat(workspace/app): add a new data source to query original policy
1 parent f059ebe commit ea90cfb

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_app_original_policy"
5+
description: |-
6+
Use this data source to get the original policy of the Workspace APP within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_workspace_app_original_policy
10+
11+
Use this data source to get the original policy of the Workspace APP within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_workspace_app_original_policy" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region where the original policy is located.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `policies` - The original policy configuration, in JSON format.

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_original_policy": workspace.DataSourceAppOriginalPolicy(),
22852286
"huaweicloud_workspace_app_policy_groups": workspace.DataSourceAppPolicyGroups(),
22862287
"huaweicloud_workspace_app_publishable_applications": workspace.DataSourceAppPublishableApplications(),
22872288
"huaweicloud_workspace_app_schedule_tasks": workspace.DataSourceAppScheduleTasks(),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package workspace
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDataAppOriginalPolicy_basic(t *testing.T) {
12+
var (
13+
dcName = "data.huaweicloud_workspace_app_original_policy.test"
14+
dc = acceptance.InitDataSourceCheck(dcName)
15+
)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
19+
ProviderFactories: acceptance.TestAccProviderFactories,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDataAppOriginalPolicy_basic,
23+
Check: resource.ComposeTestCheckFunc(
24+
dc.CheckResourceExists(),
25+
resource.TestCheckResourceAttrSet(dcName, "policies"),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccDataAppOriginalPolicy_basic = `
33+
data "huaweicloud_workspace_app_original_policy" "test" {}
34+
`
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package workspace
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/chnsz/golangsdk"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
16+
)
17+
18+
// @API Workspace GET /v1/{project_id}/policy-groups/actions/list-original-policy
19+
func DataSourceAppOriginalPolicy() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceAppOriginalPolicyRead,
22+
Schema: map[string]*schema.Schema{
23+
"region": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Computed: true,
27+
Description: `The region where the original policy is located.`,
28+
},
29+
"policies": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
Description: `The original policy configuration, in JSON format.`,
33+
},
34+
},
35+
}
36+
}
37+
38+
func dataSourceAppOriginalPolicyRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
39+
var (
40+
cfg = meta.(*config.Config)
41+
region = cfg.GetRegion(d)
42+
httpUrl = "v1/{project_id}/policy-groups/actions/list-original-policy"
43+
)
44+
45+
client, err := cfg.NewServiceClient("appstream", region)
46+
if err != nil {
47+
return diag.Errorf("error creating Workspace APP client: %s", err)
48+
}
49+
50+
getPath := client.Endpoint + httpUrl
51+
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
52+
opt := golangsdk.RequestOpts{
53+
KeepResponseBody: true,
54+
MoreHeaders: map[string]string{
55+
"Content-Type": "application/json;charset=UTF-8",
56+
},
57+
}
58+
59+
requestResp, err := client.Request("GET", getPath, &opt)
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
respBody, err := utils.FlattenResponse(requestResp)
65+
if err != nil {
66+
return diag.Errorf("error querying original policy information: %s", err)
67+
}
68+
69+
randomUUID, err := uuid.GenerateUUID()
70+
if err != nil {
71+
return diag.Errorf("unable to generate ID: %s", err)
72+
}
73+
74+
d.SetId(randomUUID)
75+
76+
mErr := multierror.Append(
77+
d.Set("region", region),
78+
d.Set("policies", utils.JsonToString(utils.PathSearch("policies", respBody, nil))),
79+
)
80+
81+
return diag.FromErr(mErr.ErrorOrNil())
82+
}

0 commit comments

Comments
 (0)