This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
/
resource_tc_cam_role_policy_attachment.go
209 lines (182 loc) · 6.06 KB
/
resource_tc_cam_role_policy_attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Provides a resource to create a CAM role policy attachment.
Example Usage
```hcl
resource "tencentcloud_cam_role_policy_attachment" "foo" {
role_id = tencentcloud_cam_role.foo.id
policy_id = tencentcloud_cam_policy.foo.id
}
```
Import
CAM role policy attachment can be imported using the id, e.g.
```
$ terraform import tencentcloud_cam_role_policy_attachment.foo 4611686018427922725#26800353
```
*/
package tencentcloud
import (
"context"
"fmt"
"log"
"strconv"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
)
func resourceTencentCloudCamRolePolicyAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudCamRolePolicyAttachmentCreate,
Read: resourceTencentCloudCamRolePolicyAttachmentRead,
Delete: resourceTencentCloudCamRolePolicyAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"role_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Id of the attached CAM role.",
},
"policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Id of the policy.",
},
"create_mode": {
Type: schema.TypeInt,
Computed: true,
Description: "Mode of Creation of the CAM role policy attachment. 1 means the CAM policy attachment is created by production, and the others indicate syntax strategy ways.",
},
"policy_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the policy strategy. 'User' means customer strategy and 'QCS' means preset strategy.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "The create time of the CAM role policy attachment.",
},
"policy_name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the policy.",
},
},
}
}
func resourceTencentCloudCamRolePolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_cam_role_policy_attachment.create")()
logId := getLogId(contextNil)
roleId := d.Get("role_id").(string)
policyId, e := strconv.Atoi(d.Get("policy_id").(string))
if e != nil {
return e
}
policyId64 := uint64(policyId)
request := cam.NewAttachRolePolicyRequest()
request.AttachRoleId = &roleId
request.PolicyId = &policyId64
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().AttachRolePolicy(request)
if e != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), e.Error())
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create CAM role policy attachment failed, reason:%s\n", logId, err.Error())
return err
}
d.SetId(roleId + "#" + strconv.Itoa(policyId))
//get really instance then read
ctx := context.WithValue(context.TODO(), logIdKey, logId)
camService := CamService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
rolePolicyAttachmentId := d.Id()
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
instance, e := camService.DescribeRolePolicyAttachmentById(ctx, rolePolicyAttachmentId)
if e != nil {
return retryError(e)
}
if instance == nil {
return resource.RetryableError(fmt.Errorf("creation not done"))
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM role policy attachment failed, reason:%s\n", logId, err.Error())
return err
}
time.Sleep(10 * time.Second)
return resourceTencentCloudCamRolePolicyAttachmentRead(d, meta)
}
func resourceTencentCloudCamRolePolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_cam_role_policy_attachment.read")()
defer inconsistentCheck(d, meta)()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
rolePolicyAttachmentId := d.Id()
camService := CamService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
var instance *cam.AttachedPolicyOfRole
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := camService.DescribeRolePolicyAttachmentById(ctx, rolePolicyAttachmentId)
if e != nil {
return retryError(e)
}
instance = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM role policy attachment failed, reason:%s\n", logId, err.Error())
return err
}
if instance == nil {
d.SetId("")
return nil
}
roleId, policyId, e := camService.decodeCamPolicyAttachmentId(rolePolicyAttachmentId)
if e != nil {
return e
}
_ = d.Set("role_id", roleId)
_ = d.Set("policy_id", strconv.Itoa(int(policyId)))
_ = d.Set("policy_name", *instance.PolicyName)
_ = d.Set("create_time", *instance.AddTime)
_ = d.Set("create_mode", int(*instance.CreateMode))
_ = d.Set("policy_type", *instance.PolicyType)
return nil
}
func resourceTencentCloudCamRolePolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_cam_role_policy_attachment.delete")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
rolePolicyAttachmentId := d.Id()
camService := CamService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
e := camService.DeleteRolePolicyAttachmentById(ctx, rolePolicyAttachmentId)
if e != nil {
log.Printf("[CRITAL]%s reason[%s]\n", logId, e.Error())
return retryError(e)
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s delete CAM role policy attachment failed, reason:%s\n", logId, err.Error())
return err
}
return nil
}