Skip to content

Commit aa8cd6c

Browse files
committed
feat(eps): add new resource to operate enterprise project
1 parent a94685f commit aa8cd6c

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "Enterprise Project Management Service (EPS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_enterprise_project_action"
5+
description: |-
6+
Use this resource to operate the enterprise project within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_enterprise_project_action
10+
11+
Use this resource to operate the enterprise project within HuaweiCloud.
12+
13+
-> This resource is only a one-time action resource for operating the enterprise project. Deleting this resource will
14+
not clear the corresponding request record, but will only remove the resource information from the tfstate file.
15+
16+
## Example Usage
17+
18+
```hcl
19+
variable "enterprise_project_id" {}
20+
21+
resource "huaweicloud_enterprise_project_action" "test" {
22+
enterprise_project_id = var.enterprise_project_id
23+
action = "disable"
24+
}
25+
```
26+
27+
## Argument Reference
28+
29+
* `enterprise_project_id` - (Required, String) Specifies the ID of enterprise project to be operated.
30+
31+
* `action` - (Required, String) Specifies the action type.
32+
The valid values are as follows:
33+
+ **enable**
34+
+ **disable**
35+
36+
## Attribute Reference
37+
38+
In addition to all arguments above, the following attributes are exported:
39+
40+
* `id` - The resource ID.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,7 @@ func Provider() *schema.Provider {
19091909
"huaweicloud_elb_security_policy": elb.ResourceSecurityPolicy(),
19101910

19111911
"huaweicloud_enterprise_project": eps.ResourceEnterpriseProject(),
1912+
"huaweicloud_enterprise_project_action": eps.ResourceAction(),
19121913
"huaweicloud_enterprise_project_authority": eps.ResourceAuthority(),
19131914

19141915
"huaweicloud_er_association": er.ResourceAssociation(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package eps
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccAction_basic(t *testing.T) {
13+
resource.ParallelTest(t, resource.TestCase{
14+
PreCheck: func() {
15+
acceptance.TestAccPreCheck(t)
16+
// Please prepare an enterprise project and the status is enable.
17+
acceptance.TestAccPreCheckEpsID(t)
18+
},
19+
ProviderFactories: acceptance.TestAccProviderFactories,
20+
// This resource is a one-time action resource and there is no logic in the delete method.
21+
// lintignore:AT001
22+
CheckDestroy: nil,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccAction_basic_step1(),
26+
},
27+
{
28+
Config: testAccAction_basic_step2(),
29+
},
30+
},
31+
})
32+
}
33+
34+
func testAccAction_basic_step1() string {
35+
return fmt.Sprintf(`
36+
resource "huaweicloud_enterprise_project_action" "disable" {
37+
enterprise_project_id = "%[1]s"
38+
action = "disable"
39+
}
40+
`, acceptance.HW_ENTERPRISE_MIGRATE_PROJECT_ID_TEST)
41+
}
42+
43+
func testAccAction_basic_step2() string {
44+
return fmt.Sprintf(`
45+
resource "huaweicloud_enterprise_project_action" "enable" {
46+
enterprise_project_id = "%[1]s"
47+
action = "disable"
48+
}
49+
`, acceptance.HW_ENTERPRISE_MIGRATE_PROJECT_ID_TEST)
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package eps
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/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
16+
)
17+
18+
var actionNonUpdatableParams = []string{
19+
"enterprise_project_id",
20+
"action",
21+
}
22+
23+
// @API ER POST /v1.0/enterprise-projects/{enterprise_project_id}/action
24+
func ResourceAction() *schema.Resource {
25+
return &schema.Resource{
26+
CreateContext: resourceActionCreate,
27+
UpdateContext: resourceActionUpdate,
28+
ReadContext: resourceActionRead,
29+
DeleteContext: resourceActionDelete,
30+
31+
CustomizeDiff: config.FlexibleForceNew(actionNonUpdatableParams),
32+
33+
Schema: map[string]*schema.Schema{
34+
"region": {
35+
Type: schema.TypeString,
36+
Optional: true,
37+
Computed: true,
38+
ForceNew: true,
39+
},
40+
"enterprise_project_id": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
Description: `The ID of enterprise project to be operated.`,
44+
},
45+
"action": {
46+
Type: schema.TypeString,
47+
Required: true,
48+
Description: `The action type.`,
49+
},
50+
"enable_force_new": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false),
54+
Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}),
55+
},
56+
},
57+
}
58+
}
59+
60+
func resourceActionCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
61+
cfg := meta.(*config.Config)
62+
client, err := cfg.NewServiceClient("eps", cfg.GetRegion(d))
63+
if err != nil {
64+
return diag.Errorf("error creating EPS client: %s", err)
65+
}
66+
67+
var (
68+
httpUrl = "v1.0/enterprise-projects/{enterprise_project_id}/action"
69+
epsId = d.Get("enterprise_project_id").(string)
70+
action = d.Get("action").(string)
71+
)
72+
createPath := client.Endpoint + httpUrl
73+
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
74+
createPath = strings.ReplaceAll(createPath, "{enterprise_project_id}", epsId)
75+
76+
createOpt := golangsdk.RequestOpts{
77+
KeepResponseBody: true,
78+
MoreHeaders: map[string]string{
79+
"Content-Type": "application/json",
80+
},
81+
OkCodes: []int{204},
82+
JSONBody: map[string]interface{}{
83+
"action": action,
84+
},
85+
}
86+
_, err = client.Request("POST", createPath, &createOpt)
87+
if err != nil {
88+
return diag.Errorf("unable to %s the enterprise project (%s): %s", action, epsId, err)
89+
}
90+
91+
randUUID, err := uuid.GenerateUUID()
92+
if err != nil {
93+
return diag.Errorf("unable to generate ID: %s", err)
94+
}
95+
d.SetId(randUUID)
96+
97+
return nil
98+
}
99+
100+
func resourceActionRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
101+
return nil
102+
}
103+
104+
func resourceActionUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
105+
return nil
106+
}
107+
108+
func resourceActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
109+
errorMsg := `This resource is only a one-time action resource for operating the enterprise project. Deleting this
110+
resource will not clear the corresponding request record, but will only remove the resource information from the tfstate
111+
file.`
112+
return diag.Diagnostics{
113+
diag.Diagnostic{
114+
Severity: diag.Warning,
115+
Summary: errorMsg,
116+
},
117+
}
118+
}

0 commit comments

Comments
 (0)