/
resource_tc_cam_tag_role_attachment.go
211 lines (175 loc) · 5.54 KB
/
resource_tc_cam_tag_role_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
210
211
package cam
import (
"context"
"fmt"
"log"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudCamTagRoleAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudCamTagRoleCreateAttachment,
Read: resourceTencentCloudCamTagRoleReadAttachment,
Delete: resourceTencentCloudCamTagRoleDeleteAttachment,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"tags": {
Required: true,
ForceNew: true,
Type: schema.TypeList,
Description: "Label.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "Label.",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Label.",
},
},
},
},
"role_name": {
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Character name, at least one input with the character ID.",
},
"role_id": {
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Character ID, at least one input with the character name.",
},
},
}
}
func resourceTencentCloudCamTagRoleCreateAttachment(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cam_tag_role_attachment.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
var (
request = cam.NewTagRoleRequest()
roleName string
roleId string
)
if v, ok := d.GetOk("tags"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
roleTags := cam.RoleTags{}
if v, ok := dMap["key"]; ok {
roleTags.Key = helper.String(v.(string))
}
if v, ok := dMap["value"]; ok {
roleTags.Value = helper.String(v.(string))
}
request.Tags = append(request.Tags, &roleTags)
}
}
if v, ok := d.GetOk("role_name"); ok {
roleName = v.(string)
request.RoleName = helper.String(v.(string))
}
if v, ok := d.GetOk("role_id"); ok {
roleId = v.(string)
request.RoleId = helper.String(v.(string))
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCamClient().TagRole(request)
if e != nil {
return tccommon.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 TagRole failed, reason:%+v", logId, err)
return err
}
d.SetId(roleName + tccommon.FILED_SP + roleId)
return resourceTencentCloudCamTagRoleReadAttachment(d, meta)
}
func resourceTencentCloudCamTagRoleReadAttachment(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cam_tag_role_attachment.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := CamService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", idSplit)
}
roleName := idSplit[0]
roleId := idSplit[1]
TagRole, err := service.DescribeCamTagRoleById(ctx, roleName, roleId)
if err != nil {
return err
}
if TagRole == nil {
d.SetId("")
log.Printf("[WARN]%s resource `CamTagRole` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}
if TagRole.Tags != nil {
tagsList := []interface{}{}
for _, tags := range TagRole.Tags {
tagsMap := map[string]interface{}{}
if tags.Key != nil {
tagsMap["key"] = tags.Key
}
if tags.Value != nil {
tagsMap["value"] = tags.Value
}
tagsList = append(tagsList, tagsMap)
}
_ = d.Set("tags", tagsList)
}
if TagRole.RoleName != nil {
_ = d.Set("role_name", TagRole.RoleName)
}
if TagRole.RoleId != nil {
_ = d.Set("role_id", TagRole.RoleId)
}
return nil
}
func resourceTencentCloudCamTagRoleDeleteAttachment(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cam_tag_role_attachment.delete")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := CamService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", idSplit)
}
roleName := idSplit[0]
roleId := idSplit[1]
keys := make([]*string, 0)
if v, ok := d.GetOk("tags"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
var key string
if v, ok := dMap["key"]; ok {
key = v.(string)
}
keys = append(keys, &key)
}
}
if err := service.DeleteCamTagRoleById(ctx, roleName, roleId, keys); err != nil {
return err
}
return nil
}