Skip to content

Commit 0d319f3

Browse files
WeiMengXSWeiMengXS
andauthored
feat: cam (#2292)
* feat: cam * feat: changelog * feat: changelog --------- Co-authored-by: WeiMengXS <nickcchen@tencent.com>
1 parent 6864e9c commit 0d319f3

File tree

7 files changed

+299
-0
lines changed

7 files changed

+299
-0
lines changed

.changelog/2292.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_cam_group_user_account
3+
```
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
Use this data source to query detailed information of cam group_user_account
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cam_group_user_account" "group_user_account" {
8+
sub_uin = 100033690181
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudCamGroupUserAccount() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudCamGroupUserAccountRead,
26+
Schema: map[string]*schema.Schema{
27+
"uid": {
28+
Optional: true,
29+
Type: schema.TypeInt,
30+
Description: "Sub-user uid.",
31+
},
32+
33+
"rp": {
34+
Optional: true,
35+
Type: schema.TypeInt,
36+
Description: "Number per page. The default is 20.",
37+
},
38+
39+
"sub_uin": {
40+
Optional: true,
41+
Type: schema.TypeInt,
42+
Description: "Sub-user uin.",
43+
},
44+
45+
"total_num": {
46+
Computed: true,
47+
Type: schema.TypeInt,
48+
Description: "The total number of user groups the sub-user has joined.",
49+
},
50+
51+
"group_info": {
52+
Computed: true,
53+
Type: schema.TypeList,
54+
Description: "User group information.",
55+
Elem: &schema.Resource{
56+
Schema: map[string]*schema.Schema{
57+
"group_id": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
Description: "User group ID.",
61+
},
62+
"group_name": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Description: "User group name.",
66+
},
67+
"create_time": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: "Create time.",
71+
},
72+
"remark": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
Description: "Remark.",
76+
},
77+
},
78+
},
79+
},
80+
81+
"result_output_file": {
82+
Type: schema.TypeString,
83+
Optional: true,
84+
Description: "Used to save results.",
85+
},
86+
},
87+
}
88+
}
89+
90+
func dataSourceTencentCloudCamGroupUserAccountRead(d *schema.ResourceData, meta interface{}) error {
91+
defer logElapsed("data_source.tencentcloud_cam_group_user_account.read")()
92+
defer inconsistentCheck(d, meta)()
93+
94+
logId := getLogId(contextNil)
95+
96+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
97+
98+
paramMap := make(map[string]interface{})
99+
if v, ok := d.GetOkExists("uid"); ok {
100+
paramMap["Uid"] = helper.IntUint64(v.(int))
101+
}
102+
103+
if v, ok := d.GetOkExists("rp"); ok {
104+
paramMap["Rp"] = helper.IntUint64(v.(int))
105+
}
106+
107+
if v, ok := d.GetOkExists("sub_uin"); ok {
108+
paramMap["SubUin"] = helper.IntUint64(v.(int))
109+
}
110+
111+
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
112+
113+
var groupInfoList []*cam.GroupInfo
114+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
115+
result, e := service.DescribeCamGroupUserAccountByFilter(ctx, paramMap)
116+
if e != nil {
117+
return retryError(e)
118+
}
119+
groupInfoList = result
120+
return nil
121+
})
122+
if err != nil {
123+
return err
124+
}
125+
126+
ids := make([]string, 0, len(groupInfoList))
127+
tmpList := make([]map[string]interface{}, 0, len(groupInfoList))
128+
129+
if len(groupInfoList) > 0 {
130+
_ = d.Set("total_num", len(groupInfoList))
131+
}
132+
133+
if groupInfoList != nil {
134+
for _, groupInfo := range groupInfoList {
135+
groupInfoMap := map[string]interface{}{}
136+
137+
if groupInfo.GroupId != nil {
138+
groupInfoMap["group_id"] = groupInfo.GroupId
139+
}
140+
141+
if groupInfo.GroupName != nil {
142+
groupInfoMap["group_name"] = groupInfo.GroupName
143+
}
144+
145+
if groupInfo.CreateTime != nil {
146+
groupInfoMap["create_time"] = groupInfo.CreateTime
147+
}
148+
149+
if groupInfo.Remark != nil {
150+
groupInfoMap["remark"] = groupInfo.Remark
151+
}
152+
153+
ids = append(ids, helper.UInt64ToStr(*groupInfo.GroupId))
154+
tmpList = append(tmpList, groupInfoMap)
155+
}
156+
157+
_ = d.Set("group_info", tmpList)
158+
}
159+
160+
d.SetId(helper.DataResourceIdsHash(ids))
161+
output, ok := d.GetOk("result_output_file")
162+
if ok && output.(string) != "" {
163+
if e := writeToFile(output.(string), tmpList); e != nil {
164+
return e
165+
}
166+
}
167+
return nil
168+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCamGroupUserAccountDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCamGroupUserAccountDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_cam_group_user_account.group_user_account"),
20+
resource.TestCheckResourceAttr("data.tencentcloud_cam_group_user_account.group_user_account", "sub_uin", "100033690181")),
21+
},
22+
},
23+
})
24+
}
25+
26+
const testAccCamGroupUserAccountDataSource = `
27+
28+
data "tencentcloud_cam_group_user_account" "group_user_account" {
29+
sub_uin = 100033690181
30+
}
31+
`

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Cloud Access Management(CAM)
228228
tencentcloud_cam_account_summary
229229
tencentcloud_cam_policy_granting_service_access
230230
tencentcloud_cam_oidc_config
231+
tencentcloud_cam_group_user_account
231232
232233
Resource
233234
tencentcloud_cam_role
@@ -2846,6 +2847,7 @@ func Provider() *schema.Provider {
28462847
"tencentcloud_cam_list_attached_user_policy": dataSourceTencentCloudCamListAttachedUserPolicy(),
28472848
"tencentcloud_cam_secret_last_used_time": dataSourceTencentCloudCamSecretLastUsedTime(),
28482849
"tencentcloud_cam_policy_granting_service_access": dataSourceTencentCloudCamPolicyGrantingServiceAccess(),
2850+
"tencentcloud_cam_group_user_account": dataSourceTencentCloudCamGroupUserAccount(),
28492851
"tencentcloud_dlc_check_data_engine_image_can_be_rollback": dataSourceTencentCloudDlcCheckDataEngineImageCanBeRollback(),
28502852
"tencentcloud_dlc_check_data_engine_image_can_be_upgrade": dataSourceTencentCloudDlcCheckDataEngineImageCanBeUpgrade(),
28512853
"tencentcloud_dlc_describe_user_type": dataSourceTencentCloudDlcDescribeUserType(),

tencentcloud/service_tencentcloud_cam.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,3 +1914,53 @@ func (me *CamService) DescribeCamAccountSummaryByFilter(ctx context.Context) (Ac
19141914
AccountSummary = response.Response
19151915
return
19161916
}
1917+
1918+
func (me *CamService) DescribeCamGroupUserAccountByFilter(ctx context.Context, param map[string]interface{}) (GroupUserAccount []*cam.GroupInfo, errRet error) {
1919+
var (
1920+
logId = getLogId(ctx)
1921+
request = cam.NewListGroupsForUserRequest()
1922+
)
1923+
1924+
defer func() {
1925+
if errRet != nil {
1926+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1927+
}
1928+
}()
1929+
1930+
for k, v := range param {
1931+
if k == "Uid" {
1932+
request.Uid = v.(*uint64)
1933+
}
1934+
if k == "SubUin" {
1935+
request.SubUin = v.(*uint64)
1936+
}
1937+
}
1938+
1939+
ratelimit.Check(request.GetAction())
1940+
1941+
pageStart := uint64(1)
1942+
rp := uint64(PAGE_ITEM) //to save in extension
1943+
result := make([]*cam.GroupInfo, 0)
1944+
for {
1945+
request.Page = &pageStart
1946+
request.Rp = &rp
1947+
response, err := me.client.UseCamClient().ListGroupsForUser(request)
1948+
if err != nil {
1949+
errRet = err
1950+
return
1951+
}
1952+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1953+
1954+
if response == nil || len(response.Response.GroupInfo) < 1 {
1955+
break
1956+
}
1957+
result = append(result, response.Response.GroupInfo...)
1958+
if len(response.Response.GroupInfo) < PAGE_ITEM {
1959+
break
1960+
}
1961+
1962+
pageStart += 1
1963+
}
1964+
GroupUserAccount = result
1965+
return
1966+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
subcategory: "Cloud Access Management(CAM)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_cam_group_user_account"
5+
sidebar_current: "docs-tencentcloud-datasource-cam_group_user_account"
6+
description: |-
7+
Use this data source to query detailed information of cam group_user_account
8+
---
9+
10+
# tencentcloud_cam_group_user_account
11+
12+
Use this data source to query detailed information of cam group_user_account
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "tencentcloud_cam_group_user_account" "group_user_account" {
18+
sub_uin = 100033690181
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `result_output_file` - (Optional, String) Used to save results.
27+
* `rp` - (Optional, Int) Number per page. The default is 20.
28+
* `sub_uin` - (Optional, Int) Sub-user uin.
29+
* `uid` - (Optional, Int) Sub-user uid.
30+
31+
## Attributes Reference
32+
33+
In addition to all arguments above, the following attributes are exported:
34+
35+
* `group_info` - User group information.
36+
* `create_time` - Create time.
37+
* `group_id` - User group ID.
38+
* `group_name` - User group name.
39+
* `remark` - Remark.
40+
* `total_num` - The total number of user groups the sub-user has joined.
41+
42+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@
528528
<li>
529529
<a href="/docs/providers/tencentcloud/d/cam_group_policy_attachments.html">tencentcloud_cam_group_policy_attachments</a>
530530
</li>
531+
<li>
532+
<a href="/docs/providers/tencentcloud/d/cam_group_user_account.html">tencentcloud_cam_group_user_account</a>
533+
</li>
531534
<li>
532535
<a href="/docs/providers/tencentcloud/d/cam_groups.html">tencentcloud_cam_groups</a>
533536
</li>

0 commit comments

Comments
 (0)