-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_rms_organizational_policy_assignments.go
258 lines (243 loc) · 8.87 KB
/
data_source_huaweicloud_rms_organizational_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
// Generated by PMS #80
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 DataSourceRmsOrganizationalPolicyAssignments() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRmsOrganizationalPolicyAssignmentsRead,
Schema: map[string]*schema.Schema{
"organization_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of the organization.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the organizational policy assignment.`,
},
"assignment_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the organizational policy assignment.`,
},
"policy_definition_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the policy definition.`,
},
"assignments": {
Type: schema.TypeSet,
Computed: true,
Description: `The organization assignments.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"organization_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the organization.`,
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
Description: `The creator of the organizational policy assignment.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the organizational policy assignment.`,
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the organizational policy assignment.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The description of the organizational policy assignment.`,
},
"policy_definition_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the built-in policy definition.`,
},
"period": {
Type: schema.TypeString,
Computed: true,
Description: `The trigger period of the organizational policy assignment.`,
},
"organization_policy_assignment_urn": {
Type: schema.TypeString,
Computed: true,
Description: `The unique identifier of the organizational policy assignment.`,
},
"policy_filter": {
Type: schema.TypeList,
Computed: true,
Description: `The configuration used to filter resources.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"region_id": {
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.`,
},
},
},
},
"parameters": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The rule definition of the organizational policy assignment.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The creation time of the organizational policy assignment.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `The latest update time of the organizational policy assignment.`,
},
},
},
},
},
}
}
type OrganizationalPolicyAssignmentsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newOrganizationalPolicyAssignmentsDSWrapper(d *schema.ResourceData, meta interface{}) *OrganizationalPolicyAssignmentsDSWrapper {
return &OrganizationalPolicyAssignmentsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceRmsOrganizationalPolicyAssignmentsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newOrganizationalPolicyAssignmentsDSWrapper(d, meta)
lisOrgPolAssRst, err := wrapper.ListOrganizationPolicyAssignments()
if err != nil {
return diag.FromErr(err)
}
id, _ := uuid.GenerateUUID()
d.SetId(id)
err = wrapper.listOrganizationPolicyAssignmentsToSchema(lisOrgPolAssRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API CONFIG GET /v1/resource-manager/organizations/{organization_id}/policy-assignments
func (w *OrganizationalPolicyAssignmentsDSWrapper) ListOrganizationPolicyAssignments() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "rms")
if err != nil {
return nil, err
}
d := w.ResourceData
uri := "/v1/resource-manager/organizations/{organization_id}/policy-assignments"
uri = strings.ReplaceAll(uri, "{organization_id}", d.Get("organization_id").(string))
params := map[string]any{
"organization_policy_assignment_id": w.Get("assignment_id"),
"organization_policy_assignment_name": w.Get("name"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("organization_policy_assignments", "page_info.next_marker", "marker").
Filter(
filters.New().From("organization_policy_assignments").
Where("policy_definition_id", "=", w.Get("policy_definition_id")),
).
Request().
Result()
}
func (w *OrganizationalPolicyAssignmentsDSWrapper) listOrganizationPolicyAssignmentsToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("assignments", schemas.SliceToList(body.Get("organization_policy_assignments"),
func(ass gjson.Result) any {
return map[string]any{
"organization_id": ass.Get("organization_id").Value(),
"owner_id": ass.Get("owner_id").Value(),
"name": ass.Get("organization_policy_assignment_name").Value(),
"id": ass.Get("organization_policy_assignment_id").Value(),
"description": ass.Get("description").Value(),
"policy_definition_id": ass.Get("policy_definition_id").Value(),
"period": ass.Get("period").Value(),
"organization_policy_assignment_urn": ass.Get("organization_policy_assignment_urn").Value(),
"policy_filter": schemas.SliceToList(ass.Get("policy_filter"),
func(polFil gjson.Result) any {
return map[string]any{
"region_id": polFil.Get("region_id").Value(),
"resource_provider": polFil.Get("resource_provider").Value(),
"resource_type": polFil.Get("resource_type").Value(),
"resource_id": polFil.Get("resource_id").Value(),
"tag_key": polFil.Get("tag_key").Value(),
"tag_value": polFil.Get("tag_value").Value(),
}
},
),
"parameters": w.setOrgPolAssPar(body, &ass),
"created_at": ass.Get("created_at").Value(),
"updated_at": ass.Get("updated_at").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
func (*OrganizationalPolicyAssignmentsDSWrapper) setOrgPolAssPar(_, 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
}