/
data_source_tc_cam_policy_granting_service_access.go
254 lines (214 loc) · 6.89 KB
/
data_source_tc_cam_policy_granting_service_access.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cam
import (
"context"
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 DataSourceTencentCloudCamPolicyGrantingServiceAccess() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCamPolicyGrantingServiceAccessRead,
Schema: map[string]*schema.Schema{
"target_uin": {
Optional: true,
Type: schema.TypeInt,
Description: "Sub-account uin, one of the three (TargetUin, RoleId, GroupId) must be passed.",
},
"role_id": {
Optional: true,
Type: schema.TypeInt,
Description: "Role Id, one of the three (TargetUin, RoleId, GroupId) must be passed.",
},
"group_id": {
Optional: true,
Type: schema.TypeInt,
Description: "Group Id, one of the three (TargetUin, RoleId, GroupId) must be passed.",
},
"service_type": {
Optional: true,
Type: schema.TypeString,
Description: "Service type, this field needs to be passed when viewing the details of the service authorization interface.",
},
"list": {
Computed: true,
Type: schema.TypeList,
Description: "List.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeList,
Computed: true,
Description: "Service info.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_type": {
Type: schema.TypeString,
Computed: true,
Description: "Service type.",
},
"service_name": {
Type: schema.TypeString,
Computed: true,
Description: "Service name.",
},
},
},
},
"action": {
Type: schema.TypeList,
Computed: true,
Description: "Action list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Action name.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Action description.",
},
},
},
},
"policy": {
Type: schema.TypeList,
Computed: true,
Description: "Policy list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"policy_id": {
Type: schema.TypeString,
Computed: true,
Description: "Policy Id.",
},
"policy_name": {
Type: schema.TypeString,
Computed: true,
Description: "Policy name.",
},
"policy_type": {
Type: schema.TypeString,
Computed: true,
Description: "Polic type.",
},
"policy_description": {
Type: schema.TypeString,
Computed: true,
Description: "Policy description.",
},
},
},
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudCamPolicyGrantingServiceAccessRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_cam_policy_granting_service_access.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
var (
targetUin string
roleId string
groupId string
)
paramMap := make(map[string]interface{})
if v, _ := d.GetOkExists("target_uin"); v != nil {
paramMap["TargetUin"] = helper.IntUint64(v.(int))
}
if v, _ := d.GetOkExists("role_id"); v != nil {
paramMap["RoleId"] = helper.IntUint64(v.(int))
}
if v, _ := d.GetOkExists("group_id"); v != nil {
paramMap["GroupId"] = helper.IntUint64(v.(int))
}
if v, ok := d.GetOk("service_type"); ok {
paramMap["ServiceType"] = helper.String(v.(string))
}
service := CamService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var list []*cam.ListGrantServiceAccessNode
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeCamPolicyGrantingServiceAccessByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
list = result
return nil
})
if err != nil {
return err
}
tmpList := make([]map[string]interface{}, 0, len(list))
if list != nil {
for _, listGrantServiceAccessNode := range list {
listGrantServiceAccessNodeMap := map[string]interface{}{}
if listGrantServiceAccessNode.Service != nil {
serviceMap := map[string]interface{}{}
if listGrantServiceAccessNode.Service.ServiceType != nil {
serviceMap["service_type"] = listGrantServiceAccessNode.Service.ServiceType
}
if listGrantServiceAccessNode.Service.ServiceName != nil {
serviceMap["service_name"] = listGrantServiceAccessNode.Service.ServiceName
}
listGrantServiceAccessNodeMap["service"] = []interface{}{serviceMap}
}
if listGrantServiceAccessNode.Action != nil {
var actionList []interface{}
for _, action := range listGrantServiceAccessNode.Action {
actionMap := map[string]interface{}{}
if action.Name != nil {
actionMap["name"] = action.Name
}
if action.Description != nil {
actionMap["description"] = action.Description
}
actionList = append(actionList, actionMap)
}
listGrantServiceAccessNodeMap["action"] = actionList
}
if listGrantServiceAccessNode.Policy != nil {
var policyList []interface{}
for _, policy := range listGrantServiceAccessNode.Policy {
policyMap := map[string]interface{}{}
if policy.PolicyId != nil {
policyMap["policy_id"] = policy.PolicyId
}
if policy.PolicyName != nil {
policyMap["policy_name"] = policy.PolicyName
}
if policy.PolicyType != nil {
policyMap["policy_type"] = policy.PolicyType
}
if policy.PolicyDescription != nil {
policyMap["policy_description"] = policy.PolicyDescription
}
policyList = append(policyList, policyMap)
}
listGrantServiceAccessNodeMap["policy"] = policyList
}
tmpList = append(tmpList, listGrantServiceAccessNodeMap)
}
_ = d.Set("list", tmpList)
}
d.SetId(targetUin + tccommon.FILED_SP + roleId + groupId)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}