Skip to content

Commit 977bfc8

Browse files
tongyimingmikatong
and
mikatong
authored
feat(tco): [119957152]add tco datasource (#2894)
* add tco datasource * add changelog * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 7d86bde commit 977bfc8

17 files changed

+1161
-0
lines changed

.changelog/2894.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:new-data-source
2+
tencentcloud_identity_center_groups
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_identity_center_role_configurations
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_identity_center_users
11+
```

tencentcloud/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,9 @@ func Provider() *schema.Provider {
10841084
"tencentcloud_organization_org_auth_node": tco.DataSourceTencentCloudOrganizationOrgAuthNode(),
10851085
"tencentcloud_organization_members": tco.DataSourceTencentCloudOrganizationMembers(),
10861086
"tencentcloud_organization_services": tco.DataSourceTencentCloudOrganizationServices(),
1087+
"tencentcloud_identity_center_groups": tco.DataSourceTencentCloudIdentityCenterGroups(),
1088+
"tencentcloud_identity_center_role_configurations": tco.DataSourceTencentCloudIdentityCenterRoleConfigurations(),
1089+
"tencentcloud_identity_center_users": tco.DataSourceTencentCloudIdentityCenterUsers(),
10871090
"tencentcloud_pts_scenario_with_jobs": pts.DataSourceTencentCloudPtsScenarioWithJobs(),
10881091
"tencentcloud_cam_list_attached_user_policy": cam.DataSourceTencentCloudCamListAttachedUserPolicy(),
10891092
"tencentcloud_cam_secret_last_used_time": cam.DataSourceTencentCloudCamSecretLastUsedTime(),

tencentcloud/provider.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,9 @@ Tencent Cloud Organization (TCO)
16781678
tencentcloud_organization_org_financial_by_product
16791679
tencentcloud_organization_org_share_area
16801680
tencentcloud_organization_services
1681+
tencentcloud_identity_center_groups
1682+
tencentcloud_identity_center_role_configurations
1683+
tencentcloud_identity_center_users
16811684
Resource
16821685
tencentcloud_organization_instance
16831686
tencentcloud_organization_org_node
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package tco
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331"
9+
10+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
11+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
12+
)
13+
14+
func DataSourceTencentCloudIdentityCenterGroups() *schema.Resource {
15+
return &schema.Resource{
16+
Read: dataSourceTencentCloudIdentityCenterGroupsRead,
17+
Schema: map[string]*schema.Schema{
18+
"zone_id": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "Space ID.",
22+
},
23+
24+
"filter": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
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.",
28+
},
29+
30+
"group_type": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
Description: "User group type. Manual: manually created; Synchronized: externally imported.",
34+
},
35+
36+
"filter_users": {
37+
Type: schema.TypeSet,
38+
Optional: true,
39+
Description: "Filtered user. IsSelected=1 will be returned for the user group associated with this user.",
40+
Elem: &schema.Schema{
41+
Type: schema.TypeString,
42+
},
43+
},
44+
45+
"sort_field": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: "Sorting field, which currently only supports CreateTime. The default is the CreateTime field.",
49+
},
50+
51+
"sort_type": {
52+
Type: schema.TypeString,
53+
Optional: true,
54+
Description: "Sorting type. Desc: descending order; Asc: ascending order. It should be set along with SortField.",
55+
},
56+
57+
"groups": {
58+
Type: schema.TypeList,
59+
Computed: true,
60+
Description: "User group list.",
61+
Elem: &schema.Resource{
62+
Schema: map[string]*schema.Schema{
63+
"group_name": {
64+
Type: schema.TypeString,
65+
Optional: true,
66+
Description: "User group name.",
67+
},
68+
"description": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Description: "User group description.",
72+
},
73+
"create_time": {
74+
Type: schema.TypeString,
75+
Optional: true,
76+
Description: "Creation time of the user group.",
77+
},
78+
"group_type": {
79+
Type: schema.TypeString,
80+
Optional: true,
81+
Description: "User group type. Manual: manually created; Synchronized: externally imported.",
82+
},
83+
"update_time": {
84+
Type: schema.TypeString,
85+
Optional: true,
86+
Description: "Modification time of the user group.",
87+
},
88+
"group_id": {
89+
Type: schema.TypeString,
90+
Optional: true,
91+
Description: "User group ID.",
92+
},
93+
"member_count": {
94+
Type: schema.TypeInt,
95+
Optional: true,
96+
Description: "Number of group members.",
97+
},
98+
"is_selected": {
99+
Type: schema.TypeBool,
100+
Optional: true,
101+
Description: "If the input parameter FilterUsers is provided, return true when the user is in the user group; otherwise, return false.",
102+
},
103+
},
104+
},
105+
},
106+
107+
"result_output_file": {
108+
Type: schema.TypeString,
109+
Optional: true,
110+
Description: "Used to save results.",
111+
},
112+
},
113+
}
114+
}
115+
116+
func dataSourceTencentCloudIdentityCenterGroupsRead(d *schema.ResourceData, meta interface{}) error {
117+
defer tccommon.LogElapsed("data_source.tencentcloud_identity_center_groups.read")()
118+
defer tccommon.InconsistentCheck(d, meta)()
119+
120+
logId := tccommon.GetLogId(tccommon.ContextNil)
121+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
122+
123+
service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
124+
125+
paramMap := make(map[string]interface{})
126+
if v, ok := d.GetOk("zone_id"); ok {
127+
paramMap["ZoneId"] = helper.String(v.(string))
128+
}
129+
130+
if v, ok := d.GetOk("filter"); ok {
131+
paramMap["Filter"] = helper.String(v.(string))
132+
}
133+
134+
if v, ok := d.GetOk("group_type"); ok {
135+
paramMap["GroupType"] = helper.String(v.(string))
136+
}
137+
138+
if v, ok := d.GetOk("filter_users"); ok {
139+
filterUsersList := []*string{}
140+
filterUsersSet := v.(*schema.Set).List()
141+
for i := range filterUsersSet {
142+
filterUsers := filterUsersSet[i].(string)
143+
filterUsersList = append(filterUsersList, helper.String(filterUsers))
144+
}
145+
paramMap["FilterUsers"] = filterUsersList
146+
}
147+
148+
if v, ok := d.GetOk("sort_field"); ok {
149+
paramMap["SortField"] = helper.String(v.(string))
150+
}
151+
152+
if v, ok := d.GetOk("sort_type"); ok {
153+
paramMap["SortType"] = helper.String(v.(string))
154+
}
155+
156+
var groups []*organization.GroupInfo
157+
158+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
159+
result, e := service.DescribeIdentityCenterGroupsByFilter(ctx, paramMap)
160+
if e != nil {
161+
return tccommon.RetryError(e)
162+
}
163+
groups = result
164+
return nil
165+
})
166+
if err != nil {
167+
return err
168+
}
169+
170+
groupsList := make([]map[string]interface{}, 0, len(groups))
171+
ids := make([]string, 0, len(groups))
172+
for _, group := range groups {
173+
groupsMap := map[string]interface{}{}
174+
175+
if group.GroupName != nil {
176+
groupsMap["group_name"] = group.GroupName
177+
}
178+
179+
if group.Description != nil {
180+
groupsMap["description"] = group.Description
181+
}
182+
183+
if group.CreateTime != nil {
184+
groupsMap["create_time"] = group.CreateTime
185+
}
186+
187+
if group.GroupType != nil {
188+
groupsMap["group_type"] = group.GroupType
189+
}
190+
191+
if group.UpdateTime != nil {
192+
groupsMap["update_time"] = group.UpdateTime
193+
}
194+
195+
if group.GroupId != nil {
196+
groupsMap["group_id"] = group.GroupId
197+
ids = append(ids, *group.GroupId)
198+
}
199+
200+
if group.MemberCount != nil {
201+
groupsMap["member_count"] = group.MemberCount
202+
}
203+
204+
if group.IsSelected != nil {
205+
groupsMap["is_selected"] = group.IsSelected
206+
}
207+
208+
groupsList = append(groupsList, groupsMap)
209+
}
210+
211+
_ = d.Set("groups", groupsList)
212+
213+
d.SetId(helper.DataResourceIdsHash(ids))
214+
215+
output, ok := d.GetOk("result_output_file")
216+
if ok && output.(string) != "" {
217+
if e := tccommon.WriteToFile(output.(string), groupsList); e != nil {
218+
return e
219+
}
220+
}
221+
222+
return nil
223+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Use this data source to query detailed information of identity center groups
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_identity_center_groups" "identity_center_groups" {
7+
zone_id = "z-xxxxxx"
8+
}
9+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tco_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
9+
)
10+
11+
func TestAccTencentCloudIdentityCenterGroupsDataSource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{{
19+
Config: testAccIdentityCenterGroupsDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_identity_center_groups.identity_center_groups"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.#"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.0.group_id"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.0.group_name"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_identity_center_groups.identity_center_groups", "groups.0.group_type"),
26+
),
27+
}},
28+
})
29+
}
30+
31+
const testAccIdentityCenterGroupsDataSource = `
32+
data "tencentcloud_identity_center_groups" "identity_center_groups" {
33+
zone_id = "z-s64jh54hbcra"
34+
}
35+
`

0 commit comments

Comments
 (0)