Skip to content

Commit

Permalink
feat(tco): [120225468]add tencentcloud_organization_nodes and tencent…
Browse files Browse the repository at this point in the history
…cloud_open_identity_center_operation (#2906)

* add resource

* add changelog

* update

---------

Co-authored-by: mikatong <mikatong@tencent.com>
  • Loading branch information
tongyiming and mikatong authored Oct 23, 2024
1 parent 2cfd758 commit 01ec1df
Show file tree
Hide file tree
Showing 13 changed files with 538 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changelog/2906.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
tencentcloud_open_identity_center_operation
```

```release-note:new-data-source
tencentcloud_organization_nodes
```
2 changes: 2 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ func Provider() *schema.Provider {
"tencentcloud_identity_center_groups": tco.DataSourceTencentCloudIdentityCenterGroups(),
"tencentcloud_identity_center_role_configurations": tco.DataSourceTencentCloudIdentityCenterRoleConfigurations(),
"tencentcloud_identity_center_users": tco.DataSourceTencentCloudIdentityCenterUsers(),
"tencentcloud_organization_nodes": tco.DataSourceTencentCloudOrganizationNodes(),
"tencentcloud_pts_scenario_with_jobs": pts.DataSourceTencentCloudPtsScenarioWithJobs(),
"tencentcloud_cam_list_attached_user_policy": cam.DataSourceTencentCloudCamListAttachedUserPolicy(),
"tencentcloud_cam_secret_last_used_time": cam.DataSourceTencentCloudCamSecretLastUsedTime(),
Expand Down Expand Up @@ -1833,6 +1834,7 @@ func Provider() *schema.Provider {
"tencentcloud_identity_center_user_sync_provisioning": tco.ResourceTencentCloudIdentityCenterUserSyncProvisioning(),
"tencentcloud_identity_center_role_assignment": tco.ResourceTencentCloudIdentityCenterRoleAssignment(),
"tencentcloud_invite_organization_member_operation": tco.ResourceTencentCloudInviteOrganizationMemberOperation(),
"tencentcloud_open_identity_center_operation": tco.ResourceTencentCloudOpenIdentityCenterOperation(),
"tencentcloud_dbbrain_sql_filter": dbbrain.ResourceTencentCloudDbbrainSqlFilter(),
"tencentcloud_dbbrain_security_audit_log_export_task": dbbrain.ResourceTencentCloudDbbrainSecurityAuditLogExportTask(),
"tencentcloud_dbbrain_db_diag_report_task": dbbrain.ResourceTencentCloudDbbrainDbDiagReportTask(),
Expand Down
2 changes: 2 additions & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ Tencent Cloud Organization (TCO)
tencentcloud_identity_center_groups
tencentcloud_identity_center_role_configurations
tencentcloud_identity_center_users
tencentcloud_organization_nodes
Resource
tencentcloud_organization_instance
tencentcloud_organization_org_node
Expand All @@ -1708,6 +1709,7 @@ Tencent Cloud Organization (TCO)
tencentcloud_identity_center_user_sync_provisioning
tencentcloud_identity_center_role_assignment
tencentcloud_invite_organization_member_operation
tencentcloud_open_identity_center_operation

TDSQL-C for PostgreSQL(TDCPG)
Data Source
Expand Down
211 changes: 211 additions & 0 deletions tencentcloud/services/tco/data_source_tc_organization_nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package tco

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func DataSourceTencentCloudOrganizationNodes() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudOrganizationNodesRead,
Schema: map[string]*schema.Schema{
"tags": {
Type: schema.TypeList,
Optional: true,
Description: "Department tag search list, with a maximum of 10.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tag_key": {
Type: schema.TypeString,
Required: true,
Description: "Tag key.",
},
"tag_value": {
Type: schema.TypeString,
Required: true,
Description: "Tag value.",
},
},
},
},

"items": {
Type: schema.TypeList,
Computed: true,
Description: "List details.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"node_id": {
Type: schema.TypeInt,
Required: true,
Description: "Organization node ID.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name.",
},
"parent_node_id": {
Type: schema.TypeInt,
Required: true,
Description: "Parent node ID.",
},
"remark": {
Type: schema.TypeString,
Required: true,
Description: "Remarks.",
},
"create_time": {
Type: schema.TypeString,
Required: true,
Description: "Creation time.",
},
"update_time": {
Type: schema.TypeString,
Required: true,
Description: "Update time.",
},
"tags": {
Type: schema.TypeList,
Required: true,
Description: "Member tag list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tag_key": {
Type: schema.TypeString,
Required: true,
Description: "Tag key.",
},
"tag_value": {
Type: schema.TypeString,
Required: true,
Description: "Tag value.",
},
},
},
},
},
},
},

"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudOrganizationNodesRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_organization_nodes.read")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)

service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}

paramMap := make(map[string]interface{})
if v, ok := d.GetOk("tags"); ok {
tagsSet := v.([]interface{})
tmpSet := make([]*organization.Tag, 0, len(tagsSet))
for _, item := range tagsSet {
tagsMap := item.(map[string]interface{})
tag := organization.Tag{}
if v, ok := tagsMap["tag_key"]; ok {
tag.TagKey = helper.String(v.(string))
}
if v, ok := tagsMap["tag_value"]; ok {
tag.TagValue = helper.String(v.(string))
}
tmpSet = append(tmpSet, &tag)
}
paramMap["Tags"] = tmpSet
}

var nodes []*organization.OrgNode
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeOrganizationNodesByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
nodes = result
return nil
})
if err != nil {
return err
}

itemList := make([]map[string]interface{}, 0, len(nodes))
ids := make([]string, 0, len(nodes))
for _, item := range nodes {
itemMap := map[string]interface{}{}

if item.NodeId != nil {
itemMap["node_id"] = item.NodeId
nodeIdStr := strconv.FormatInt(*item.NodeId, 10)
ids = append(ids, nodeIdStr)
}

if item.Name != nil {
itemMap["name"] = item.Name
}

if item.ParentNodeId != nil {
itemMap["parent_node_id"] = item.ParentNodeId
}

if item.Remark != nil {
itemMap["remark"] = item.Remark
}

if item.CreateTime != nil {
itemMap["create_time"] = item.CreateTime
}

if item.UpdateTime != nil {
itemMap["update_time"] = item.UpdateTime
}

tagsList := make([]map[string]interface{}, 0, len(item.Tags))
if item.Tags != nil {
for _, tags := range item.Tags {
tagsMap := map[string]interface{}{}

if tags.TagKey != nil {
tagsMap["tag_key"] = tags.TagKey
}

if tags.TagValue != nil {
tagsMap["tag_value"] = tags.TagValue
}

tagsList = append(tagsList, tagsMap)
}

itemMap["tags"] = tagsList
}
itemList = append(itemList, itemMap)
}

_ = d.Set("items", itemList)

d.SetId(helper.DataResourceIdsHash(ids))

output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), itemList); e != nil {
return e
}
}

return nil
}
12 changes: 12 additions & 0 deletions tencentcloud/services/tco/data_source_tc_organization_nodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Use this data source to query detailed information of organization nodes

Example Usage

```hcl
data "tencentcloud_organization_nodes" "organization_nodes" {
tags {
tag_key = "createBy"
tag_value = "terraform"
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tco_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
)

func TestAccTencentCloudOrganizationNodesDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{{
Config: testAccOrganizationNodesDataSource,
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_organization_nodes.organization_nodes"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.#"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.0.name"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.0.node_id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.0.parent_node_id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.0.create_time"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.0.update_time"),
resource.TestCheckResourceAttrSet("data.tencentcloud_organization_nodes.organization_nodes", "items.0.tags.#"),
),
}},
})
}

