Skip to content

Commit 733045e

Browse files
tongyimingmikatong
and
mikatong
authored
fix(tco): [119840258] support organization name (#2844)
* support organization name * add changelog * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 0393cf4 commit 733045e

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

.changelog/2844.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_organization_instance: support param `root_node_name`.
3+
```

tencentcloud/services/tco/resource_tc_organization_instance.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func ResourceTencentCloudOrganizationOrganization() *schema.Resource {
1717
return &schema.Resource{
1818
Create: resourceTencentCloudOrganizationOrganizationCreate,
1919
Read: resourceTencentCloudOrganizationOrganizationRead,
20+
Update: resourceTencentCloudOrganizationOrganizationUpdate,
2021
Delete: resourceTencentCloudOrganizationOrganizationDelete,
2122
Importer: &schema.ResourceImporter{
2223
State: schema.ImportStatePassthrough,
@@ -28,6 +29,13 @@ func ResourceTencentCloudOrganizationOrganization() *schema.Resource {
2829
Description: "Enterprise organization ID.Note: This field may return NULL, indicating that the valid value cannot be obtained.",
2930
},
3031

32+
"root_node_name": {
33+
Optional: true,
34+
Computed: true,
35+
Type: schema.TypeString,
36+
Description: "Root node name.",
37+
},
38+
3139
"host_uin": {
3240
Computed: true,
3341
Type: schema.TypeInt,
@@ -140,6 +148,7 @@ func resourceTencentCloudOrganizationOrganizationCreate(d *schema.ResourceData,
140148
defer tccommon.InconsistentCheck(d, meta)()
141149

142150
logId := tccommon.GetLogId(tccommon.ContextNil)
151+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
143152

144153
var (
145154
request = organization.NewCreateOrganizationRequest()
@@ -162,6 +171,15 @@ func resourceTencentCloudOrganizationOrganizationCreate(d *schema.ResourceData,
162171
}
163172

164173
orgId = *response.Response.OrgId
174+
175+
if v, ok := d.GetOk("root_node_name"); ok {
176+
service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
177+
innerErr := service.UpdateOrganizationRootNodeName(ctx, orgId, v.(string))
178+
if innerErr != nil {
179+
return innerErr
180+
}
181+
}
182+
165183
d.SetId(helper.UInt64ToStr(orgId))
166184

167185
return resourceTencentCloudOrganizationOrganizationRead(d, meta)
@@ -268,9 +286,37 @@ func resourceTencentCloudOrganizationOrganizationRead(d *schema.ResourceData, me
268286
_ = d.Set("is_auth_manager", organization.IsAuthManager)
269287
}
270288

289+
if organization.RootNodeId != nil {
290+
orgNode, err := service.DescribeOrganizationOrgNode(ctx, helper.Int64ToStr(*organization.RootNodeId))
291+
if err != nil {
292+
return err
293+
}
294+
_ = d.Set("root_node_name", orgNode.Name)
295+
}
271296
return nil
272297
}
273298

299+
func resourceTencentCloudOrganizationOrganizationUpdate(d *schema.ResourceData, meta interface{}) error {
300+
defer tccommon.LogElapsed("resource.tencentcloud_organization_instance.update")()
301+
defer tccommon.InconsistentCheck(d, meta)()
302+
303+
logId := tccommon.GetLogId(tccommon.ContextNil)
304+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
305+
orgIdString := d.Id()
306+
orgId := helper.StrToUInt64(orgIdString)
307+
if d.HasChange("root_node_name") {
308+
if v, ok := d.GetOk("root_node_name"); ok {
309+
service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
310+
innerErr := service.UpdateOrganizationRootNodeName(ctx, orgId, v.(string))
311+
if innerErr != nil {
312+
return innerErr
313+
}
314+
}
315+
}
316+
317+
return resourceTencentCloudOrganizationOrganizationRead(d, meta)
318+
}
319+
274320
func resourceTencentCloudOrganizationOrganizationDelete(d *schema.ResourceData, meta interface{}) error {
275321
defer tccommon.LogElapsed("resource.tencentcloud_organization_instance.delete")()
276322
defer tccommon.InconsistentCheck(d, meta)()

tencentcloud/services/tco/resource_tc_organization_instance_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func TestAccTencentCloudOrganizationOrganizationResource_basic(t *testing.T) {
12-
t.Parallel()
1312
resource.Test(t, resource.TestCase{
1413
PreCheck: func() {
1514
tcacctest.AccPreCheck(t)
@@ -29,9 +28,51 @@ func TestAccTencentCloudOrganizationOrganizationResource_basic(t *testing.T) {
2928
})
3029
}
3130

31+
func TestAccTencentCloudOrganizationOrganizationResource_rootNodeName(t *testing.T) {
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() {
34+
tcacctest.AccPreCheck(t)
35+
},
36+
Providers: tcacctest.AccProviders,
37+
Steps: []resource.TestStep{
38+
{
39+
Config: testAccOrganizationOrganizationWithRootNodeName,
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttrSet("tencentcloud_organization_instance.organization", "id"),
42+
resource.TestCheckResourceAttr("tencentcloud_organization_instance.organization", "root_node_name", "root_node_name"),
43+
),
44+
},
45+
{
46+
Config: testAccOrganizationOrganizationWithRootNodeNameUpdate,
47+
Check: resource.ComposeTestCheckFunc(
48+
resource.TestCheckResourceAttrSet("tencentcloud_organization_instance.organization", "id"),
49+
resource.TestCheckResourceAttr("tencentcloud_organization_instance.organization", "root_node_name", "root_node_name_update"),
50+
),
51+
},
52+
{
53+
ResourceName: "tencentcloud_organization_instance.organization",
54+
ImportState: true,
55+
ImportStateVerify: true,
56+
},
57+
},
58+
})
59+
}
60+
3261
const testAccOrganizationOrganization = `
3362
3463
resource "tencentcloud_organization_instance" "organization" {
3564
}
3665
3766
`
67+
68+
const testAccOrganizationOrganizationWithRootNodeName = `
69+
resource "tencentcloud_organization_instance" "organization" {
70+
root_node_name = "root_node_name"
71+
}
72+
`
73+
74+
const testAccOrganizationOrganizationWithRootNodeNameUpdate = `
75+
resource "tencentcloud_organization_instance" "organization" {
76+
root_node_name = "root_node_name_update"
77+
}
78+
`

tencentcloud/services/tco/service_tencentcloud_organization.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,3 +1621,37 @@ func (me *OrganizationService) AssignmentTaskStatusStateRefreshFunc(zoneId, task
16211621
return object, *object.Status, nil
16221622
}
16231623
}
1624+
1625+
func (me *OrganizationService) UpdateOrganizationRootNodeName(ctx context.Context, orgId uint64, name string) (errRet error) {
1626+
logId := tccommon.GetLogId(ctx)
1627+
organizationResponseParams, err := me.DescribeOrganizationOrganizationById(ctx)
1628+
if err != nil {
1629+
return err
1630+
}
1631+
if organizationResponseParams == nil {
1632+
return fmt.Errorf("organization is nil")
1633+
}
1634+
rootNodeId := organizationResponseParams.RootNodeId
1635+
1636+
request := organization.NewUpdateOrganizationNodeRequest()
1637+
request.NodeId = helper.Int64Uint64(*rootNodeId)
1638+
request.Name = &name
1639+
1640+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1641+
result, e := me.client.UseOrganizationClient().UpdateOrganizationNode(request)
1642+
if e != nil {
1643+
return tccommon.RetryError(e)
1644+
} else {
1645+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
1646+
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
1647+
}
1648+
return nil
1649+
})
1650+
1651+
if err != nil {
1652+
log.Printf("[CRITAL]%s update organization orgNode name failed, reason:%+v", logId, err)
1653+
return err
1654+
}
1655+
1656+
return nil
1657+
}

website/docs/r/organization_instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ resource "tencentcloud_organization_instance" "organization" {
2222

2323
The following arguments are supported:
2424

25-
25+
* `root_node_name` - (Optional, String) Root node name.
2626

2727
## Attributes Reference
2828

0 commit comments

Comments
 (0)