Skip to content

Commit 74dfe75

Browse files
committed
feat(workspace): add new resource to amanage ou
1 parent 11aee55 commit 74dfe75

File tree

5 files changed

+462
-1
lines changed

5 files changed

+462
-1
lines changed

docs/resources/workspace_ou.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
subcategory: "Workspace"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_workspace_ou"
5+
description: |-
6+
Manages an OU resource of Workspace within HuaweiCloud.
7+
---
8+
# huaweicloud_workspace_ou
9+
10+
Manages an OU resource of Workspace within HuaweiCloud.
11+
12+
-> Before creating an OU, you need to create OUs on the AD server and register the Workspace service
13+
to connect to the AD domain.
14+
15+
## Example Usage
16+
17+
```hcl
18+
variable "ou_name" {}
19+
variable "ad_domain_name" {}
20+
21+
resource "huaweicloud_workspace_ou" "test" {
22+
name = var.ou_name
23+
domain = var.ad_domain_name
24+
}
25+
```
26+
27+
## Argument Reference
28+
29+
The following arguments are supported:
30+
31+
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
32+
If omitted, the provider-level region will be used.
33+
Changing this creates a new resource.
34+
35+
* `name` - (Required, String) Specifies the name of the OU.
36+
The name only allows Chinese and English characters, digits, spaces and special characters (-_$!@*?.).
37+
Multiple levels of OUs are separated by slashes (/) and no space is allowed before or after the slashes (/),
38+
a maximum of five layers of OUs are supported. e.g. `ab/cd/ef`.
39+
40+
* `domain` - (Required, String, ForceNew) Specifies the AD domain name to which the OU belongs.
41+
42+
* `description` - (Optional, String) Specifies the description of the OU.
43+
44+
## Attribute Reference
45+
46+
In addition to all arguments above, the following attributes are exported:
47+
48+
* `id` - The resource ID, also OU ID.
49+
50+
## Import
51+
52+
The OU resource can be imported using `name`, e.g.
53+
54+
```bash
55+
$ terraform import huaweicloud_workspace_ou.test <name>
56+
```

