Skip to content

feat(tco): [119957152]add tco datasource #2894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changelog/2894.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:new-data-source
tencentcloud_identity_center_groups
```

```release-note:new-data-source
tencentcloud_identity_center_role_configurations
```

```release-note:new-data-source
tencentcloud_identity_center_users
```
3 changes: 3 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,9 @@ func Provider() *schema.Provider {
"tencentcloud_organization_org_auth_node": tco.DataSourceTencentCloudOrganizationOrgAuthNode(),
"tencentcloud_organization_members": tco.DataSourceTencentCloudOrganizationMembers(),
"tencentcloud_organization_services": tco.DataSourceTencentCloudOrganizationServices(),
"tencentcloud_identity_center_groups": tco.DataSourceTencentCloudIdentityCenterGroups(),
"tencentcloud_identity_center_role_configurations": tco.DataSourceTencentCloudIdentityCenterRoleConfigurations(),
"tencentcloud_identity_center_users": tco.DataSourceTencentCloudIdentityCenterUsers(),
"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
3 changes: 3 additions & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,9 @@ Tencent Cloud Organization (TCO)
tencentcloud_organization_org_financial_by_product
tencentcloud_organization_org_share_area
tencentcloud_organization_services
tencentcloud_identity_center_groups
tencentcloud_identity_center_role_configurations
tencentcloud_identity_center_users
Resource
tencentcloud_organization_instance
tencentcloud_organization_org_node
Expand Down
223 changes: 223 additions & 0 deletions tencentcloud/services/tco/data_source_tc_identity_center_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package tco

import (
"context"

"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 DataSourceTencentCloudIdentityCenterGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudIdentityCenterGroupsRead,
Schema: map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeString,
Required: true,
Description: "Space ID.",
},

"filter": {
Type: schema.TypeString,
Optional: true,
Description: "Filter criterion. Format: <Attribute> <Operator> <Value>, case-insensitive. Currently, <Attribute> supports only GroupName, and <Operator> supports only eq (Equals) and sw (Start With). For example, Filter = \"GroupName sw test\" indicates querying all user groups with names starting with test; Filter = \"GroupName eq testgroup\" indicates querying the user group with the name testgroup.",
},

"group_type": {
Type: schema.TypeString,
Optional: true,
Description: "User group type. Manual: manually created; Synchronized: externally imported.",
},

"filter_users": {
Type: schema.TypeSet,
Optional: true,
Description: "Filtered user. IsSelected=1 will be returned for the user group associated with this user.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"sort_field": {
Type: schema.TypeString,
Optional: true,
Description: "Sorting field, which currently only supports CreateTime. The default is the CreateTime field.",
},

"sort_type": {
Type: schema.TypeString,
Optional: true,
Description: "Sorting type. Desc: descending order; Asc: ascending order. It should be set along with SortField.",
},

"groups": {
Type: schema.TypeList,
Computed: true,
Description: "User group list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_name": {
Type: schema.TypeString,
Optional: true,
Description: "User group name.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "User group description.",
},
"create_time": {
Type: schema.TypeString,
Optional: true,
Description: "Creation time of the user group.",
},
"group_type": {
Type: schema.TypeString,
Optional: true,
Description: "User group type. Manual: manually created; Synchronized: externally imported.",
},
"update_time": {
Type: schema.TypeString,
Optional: true,
Description: "Modification time of the user group.",
},
"group_id": {
Type: schema.TypeString,
Optional: true,
Description: "User group ID.",
},
"member_count": {
Type: schema.TypeInt,
Optional: true,
Description: "Number of group members.",
},
"is_selected": {
Type: schema.TypeBool,
Optional: true,
Description: "If the input parameter FilterUsers is provided, return true when the user is in the user group; otherwise, return false.",
},
},
},
},

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

func dataSourceTencentCloudIdentityCenterGroupsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_identity_center_groups.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("zone_id"); ok {
paramMap["ZoneId"] = helper.String(v.(string))
}

if v, ok := d.GetOk("filter"); ok {
paramMap["Filter"] = helper.String(v.(string))
}

if v, ok := d.GetOk("group_type"); ok {
paramMap["GroupType"] = helper.String(v.(string))
}

if v, ok := d.GetOk("filter_users"); ok {
filterUsersList := []*string{}
filterUsersSet := v.(*schema.Set).List()
for i := range filterUsersSet {
filterUsers := filterUsersSet[i].(string)
filterUsersList = append(filterUsersList, helper.String(filterUsers))
}
paramMap["FilterUsers"] = filterUsersList
}

if v, ok := d.GetOk("sort_field"); ok {
paramMap["SortField"] = helper.String(v.(string))
}

if v, ok := d.GetOk("sort_type"); ok {
paramMap["SortType"] = helper.String(v.(string))
}

var groups []*organization.GroupInfo

err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeIdentityCenterGroupsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
groups = result
return nil
})
if err != nil {
return err
}

groupsList := make([]map[string]interface{}, 0, len(groups))
ids := make([]string, 0, len(groups))
for _, group := range groups {
groupsMap := map[string]interface{}{}

if group.GroupName != nil {
groupsMap["group_name"] = group.GroupName
}

if group.Description != nil {
groupsMap["description"] = group.Description
}

if group.CreateTime != nil {
groupsMap["create_time"] = group.CreateTime
}

if group.GroupType != nil {
groupsMap["group_type"] = group.GroupType
}

if group.UpdateTime != nil {
groupsMap["update_time"] = group.UpdateTime
}

if group.GroupId != nil {
groupsMap["group_id"] = group.GroupId
ids = append(ids, *group.GroupId)
}

if group.MemberCount != nil {
groupsMap["member_count"] = group.MemberCount
}

if group.IsSelected != nil {
groupsMap["is_selected"] = group.IsSelected
}

groupsList = append(groupsList, groupsMap)
}

_ = d.Set("groups", groupsList)

d.SetId(helper.DataResourceIdsHash(ids))

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

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Use this data source to query detailed information of identity center groups

Example Usage

```hcl
data "tencentcloud_identity_center_groups" "identity_center_groups" {
zone_id = "z-xxxxxx"
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tco_test

import (
"testing"

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

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

func TestAccTencentCloudIdentityCenterGroupsDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{{
Config: testAccIdentityCenterGroupsDataSource,
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_identity_center_groups.identity_center_groups"),
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.#"),
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.0.group_id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.0.group_name"),
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.0.group_type"),
),
}},
})
}

const testAccIdentityCenterGroupsDataSource = `
data "tencentcloud_identity_center_groups" "identity_center_groups" {
zone_id = "z-s64jh54hbcra"
}
`
Loading
Loading