const testAccOrganizationNodesDataSource = `
data "tencentcloud_organization_nodes" "organization_nodes" {
tags {
tag_key = "createBy"
tag_value = "terraform"
}
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tco

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func ResourceTencentCloudOpenIdentityCenterOperation() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudInviteOpenIdentityCenterOperationCreate,
Read: resourceTencentCloudInviteOpenIdentityCenterOperationRead,
Delete: resourceTencentCloudInviteOpenIdentityCenterOperationDelete,
Schema: map[string]*schema.Schema{
"zone_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Space name, which must be globally unique and contain 2-64 characters including lowercase letters, digits, and hyphens (-). It can neither start or end with a hyphen (-) nor contain two consecutive hyphens (-).",
},

"zone_id": {
Type: schema.TypeString,
Computed: true,
Description: "Space ID. z-Prefix starts with 12 random numbers/lowercase letters followed by.",
},
},
}
}

func resourceTencentCloudInviteOpenIdentityCenterOperationCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_open_identity_center_operation.create")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

var (
request = organization.NewOpenIdentityCenterRequest()
response = organization.NewOpenIdentityCenterResponse()
)

if v, ok := d.GetOk("zone_name"); ok {
request.ZoneName = helper.String(v.(string))
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().OpenIdentityCenter(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s open identity center operation failed, reason:%+v", logId, err)
return err
}

if response.Response != nil && response.Response.ZoneId != nil {
d.SetId(*response.Response.ZoneId)
_ = d.Set("zone_id", *response.Response.ZoneId)
}

_ = response

return resourceTencentCloudInviteOpenIdentityCenterOperationRead(d, meta)
}

func resourceTencentCloudInviteOpenIdentityCenterOperationRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_open_identity_center_operation.read")()
defer tccommon.InconsistentCheck(d, meta)()

return nil
}

func resourceTencentCloudInviteOpenIdentityCenterOperationDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_open_identity_center_operation.delete")()
defer tccommon.InconsistentCheck(d, meta)()

return nil
}
Loading

0 comments on commit 01ec1df

Please sign in to comment.