Skip to content

Commit 6d7f13a

Browse files
authored
Merge pull request #2115 from tencentcloudstack/feat/cam-mfa-config
feat: resource mfaFlag
2 parents bfbb904 + 0d14864 commit 6d7f13a

11 files changed

+460
-4
lines changed

.changelog/2115.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:new-resource
2+
tencentcloud_cam_mfa_flag
3+
```
4+
```release-note:enhancement
5+
resource/tencentcloud_ssl_pay_certificate: Supports `csr_type` modification
6+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Cloud Access Management(CAM)
233233
tencentcloud_cam_oidc_sso
234234
tencentcloud_cam_role_sso
235235
tencentcloud_cam_service_linked_role
236+
tencentcloud_cam_mfa_flag
236237
tencentcloud_cam_user_saml_config
237238
238239
Customer Identity and Access Management(CIAM)
@@ -2552,6 +2553,7 @@ func Provider() *schema.Provider {
25522553
"tencentcloud_cam_group_membership": resourceTencentCloudCamGroupMembership(),
25532554
"tencentcloud_cam_saml_provider": resourceTencentCloudCamSAMLProvider(),
25542555
"tencentcloud_cam_service_linked_role": resourceTencentCloudCamServiceLinkedRole(),
2556+
"tencentcloud_cam_mfa_flag": resourceTencentCloudCamMfaFlag(),
25552557
"tencentcloud_cam_user_saml_config": resourceTencentCloudCamUserSamlConfig(),
25562558
"tencentcloud_ciam_user_group": resourceTencentCloudCiamUserGroup(),
25572559
"tencentcloud_ciam_user_store": resourceTencentCloudCiamUserStore(),
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/*
2+
Provides a resource to create a cam mfa_flag
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_user_info" "info"{}
8+
9+
resource "tencentcloud_cam_mfa_flag" "mfa_flag" {
10+
op_uin = data.tencentcloud_user_info.info.uin
11+
login_flag {
12+
phone = 0
13+
stoken = 1
14+
wechat = 0
15+
}
16+
action_flag {
17+
phone = 0
18+
stoken = 1
19+
wechat = 0
20+
}
21+
}
22+
23+
```
24+
25+
Import
26+
27+
cam mfa_flag can be imported using the id, e.g.
28+
29+
```
30+
terraform import tencentcloud_cam_mfa_flag.mfa_flag mfa_flag_id
31+
```
32+
*/
33+
package tencentcloud
34+
35+
import (
36+
"context"
37+
"log"
38+
"strconv"
39+
40+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
41+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
42+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
43+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
44+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
45+
)
46+
47+
func resourceTencentCloudCamMfaFlag() *schema.Resource {
48+
return &schema.Resource{
49+
Create: resourceTencentCloudCamMfaFlagCreate,
50+
Read: resourceTencentCloudCamMfaFlagRead,
51+
Update: resourceTencentCloudCamMfaFlagUpdate,
52+
Delete: resourceTencentCloudCamMfaFlagDelete,
53+
Importer: &schema.ResourceImporter{
54+
State: schema.ImportStatePassthrough,
55+
},
56+
Schema: map[string]*schema.Schema{
57+
"op_uin": {
58+
Required: true,
59+
Type: schema.TypeInt,
60+
Description: "Operate uin.",
61+
},
62+
63+
"login_flag": {
64+
Optional: true,
65+
Type: schema.TypeList,
66+
MaxItems: 1,
67+
Description: "Login flag setting.",
68+
Elem: &schema.Resource{
69+
Schema: map[string]*schema.Schema{
70+
"phone": {
71+
Type: schema.TypeInt,
72+
Optional: true,
73+
Description: "Phone.",
74+
},
75+
"stoken": {
76+
Type: schema.TypeInt,
77+
Optional: true,
78+
Description: "Soft token.",
79+
},
80+
"wechat": {
81+
Type: schema.TypeInt,
82+
Optional: true,
83+
Description: "Wechat.",
84+
},
85+
},
86+
},
87+
},
88+
89+
"action_flag": {
90+
Optional: true,
91+
Type: schema.TypeList,
92+
MaxItems: 1,
93+
Description: "Action flag setting.",
94+
Elem: &schema.Resource{
95+
Schema: map[string]*schema.Schema{
96+
"phone": {
97+
Type: schema.TypeInt,
98+
Optional: true,
99+
Description: "Phone.",
100+
},
101+
"stoken": {
102+
Type: schema.TypeInt,
103+
Optional: true,
104+
Description: "Soft token.",
105+
},
106+
"wechat": {
107+
Type: schema.TypeInt,
108+
Optional: true,
109+
Description: "Wechat.",
110+
},
111+
},
112+
},
113+
},
114+
},
115+
}
116+
}
117+
118+
func resourceTencentCloudCamMfaFlagCreate(d *schema.ResourceData, meta interface{}) error {
119+
defer logElapsed("resource.tencentcloud_cam_mfa_flag.create")()
120+
defer inconsistentCheck(d, meta)()
121+
var opUin int
122+
123+
if v, ok := d.GetOk("op_uin"); ok {
124+
opUin = v.(int)
125+
}
126+
d.SetId(strconv.Itoa(opUin))
127+
return resourceTencentCloudCamMfaFlagUpdate(d, meta)
128+
}
129+
130+
func resourceTencentCloudCamMfaFlagRead(d *schema.ResourceData, meta interface{}) error {
131+
defer logElapsed("resource.tencentcloud_cam_mfa_flag.read")()
132+
defer inconsistentCheck(d, meta)()
133+
134+
logId := getLogId(contextNil)
135+
136+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
137+
opUin := d.Id()
138+
uin, err := strconv.Atoi(opUin)
139+
if err != nil {
140+
return err
141+
}
142+
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
143+
144+
loginFlag, actionFlag, err := service.DescribeCamMfaFlagById(ctx, uint64(uin))
145+
if err != nil {
146+
return err
147+
}
148+
149+
if loginFlag == nil && actionFlag == nil {
150+
log.Printf("[WARN]%s resource `CamMfaFlag` not found, please check if it has been deleted.\n", logId)
151+
return nil
152+
}
153+
154+
if loginFlag != nil {
155+
loginFlagMap := map[string]interface{}{}
156+
157+
if loginFlag.Phone != nil {
158+
loginFlagMap["phone"] = loginFlag.Phone
159+
}
160+
161+
if loginFlag.Stoken != nil {
162+
loginFlagMap["stoken"] = loginFlag.Stoken
163+
}
164+
165+
if loginFlag.Wechat != nil {
166+
loginFlagMap["wechat"] = loginFlag.Wechat
167+
}
168+
169+
_ = d.Set("login_flag", []interface{}{loginFlagMap})
170+
}
171+
172+
if actionFlag != nil {
173+
actionFlagMap := map[string]interface{}{}
174+
175+
if actionFlag.Phone != nil {
176+
actionFlagMap["phone"] = actionFlag.Phone
177+
}
178+
179+
if actionFlag.Stoken != nil {
180+
actionFlagMap["stoken"] = actionFlag.Stoken
181+
}
182+
183+
if actionFlag.Wechat != nil {
184+
actionFlagMap["wechat"] = actionFlag.Wechat
185+
}
186+
187+
_ = d.Set("action_flag", []interface{}{actionFlagMap})
188+
}
189+
190+
return nil
191+
}
192+
193+
func resourceTencentCloudCamMfaFlagUpdate(d *schema.ResourceData, meta interface{}) error {
194+
defer logElapsed("resource.tencentcloud_cam_mfa_flag.update")()
195+
defer inconsistentCheck(d, meta)()
196+
197+
logId := getLogId(contextNil)
198+
opUin := d.Id()
199+
request := cam.NewSetMfaFlagRequest()
200+
uin, err := strconv.Atoi(opUin)
201+
if err != nil {
202+
return err
203+
}
204+
request.OpUin = common.Uint64Ptr(uint64(uin))
205+
206+
if d.HasChange("login_flag") {
207+
if dMap, ok := helper.InterfacesHeadMap(d, "login_flag"); ok {
208+
loginActionMfaFlag := cam.LoginActionMfaFlag{}
209+
if v, ok := dMap["phone"]; ok {
210+
loginActionMfaFlag.Phone = helper.IntUint64(v.(int))
211+
}
212+
if v, ok := dMap["stoken"]; ok {
213+
loginActionMfaFlag.Stoken = helper.IntUint64(v.(int))
214+
}
215+
if v, ok := dMap["wechat"]; ok {
216+
loginActionMfaFlag.Wechat = helper.IntUint64(v.(int))
217+
}
218+
request.LoginFlag = &loginActionMfaFlag
219+
}
220+
}
221+
222+
if d.HasChange("action_flag") {
223+
if dMap, ok := helper.InterfacesHeadMap(d, "action_flag"); ok {
224+
loginActionMfaFlag := cam.LoginActionMfaFlag{}
225+
if v, ok := dMap["phone"]; ok {
226+
loginActionMfaFlag.Phone = helper.IntUint64(v.(int))
227+
}
228+
if v, ok := dMap["stoken"]; ok {
229+
loginActionMfaFlag.Stoken = helper.IntUint64(v.(int))
230+
}
231+
if v, ok := dMap["wechat"]; ok {
232+
loginActionMfaFlag.Wechat = helper.IntUint64(v.(int))
233+
}
234+
request.ActionFlag = &loginActionMfaFlag
235+
}
236+
}
237+
238+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
239+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().SetMfaFlag(request)
240+
if e != nil {
241+
return retryError(e)
242+
} else {
243+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
244+
}
245+
return nil
246+
})
247+
if err != nil {
248+
log.Printf("[CRITAL]%s update cam mfaFlag failed, reason:%+v", logId, err)
249+
return err
250+
}
251+
252+
return resourceTencentCloudCamMfaFlagRead(d, meta)
253+
}
254+
255+
func resourceTencentCloudCamMfaFlagDelete(d *schema.ResourceData, meta interface{}) error {
256+
defer logElapsed("resource.tencentcloud_cam_mfa_flag.delete")()
257+
defer inconsistentCheck(d, meta)()
258+
259+
return nil
260+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCamMfaFlagResource_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: testAccCamMfaFlag,
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttrSet("tencentcloud_cam_mfa_flag.mfa_flag", "id"),
21+
resource.TestCheckResourceAttrSet("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.#"),
22+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.0.phone", "0"),
23+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.0.stoken", "1"),
24+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.0.wechat", "0"),
25+
26+
resource.TestCheckResourceAttrSet("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.#"),
27+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.0.phone", "0"),
28+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.0.stoken", "1"),
29+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.0.wechat", "0"),
30+
),
31+
},
32+
{
33+
Config: testAccCamMfaFlagUpdate,
34+
Check: resource.ComposeTestCheckFunc(
35+
resource.TestCheckResourceAttrSet("tencentcloud_cam_mfa_flag.mfa_flag", "id"),
36+
resource.TestCheckResourceAttrSet("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.#"),
37+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.0.phone", "0"),
38+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.0.stoken", "0"),
39+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "login_flag.0.wechat", "0"),
40+
41+
resource.TestCheckResourceAttrSet("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.#"),
42+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.0.phone", "0"),
43+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.0.stoken", "0"),
44+
resource.TestCheckResourceAttr("tencentcloud_cam_mfa_flag.mfa_flag", "action_flag.0.wechat", "0"),
45+
),
46+
},
47+
},
48+
})
49+
}
50+
51+
const testAccCamMfaFlag = `
52+
data "tencentcloud_user_info" "info"{}
53+
54+
resource "tencentcloud_cam_mfa_flag" "mfa_flag" {
55+
op_uin = data.tencentcloud_user_info.info.uin
56+
login_flag {
57+
phone = 0
58+
stoken = 1
59+
wechat = 0
60+
}
61+
action_flag {
62+
phone = 0
63+
stoken = 1
64+
wechat = 0
65+
}
66+
}
67+
68+
`
69+
const testAccCamMfaFlagUpdate = `
70+
data "tencentcloud_user_info" "info"{}
71+
72+
resource "tencentcloud_cam_mfa_flag" "mfa_flag" {
73+
op_uin = data.tencentcloud_user_info.info.uin
74+
login_flag {
75+
phone = 0
76+
stoken = 0
77+
wechat = 0
78+
}
79+
action_flag {
80+
phone = 0
81+
stoken = 0
82+
wechat = 0
83+
}
84+
}
85+
86+
`

tencentcloud/resource_tc_ssl_commit_certificate_information.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ package tencentcloud
4646

4747
import (
4848
"context"
49+
"log"
50+
4951
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
5052
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5153
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
5254
ssl "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl/v20191205"
5355
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
54-
"log"
5556
)
5657

5758
func resourceTencentCloudSslCommitCertificateInformation() *schema.Resource {

tencentcloud/resource_tc_ssl_commit_certificate_information_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package tencentcloud
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
54
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
67
)
78

89
func TestAccTencentCloudSslCommitCertificateInformationResource_basic(t *testing.T) {

0 commit comments

Comments
 (0)