Skip to content

Commit 6648466

Browse files
committed
feat(xxx): add new one-time action resource to update tenant profile
1 parent 61c5e02 commit 6648466

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_app_action"
5+
description: |-
6+
Manages a Workspace app action resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_workspace_app_action
10+
11+
Manages a Workspace app action resource within HuaweiCloud.
12+
13+
-> This resource is used to manage the tenant profile settings. Deleting this resource will not
14+
clear the corresponding configuration, but will only remove the resource information from the tfstate file.
15+
16+
## Example Usage
17+
18+
```hcl
19+
resource "huaweicloud_workspace_app_action" "test" {
20+
app_restrict_rule_switch = true
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `region` - (Optional, String, ForceNew) Specifies the region where the tenant profiles are located.
29+
If omitted, the provider-level region will be used. Changing this parameter will create a new resource.
30+
31+
* `app_restrict_rule_switch` - (Required, Bool, NonUpdatable) Specifies whether to enable the application restriction
32+
rule switch.
33+
+ **true**: enable
34+
+ **false**: disable
35+
36+
## Attributes Reference
37+
38+
In addition to all arguments above, the following attributes are exported:
39+
40+
* `id` - The resource ID.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,6 +3740,7 @@ func Provider() *schema.Provider {
37403740

37413741
// Workspace
37423742
"huaweicloud_workspace_access_policy": workspace.ResourceAccessPolicy(),
3743+
"huaweicloud_workspace_app_action": workspace.ResourceAppAction(),
37433744
"huaweicloud_workspace_application": workspace.ResourceApplication(),
37443745
"huaweicloud_workspace_application_batch_authorize": workspace.ResourceApplicationBatchAuthorize(),
37453746
"huaweicloud_workspace_application_batch_auto_install": workspace.ResourceApplicationBatchAutoInstall(),
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 TestAccAppAction_basic(t *testing.T) {
12+
var (
13+
resourceName = "huaweicloud_workspace_app_action.test"
14+
)
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() {
18+
acceptance.TestAccPreCheck(t)
19+
},
20+
ProviderFactories: acceptance.TestAccProviderFactories,
21+
// This resource is used to manage the tenant profile settings. Deleting this resource will not
22+
// clear the corresponding configuration, but will only remove the resource information from the tfstate file.
23+
// lintignore:AT001
24+
CheckDestroy: nil,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccAppAction_basic(),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr(resourceName, "app_restrict_rule_switch", "true"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccAppAction_basic() string {
37+
return `
38+
resource "huaweicloud_workspace_app_action" "test" {
39+
app_restrict_rule_switch = true
40+
}
41+
`
42+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package workspace
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-uuid"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
12+
"github.com/chnsz/golangsdk"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
17+
)
18+
19+
var appActionNonUpdatableParams = []string{
20+
"app_restrict_rule_switch",
21+
}
22+
23+
// @API Workspace PATCH /v1/{project_id}/app-center/profiles
24+
// @API Workspace GET /v1/{project_id}/app-center/profiles
25+
func ResourceAppAction() *schema.Resource {
26+
return &schema.Resource{
27+
CreateContext: resourceAppActionCreate,
28+
ReadContext: resourceAppActionRead,
29+
UpdateContext: resourceAppActionUpdate,
30+
DeleteContext: resourceAppActionDelete,
31+
32+
CustomizeDiff: config.FlexibleForceNew(appActionNonUpdatableParams),
33+
34+
Schema: map[string]*schema.Schema{
35+
"region": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Computed: true,
39+
ForceNew: true,
40+
Description: `The region where the tenant profiles are located.`,
41+
},
42+
"app_restrict_rule_switch": {
43+
Type: schema.TypeBool,
44+
Required: true,
45+
Description: `Whether to enable the application restriction rule switch.`,
46+
},
47+
48+
// Internal parameters.
49+
"enable_force_new": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false),
53+
Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}),
54+
},
55+
},
56+
}
57+
}
58+
59+
func buildAppActionBodyParams(d *schema.ResourceData) map[string]interface{} {
60+
body := map[string]interface{}{
61+
"app_restrict_rule_switch": d.Get("app_restrict_rule_switch"),
62+
}
63+
return body
64+
}
65+
66+
func resourceAppActionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
67+
var (
68+
cfg = meta.(*config.Config)
69+
region = cfg.GetRegion(d)
70+
httpUrl = "v1/{project_id}/app-center/profiles"
71+
)
72+
73+
client, err := cfg.NewServiceClient("workspace", region)
74+
if err != nil {
75+
return diag.Errorf("error creating Workspace client: %s", err)
76+
}
77+
78+
createPath := client.Endpoint + httpUrl
79+
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
80+
81+
opt := golangsdk.RequestOpts{
82+
KeepResponseBody: true,
83+
MoreHeaders: map[string]string{
84+
"Content-Type": "application/json",
85+
},
86+
JSONBody: buildAppActionBodyParams(d),
87+
}
88+
89+
_, err = client.Request("PATCH", createPath, &opt)
90+
if err != nil {
91+
return diag.Errorf("error creating app action: %s", err)
92+
}
93+
94+
randomUUID, err := uuid.GenerateUUID()
95+
if err != nil {
96+
return diag.Errorf("unable to generate ID: %s", err)
97+
}
98+
d.SetId(randomUUID)
99+
100+
return resourceAppActionRead(ctx, d, meta)
101+
}
102+
103+
func resourceAppActionRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
104+
var (
105+
cfg = meta.(*config.Config)
106+
region = cfg.GetRegion(d)
107+
httpUrl = "v1/{project_id}/app-center/profiles"
108+
)
109+
110+
client, err := cfg.NewServiceClient("workspace", region)
111+
if err != nil {
112+
return diag.Errorf("error creating Workspace client: %s", err)
113+
}
114+
115+
getPath := client.Endpoint + httpUrl
116+
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
117+
118+
opt := golangsdk.RequestOpts{
119+
KeepResponseBody: true,
120+
MoreHeaders: map[string]string{
121+
"Content-Type": "application/json",
122+
},
123+
}
124+
125+
response, err := client.Request("GET", getPath, &opt)
126+
if err != nil {
127+
return common.CheckDeletedDiag(d, err, "error retrieving app action")
128+
}
129+
130+
respBody, err := utils.FlattenResponse(response)
131+
if err != nil {
132+
return diag.FromErr(err)
133+
}
134+
135+
appRestrictRuleSwitch := utils.PathSearch("app_restrict_rule_switch", respBody, false)
136+
mErr := d.Set("app_restrict_rule_switch", appRestrictRuleSwitch.(bool))
137+
if mErr != nil {
138+
return diag.Errorf("error setting app_restrict_rule_switch: %s", mErr)
139+
}
140+
141+
return nil
142+
}
143+
144+
func resourceAppActionUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
145+
return nil
146+
}
147+
148+
func resourceAppActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
149+
errorMsg := `This resource is used to manage the tenant profile settings. Deleting this resource will not
150+
clear the corresponding configuration, but will only remove the resource information from the tfstate file.`
151+
return diag.Diagnostics{
152+
diag.Diagnostic{
153+
Severity: diag.Warning,
154+
Summary: errorMsg,
155+
},
156+
}
157+
}

0 commit comments

Comments
 (0)