Skip to content

Commit d4bb36c

Browse files
WeiMengXSWeiMengXS
andauthored
feat: update cam resource (#2220)
* feat: update cam resource * feat: changelog --------- Co-authored-by: WeiMengXS <[email protected]>
1 parent 9f23ffa commit d4bb36c

11 files changed

+774
-2
lines changed

.changelog/2220.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:new-resource
2+
tencentcloud_cam_tag_role_attachment
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_cam_list_attached_user_policy
7+
```
8+
9+
```release-note:enhancement
10+
tencentcloud_ckafka_instance: Support api `UpdateRoleConsoleLogin`
11+
```
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
Use this data source to query detailed information of cam list_attached_user_policy
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cam_list_attached_user_policy" "list_attached_user_policy" {
8+
target_uin = 100032767426
9+
attach_type = 0
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudCamListAttachedUserPolicy() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudCamListAttachedUserPolicyRead,
27+
Schema: map[string]*schema.Schema{
28+
"target_uin": {
29+
Required: true,
30+
Type: schema.TypeInt,
31+
Description: "Target User ID.",
32+
},
33+
34+
"attach_type": {
35+
Required: true,
36+
Type: schema.TypeInt,
37+
Description: "0: Return direct association and group association policies, 1: Only return direct association policies, 2: Only return group association policies.",
38+
},
39+
40+
"strategy_type": {
41+
Optional: true,
42+
Type: schema.TypeInt,
43+
Description: "Policy type.",
44+
},
45+
46+
"keyword": {
47+
Optional: true,
48+
Type: schema.TypeString,
49+
Description: "Search Keywords.",
50+
},
51+
52+
"policy_list": {
53+
Computed: true,
54+
Type: schema.TypeList,
55+
Description: "Policy List Data.",
56+
Elem: &schema.Resource{
57+
Schema: map[string]*schema.Schema{
58+
"policy_id": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
Description: "Policy ID.",
62+
},
63+
"policy_name": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
Description: "Policy Name.",
67+
},
68+
"description": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
Description: "Policy Description.",
72+
},
73+
"add_time": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
Description: "Creation time.",
77+
},
78+
"strategy_type": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
Description: "Policy type (1 represents custom policy, 2 represents preset policy).",
82+
},
83+
"create_mode": {
84+
Type: schema.TypeString,
85+
Computed: true,
86+
Description: "Creation mode (1 represents policies created by product or project permissions, others represent policies created by policy syntax).",
87+
},
88+
"groups": {
89+
Type: schema.TypeList,
90+
Computed: true,
91+
Description: "Associated information with groupNote: This field may return null, indicating that a valid value cannot be obtained.",
92+
Elem: &schema.Resource{
93+
Schema: map[string]*schema.Schema{
94+
"group_id": {
95+
Type: schema.TypeInt,
96+
Computed: true,
97+
Description: "Group ID.",
98+
},
99+
"group_name": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
Description: "Group Name.",
103+
},
104+
},
105+
},
106+
},
107+
"deactived": {
108+
Type: schema.TypeInt,
109+
Computed: true,
110+
Description: "Has it been taken offline (0: No 1: Yes)Note: This field may return null, indicating that a valid value cannot be obtained.",
111+
},
112+
"deactived_detail": {
113+
Type: schema.TypeSet,
114+
Elem: &schema.Schema{
115+
Type: schema.TypeString,
116+
},
117+
Computed: true,
118+
Description: "List of offline productsNote: This field may return null, indicating that a valid value cannot be obtained.",
119+
},
120+
},
121+
},
122+
},
123+
124+
"result_output_file": {
125+
Type: schema.TypeString,
126+
Optional: true,
127+
Description: "Used to save results.",
128+
},
129+
},
130+
}
131+
}
132+
133+
func dataSourceTencentCloudCamListAttachedUserPolicyRead(d *schema.ResourceData, meta interface{}) error {
134+
defer logElapsed("data_source.tencentcloud_cam_list_attached_user_policy.read")()
135+
defer inconsistentCheck(d, meta)()
136+
137+
logId := getLogId(contextNil)
138+
139+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
140+
141+
paramMap := make(map[string]interface{})
142+
if v, _ := d.GetOk("target_uin"); v != nil {
143+
paramMap["TargetUin"] = helper.IntUint64(v.(int))
144+
}
145+
146+
if v, _ := d.GetOk("attach_type"); v != nil {
147+
paramMap["AttachType"] = helper.IntUint64(v.(int))
148+
}
149+
150+
if v, _ := d.GetOk("strategy_type"); v != nil {
151+
paramMap["StrategyType"] = helper.IntUint64(v.(int))
152+
}
153+
154+
if v, ok := d.GetOk("keyword"); ok {
155+
paramMap["Keyword"] = helper.String(v.(string))
156+
}
157+
158+
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
159+
160+
var policyList []*cam.AttachedUserPolicy
161+
162+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
163+
result, e := service.DescribeCamListAttachedUserPolicyByFilter(ctx, paramMap)
164+
if e != nil {
165+
return retryError(e)
166+
}
167+
policyList = result
168+
return nil
169+
})
170+
if err != nil {
171+
return err
172+
}
173+
174+
ids := make([]string, 0, len(policyList))
175+
tmpList := make([]map[string]interface{}, 0, len(policyList))
176+
177+
if policyList != nil {
178+
for _, attachedUserPolicy := range policyList {
179+
attachedUserPolicyMap := map[string]interface{}{}
180+
181+
if attachedUserPolicy.PolicyId != nil {
182+
attachedUserPolicyMap["policy_id"] = attachedUserPolicy.PolicyId
183+
}
184+
185+
if attachedUserPolicy.PolicyName != nil {
186+
attachedUserPolicyMap["policy_name"] = attachedUserPolicy.PolicyName
187+
}
188+
189+
if attachedUserPolicy.Description != nil {
190+
attachedUserPolicyMap["description"] = attachedUserPolicy.Description
191+
}
192+
193+
if attachedUserPolicy.AddTime != nil {
194+
attachedUserPolicyMap["add_time"] = attachedUserPolicy.AddTime
195+
}
196+
197+
if attachedUserPolicy.StrategyType != nil {
198+
attachedUserPolicyMap["strategy_type"] = attachedUserPolicy.StrategyType
199+
}
200+
201+
if attachedUserPolicy.CreateMode != nil {
202+
attachedUserPolicyMap["create_mode"] = attachedUserPolicy.CreateMode
203+
}
204+
205+
if attachedUserPolicy.Groups != nil {
206+
groupsList := []interface{}{}
207+
for _, groups := range attachedUserPolicy.Groups {
208+
groupsMap := map[string]interface{}{}
209+
210+
if groups.GroupId != nil {
211+
groupsMap["group_id"] = groups.GroupId
212+
}
213+
214+
if groups.GroupName != nil {
215+
groupsMap["group_name"] = groups.GroupName
216+
}
217+
218+
groupsList = append(groupsList, groupsMap)
219+
}
220+
221+
attachedUserPolicyMap["groups"] = groupsList
222+
}
223+
224+
if attachedUserPolicy.Deactived != nil {
225+
attachedUserPolicyMap["deactived"] = attachedUserPolicy.Deactived
226+
}
227+
228+
if attachedUserPolicy.DeactivedDetail != nil {
229+
attachedUserPolicyMap["deactived_detail"] = attachedUserPolicy.DeactivedDetail
230+
}
231+
232+
ids = append(ids, *attachedUserPolicy.PolicyId)
233+
tmpList = append(tmpList, attachedUserPolicyMap)
234+
}
235+
236+
_ = d.Set("policy_list", tmpList)
237+
}
238+
239+
d.SetId(helper.DataResourceIdsHash(ids))
240+
output, ok := d.GetOk("result_output_file")
241+
if ok && output.(string) != "" {
242+
if e := writeToFile(output.(string), tmpList); e != nil {
243+
return e
244+
}
245+
}
246+
return nil
247+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCamListAttachedUserPolicyDataSource_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: testAccCamListAttachedUserPolicyDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_cam_list_attached_user_policy.list_attached_user_policy"),
20+
resource.TestCheckResourceAttrSet("data.tencentcloud_cam_list_attached_user_policy.list_attached_user_policy", "policy_list.#"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_cam_list_attached_user_policy.list_attached_user_policy", "target_uin", "100032767426"),
22+
resource.TestCheckResourceAttr("data.tencentcloud_cam_list_attached_user_policy.list_attached_user_policy", "attach_type", "0"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccCamListAttachedUserPolicyDataSource = `
30+
31+
data "tencentcloud_cam_list_attached_user_policy" "list_attached_user_policy" {
32+
target_uin = 100032767426
33+
attach_type = 0
34+
}
35+
36+
`

tencentcloud/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Cloud Access Management(CAM)
244244
tencentcloud_cam_mfa_flag
245245
tencentcloud_cam_access_key
246246
tencentcloud_cam_user_saml_config
247+
tencentcloud_cam_tag_role_attachment
247248
tencentcloud_cam_policy_version
248249
tencentcloud_cam_user_permission_boundary_attachment
249250
@@ -2585,6 +2586,7 @@ func Provider() *schema.Provider {
25852586
"tencentcloud_organization_org_financial_by_product": dataSourceTencentCloudOrganizationOrgFinancialByProduct(),
25862587
"tencentcloud_organization_org_auth_node": dataSourceTencentCloudOrganizationOrgAuthNode(),
25872588
"tencentcloud_pts_scenario_with_jobs": dataSourceTencentCloudPtsScenarioWithJobs(),
2589+
"tencentcloud_cam_list_attached_user_policy": dataSourceTencentCloudCamListAttachedUserPolicy(),
25882590
},
25892591

25902592
ResourcesMap: map[string]*schema.Resource{
@@ -2843,6 +2845,7 @@ func Provider() *schema.Provider {
28432845
"tencentcloud_cam_mfa_flag": resourceTencentCloudCamMfaFlag(),
28442846
"tencentcloud_cam_access_key": resourceTencentCloudCamAccessKey(),
28452847
"tencentcloud_cam_user_saml_config": resourceTencentCloudCamUserSamlConfig(),
2848+
"tencentcloud_cam_tag_role_attachment": resourceTencentCloudCamTagRoleAttachment(),
28462849
"tencentcloud_cam_policy_version": resourceTencentCloudCamPolicyVersion(),
28472850
"tencentcloud_cam_user_permission_boundary_attachment": resourceTencentCloudCamUserPermissionBoundaryAttachment(),
28482851
"tencentcloud_ciam_user_group": resourceTencentCloudCiamUserGroup(),

tencentcloud/resource_tc_cam_role.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ func resourceTencentCloudCamRole() *schema.Resource {
149149
"console_login": {
150150
Type: schema.TypeBool,
151151
Optional: true,
152-
ForceNew: true,
153152
Description: "Indicates whether the CAM role can login or not.",
154153
},
155154
"create_time": {
@@ -409,6 +408,39 @@ func resourceTencentCloudCamRoleUpdate(d *schema.ResourceData, meta interface{})
409408

410409
}
411410

411+
if d.HasChange("console_login") {
412+
consoleLoginRequest := cam.NewUpdateRoleConsoleLoginRequest()
413+
414+
if v, ok := d.GetOkExists("console_login"); ok {
415+
loginBool := v.(bool)
416+
loginInt := int64(1)
417+
if !loginBool {
418+
loginInt = int64(0)
419+
}
420+
consoleLoginRequest.ConsoleLogin = &loginInt
421+
}
422+
423+
consoleLoginRequest.RoleId = helper.StrToInt64Point(roleId)
424+
425+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
426+
response, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().UpdateRoleConsoleLogin(consoleLoginRequest)
427+
428+
if e != nil {
429+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
430+
logId, consoleLoginRequest.GetAction(), consoleLoginRequest.ToJsonString(), e.Error())
431+
return retryError(e)
432+
} else {
433+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
434+
logId, consoleLoginRequest.GetAction(), consoleLoginRequest.ToJsonString(), response.ToJsonString())
435+
}
436+
return nil
437+
})
438+
439+
if err != nil {
440+
log.Printf("[CRITAL]%s update CAM role console login failed, reason:%s\n", logId, err.Error())
441+
return err
442+
}
443+
}
412444
return resourceTencentCloudCamRoleRead(d, meta)
413445
}
414446

0 commit comments

Comments
 (0)