/
data_source_huaweicloud_rms_policy_assignments.go
303 lines (286 loc) · 9.71 KB
/
data_source_huaweicloud_rms_policy_assignments.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Generated by PMS #35
package rms
import (
"context"
"encoding/json"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
func DataSourceRmsPolicyAssignments() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRmsPolicyAssignmentsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the assignment name.`,
},
"assignment_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the policy assignment.`,
},
"policy_definition_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the policy definition.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the expect status of the policy.`,
},
"assignments": {
Type: schema.TypeList,
Computed: true,
Description: `The policy assignment list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The policy assignment ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The policy assignment name.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The policy assignment description.`,
},
"policy_assignment_type": {
Type: schema.TypeString,
Computed: true,
Description: `Specifies the policy assignment type.`,
},
"policy_definition_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the policy used by the policy assignment.`,
},
"period": {
Type: schema.TypeString,
Computed: true,
Description: `The policy assignment period.`,
},
"policy_filter": {
Type: schema.TypeList,
Computed: true,
Description: `The configuration used to filter resources.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the region to which the filtered resources belong.`,
},
"resource_provider": {
Type: schema.TypeString,
Computed: true,
Description: `The service name to which the filtered resources belong.`,
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: `The resource type of the filtered resources.`,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
Description: `The resource ID used to filter a specified resource.`,
},
"tag_key": {
Type: schema.TypeString,
Computed: true,
Description: `The tag name used to filter resources.`,
},
"tag_value": {
Type: schema.TypeString,
Computed: true,
Description: `The tag value used to filter resources.`,
},
},
},
},
"custom_policy": {
Type: schema.TypeList,
Computed: true,
Description: `The configuration of the custom policy.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"function_urn": {
Type: schema.TypeString,
Computed: true,
Description: `The function URN used to create the custom policy.`,
},
"auth_type": {
Type: schema.TypeString,
Computed: true,
Description: `The authorization type of the custom policy.`,
},
"auth_value": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The authorization value of the custom policy.`,
},
},
},
},
"parameters": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The policy assignment parameter.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The policy assignment status.`,
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: `The policy assignment creator.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The creation time of the policy assignment.`,
},
"updated": {
Type: schema.TypeString,
Computed: true,
Description: `The latest update time of the policy assignment.`,
},
},
},
},
},
}
}
type PolicyAssignmentsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newPolicyAssignmentsDSWrapper(d *schema.ResourceData, meta interface{}) *PolicyAssignmentsDSWrapper {
return &PolicyAssignmentsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceRmsPolicyAssignmentsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newPolicyAssignmentsDSWrapper(d, meta)
lisPolAssRst, err := wrapper.ListPolicyAssignments()
if err != nil {
return diag.FromErr(err)
}
id, _ := uuid.GenerateUUID()
d.SetId(id)
err = wrapper.listPolicyAssignmentsToSchema(lisPolAssRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API CONFIG GET /v1/resource-manager/domains/{domain_id}/policy-assignments
func (w *PolicyAssignmentsDSWrapper) ListPolicyAssignments() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "rms")
if err != nil {
return nil, err
}
uri := "/v1/resource-manager/domains/{domain_id}/policy-assignments"
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID)
params := map[string]any{
"policy_assignment_name": w.Get("name"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("value", "page_info.next_marker", "marker").
Filter(
filters.New().From("value").
Where("policy_definition_id", "=", w.Get("policy_definition_id")).
Where("state", "=", w.Get("status")).
Where("id", "=", w.Get("assignment_id")),
).
Request().
Result()
}
func (w *PolicyAssignmentsDSWrapper) listPolicyAssignmentsToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("assignments", schemas.SliceToList(body.Get("value"),
func(assignments gjson.Result) any {
return map[string]any{
"id": assignments.Get("id").Value(),
"name": assignments.Get("name").Value(),
"description": assignments.Get("description").Value(),
"policy_assignment_type": assignments.Get("policy_assignment_type").Value(),
"policy_definition_id": assignments.Get("policy_definition_id").Value(),
"period": assignments.Get("period").Value(),
"policy_filter": schemas.SliceToList(assignments.Get("policy_filter"),
func(policyFilter gjson.Result) any {
return map[string]any{
"region": policyFilter.Get("region_id").Value(),
"resource_provider": policyFilter.Get("resource_provider").Value(),
"resource_type": policyFilter.Get("resource_type").Value(),
"resource_id": policyFilter.Get("resource_id").Value(),
"tag_key": policyFilter.Get("tag_key").Value(),
"tag_value": policyFilter.Get("tag_value").Value(),
}
},
),
"custom_policy": schemas.SliceToList(assignments.Get("custom_policy"),
func(customPolicy gjson.Result) any {
return map[string]any{
"function_urn": customPolicy.Get("function_urn").Value(),
"auth_type": customPolicy.Get("auth_type").Value(),
"auth_value": w.setValCusPolAutVal(body, &customPolicy),
}
},
),
"parameters": w.setValueParameters(body, &assignments),
"status": assignments.Get("state").Value(),
"created_by": assignments.Get("created_by").Value(),
"created_at": assignments.Get("created").Value(),
"updated": assignments.Get("updated").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
func (*PolicyAssignmentsDSWrapper) setValCusPolAutVal(_, data *gjson.Result) map[string]interface{} {
authValues := data.Get("auth_value").Map()
res := make(map[string]interface{})
for k, v := range authValues {
jsonBytes, _ := json.Marshal(v)
res[k] = string(jsonBytes)
}
return res
}
func (*PolicyAssignmentsDSWrapper) setValueParameters(_, data *gjson.Result) map[string]interface{} {
parameters := data.Get("parameters")
result := make(map[string]interface{})
for k, v := range parameters.Map() {
val := v.Map()
jsonBytes, _ := json.Marshal(val["value"])
result[k] = string(jsonBytes)
}
return result
}