huaweicloud/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,11 +2291,12 @@ func Provider() *schema.Provider {
22912291
"huaweicloud_workspace_access_policy": workspace.ResourceAccessPolicy(),
22922292
"huaweicloud_workspace_desktop_name_rule": workspace.ResourceDesktopNameRule(),
22932293
"huaweicloud_workspace_desktop": workspace.ResourceDesktop(),
2294+
"huaweicloud_workspace_eip_associate": workspace.ResourceEipAssociate(),
2295+
"huaweicloud_workspace_ou": workspace.ResourceOu(),
22942296
"huaweicloud_workspace_policy_group": workspace.ResourcePolicyGroup(),
22952297
"huaweicloud_workspace_service": workspace.ResourceService(),
22962298
"huaweicloud_workspace_terminal_binding": workspace.ResourceTerminalBinding(),
22972299
"huaweicloud_workspace_user": workspace.ResourceUser(),
2298-
"huaweicloud_workspace_eip_associate": workspace.ResourceEipAssociate(),
22992300

23002301
"huaweicloud_cpts_project": cpts.ResourceProject(),
23012302
"huaweicloud_cpts_task": cpts.ResourceTask(),

huaweicloud/services/acceptance/acceptance.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ var (
234234
HW_WORKSPACE_APP_SERVER_GROUP_IMAGE_PRODUCT_ID = os.Getenv("HW_WORKSPACE_APP_SERVER_GROUP_IMAGE_PRODUCT_ID")
235235
HW_WORKSPACE_APP_SERVER_GROUP_IMAGE_SPEC_CODE = os.Getenv("HW_WORKSPACE_APP_SERVER_GROUP_IMAGE_SPEC_CODE")
236236
HW_WORKSPACE_OU_NAME = os.Getenv("HW_WORKSPACE_OU_NAME")
237+
HW_WORKSPACE_OU_UPDATE_NAME = os.Getenv("HW_WORKSPACE_OU_UPDATE_NAME")
237238
HW_WORKSPACE_APP_FILE_NAME = os.Getenv("HW_WORKSPACE_APP_FILE_NAME")
238239
HW_WORKSPACE_USER_NAMES = os.Getenv("HW_WORKSPACE_USER_NAMES")
239240

@@ -1622,6 +1623,12 @@ func TestAccPreCheckWorkspaceOUName(t *testing.T) {
16221623
}
16231624
}
16241625

1626+
func TestAccPreCheckWorkspaceOUUpdateName(t *testing.T) {
1627+
if HW_WORKSPACE_OU_UPDATE_NAME == "" {
1628+
t.Skip("HW_WORKSPACE_OU_UPDATE_NAME must be set for Workspace service acceptance tests.")
1629+
}
1630+
}
1631+
16251632
// lintignore:AT003
16261633
func TestAccPreCheckWorkspaceAppFileName(t *testing.T) {
16271634
if HW_WORKSPACE_APP_FILE_NAME == "" {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package workspace
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
10+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
11+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
12+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/workspace"
13+
)
14+
15+
func getOuFun(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
16+
client, err := conf.NewServiceClient("workspace", acceptance.HW_REGION_NAME)
17+
if err != nil {
18+
return nil, fmt.Errorf("error creating Workspace client: %s", err)
19+
}
20+
21+
return workspace.GetOuByName(client, state.Primary.Attributes["name"])
22+
}
23+
24+
func TestAccOu_basic(t *testing.T) {
25+
var (
26+
ouObj interface{}
27+
resourceName = "huaweicloud_workspace_ou.test"
28+
rc = acceptance.InitResourceCheck(
29+
resourceName,
30+
&ouObj,
31+
getOuFun,
32+
)
33+
)
34+
35+
resource.ParallelTest(t, resource.TestCase{
36+
PreCheck: func() {
37+
acceptance.TestAccPreCheck(t)
38+
acceptance.TestAccPreCheckWorkspaceAD(t)
39+
acceptance.TestAccPreCheckWorkspaceOUName(t)
40+
acceptance.TestAccPreCheckWorkspaceOUUpdateName(t)
41+
},
42+
ProviderFactories: acceptance.TestAccProviderFactories,
43+
CheckDestroy: rc.CheckResourceDestroy(),
44+
Steps: []resource.TestStep{
45+
{
46+
Config: testAccOu_step1(),
47+
Check: resource.ComposeTestCheckFunc(
48+
rc.CheckResourceExists(),
49+
resource.TestCheckResourceAttr(resourceName, "name", acceptance.HW_WORKSPACE_OU_NAME),
50+
resource.TestCheckResourceAttrPair(resourceName, "domain",
51+
"huaweicloud_workspace_service.test", "ad_domain.0.name"),
52+
resource.TestCheckResourceAttr(resourceName, "description", "Created by terraform script"),
53+
),
54+
},
55+
{
56+
Config: testAccOu_step2(),
57+
Check: resource.ComposeTestCheckFunc(
58+
rc.CheckResourceExists(),
59+
resource.TestCheckResourceAttr(resourceName, "name", acceptance.HW_WORKSPACE_OU_UPDATE_NAME),
60+
resource.TestCheckResourceAttr(resourceName, "description", ""),
61+
),
62+
},
63+
{
64+
ResourceName: resourceName,
65+
ImportState: true,
66+
ImportStateVerify: true,
67+
ImportStateIdFunc: testAccOuImportStateFunc(resourceName),
68+
},
69+
},
70+
})
71+
}
72+
73+
func testAccOu_base() string {
74+
return fmt.Sprintf(`
75+
resource "huaweicloud_workspace_service" "test" {
76+
ad_domain {
77+
name = try(element(regexall("\\w+\\.(.*)", element(split(",", "%[1]s"), 0))[0], 0), "")
78+
active_domain_name = element(split(",", "%[1]s"), 0)
79+
active_domain_ip = element(split(",", "%[2]s"), 0)
80+
active_dns_ip = element(split(",", "%[2]s"), 0)
81+
admin_account = "%[3]s"
82+
password = "%[4]s"
83+
delete_computer_object = true
84+
}
85+
86+
auth_type = "LOCAL_AD"
87+
access_mode = "INTERNET"
88+
vpc_id = "%[5]s"
89+
network_ids = ["%[6]s"]
90+
}
91+
`, acceptance.HW_WORKSPACE_AD_DOMAIN_NAMES,
92+
acceptance.HW_WORKSPACE_AD_DOMAIN_IPS,
93+
acceptance.HW_WORKSPACE_AD_SERVER_ACCOUNT,
94+
acceptance.HW_WORKSPACE_AD_SERVER_PWD,
95+
acceptance.HW_WORKSPACE_AD_VPC_ID,
96+
acceptance.HW_WORKSPACE_AD_NETWORK_ID)
97+
}
98+
99+
func testAccOu_step1() string {
100+
return fmt.Sprintf(`
101+
%[1]s
102+
103+
resource "huaweicloud_workspace_ou" "test" {
104+
depends_on = [huaweicloud_workspace_service.test]
105+
106+
name = "%[2]s"
107+
domain = huaweicloud_workspace_service.test.ad_domain[0].name
108+
description = "Created by terraform script"
109+
}
110+
`, testAccOu_base(), acceptance.HW_WORKSPACE_OU_NAME)
111+
}
112+
113+
func testAccOu_step2() string {
114+
return fmt.Sprintf(`
115+
%[1]s
116+
117+
resource "huaweicloud_workspace_ou" "test" {
118+
depends_on = [huaweicloud_workspace_service.test]
119+
120+
name = "%[2]s"
121+
domain = huaweicloud_workspace_service.test.ad_domain[0].name
122+
}
123+
`, testAccOu_base(), acceptance.HW_WORKSPACE_OU_UPDATE_NAME)
124+
}
125+
126+
func testAccOuImportStateFunc(rName string) resource.ImportStateIdFunc {
127+
return func(s *terraform.State) (string, error) {
128+
rs, ok := s.RootModule().Resources[rName]
129+
if !ok {
130+
return "", fmt.Errorf("resource (%s) not found: %s", rName, rs)
131+
}
132+
ouName := rs.Primary.Attributes["name"]
133+
if ouName == "" {
134+
return "", fmt.Errorf("invalid format specified for import ID, want '<name>', but got '%s'", ouName)
135+
}
136+
return ouName, nil
137+
}
138+
}

0 commit comments

Comments
 (0)