-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_cc_central_network_policies.go
349 lines (332 loc) · 11.2 KB
/
data_source_huaweicloud_cc_central_network_policies.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Generated by PMS #57
package cc
import (
"context"
"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/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
func DataSourceCcCentralNetworkPolicies() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCcCentralNetworkPoliciesRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"central_network_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of central network.`,
},
"policy_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of central network policy.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the status of central network policy.`,
},
"is_applied": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies whether the central network policy is applied or not.`,
},
"central_network_policies": {
Type: schema.TypeList,
Computed: true,
Description: `The list of the central network policies.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the central network policies.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The status of the central network policy.`,
},
"central_network_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the central network.`,
},
"is_applied": {
Type: schema.TypeBool,
Computed: true,
Description: `Whether the policy is applied or not.`,
},
"version": {
Type: schema.TypeInt,
Computed: true,
Description: `The version of the central network policy.`,
},
"document_template_version": {
Type: schema.TypeString,
Computed: true,
Description: `The document template version of the central network policy.`,
},
"document": {
Type: schema.TypeList,
Computed: true,
Description: `The document of the central network policy.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_plane": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the default central network plane.`,
},
"planes": {
Type: schema.TypeList,
Computed: true,
Description: `The list of the central network planes.`,
Elem: cenNetPolDocPlaElem(),
},
"er_instances": {
Type: schema.TypeList,
Computed: true,
Description: `The list of the enterprise routers instances.`,
Elem: cenNetPolDocErInsElem(),
},
},
},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The creation time of the central network policy.`,
},
},
},
},
},
}
}
// cenNetPolDocErInsElem
// The Elem of "central_network_policies.document.er_instances"
func cenNetPolDocErInsElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The project ID of the enterprise router on the central network.`,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: `The region ID of the enterprise router on the central network.`,
},
"enterprise_router_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the enterprise router on the central network.`,
},
},
}
}
// cenNetPolDocPlaElem
// The Elem of "central_network_policies.document.planes"
func cenNetPolDocPlaElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the central network plane.`,
},
"associate_er_tables": {
Type: schema.TypeList,
Computed: true,
Description: `The list of the enterprise router tables on the central network.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The project ID of the enterprise router table on the central network.`,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: `The region ID of the enterprise router table on the central network.`,
},
"enterprise_router_table_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the enterprise router table on the central network.`,
},
"enterprise_router_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the enterprise router table on the central network.`,
},
},
},
},
"exclude_er_connections": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"exclude_er_instances": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The project ID of the exclude enterprise router instance.`,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: `The region ID of the exclude enterprise router instance`,
},
"enterprise_router_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the exclude enterprise router instance.`,
},
},
},
},
},
},
},
},
}
}
type CentralNetworkPoliciesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newCentralNetworkPoliciesDSWrapper(d *schema.ResourceData, meta interface{}) *CentralNetworkPoliciesDSWrapper {
return &CentralNetworkPoliciesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceCcCentralNetworkPoliciesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newCentralNetworkPoliciesDSWrapper(d, meta)
lisCenNetPolRst, err := wrapper.ListCentralNetworkPolicies()
if err != nil {
return diag.FromErr(err)
}
err = wrapper.listCentralNetworkPoliciesToSchema(lisCenNetPolRst)
if err != nil {
return diag.FromErr(err)
}
id, _ := uuid.GenerateUUID()
d.SetId(id)
return nil
}
// @API CC GET /v3/{domain_id}/gcn/central-network/{central_network_id}/policies
func (w *CentralNetworkPoliciesDSWrapper) ListCentralNetworkPolicies() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "cc")
if err != nil {
return nil, err
}
d := w.ResourceData
uri := "/v3/{domain_id}/gcn/central-network/{central_network_id}/policies"
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID)
uri = strings.ReplaceAll(uri, "{central_network_id}", d.Get("central_network_id").(string))
params := map[string]any{
"id": w.Get("policy_id"),
"state": w.Get("status"),
"is_applied": w.Get("is_applied"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("central_network_policies", "page_info.next_marker", "marker").
Request().
Result()
}
func (w *CentralNetworkPoliciesDSWrapper) listCentralNetworkPoliciesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("central_network_policies", schemas.SliceToList(body.Get("central_network_policies"),
func(cenNetPol gjson.Result) any {
return map[string]any{
"id": cenNetPol.Get("id").Value(),
"status": cenNetPol.Get("state").Value(),
"central_network_id": cenNetPol.Get("central_network_id").Value(),
"is_applied": cenNetPol.Get("is_applied").Value(),
"version": cenNetPol.Get("version").Value(),
"document_template_version": cenNetPol.Get("document_template_version").Value(),
"document": schemas.SliceToList(cenNetPol.Get("document"),
func(doc gjson.Result) any {
return map[string]any{
"default_plane": doc.Get("default_plane").Value(),
"planes": w.setCenNetPolDocPla(doc),
"er_instances": w.setCenNetPolDocErIns(doc),
}
},
),
"created_at": cenNetPol.Get("created_at").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
func (*CentralNetworkPoliciesDSWrapper) setCenNetPolDocPla(doc gjson.Result) any {
return schemas.SliceToList(doc.Get("planes"), func(planes gjson.Result) any {
return map[string]any{
"name": planes.Get("name").Value(),
"associate_er_tables": schemas.SliceToList(planes.Get("associate_er_tables"),
func(assErTab gjson.Result) any {
return map[string]any{
"project_id": assErTab.Get("project_id").Value(),
"region_id": assErTab.Get("region_id").Value(),
"enterprise_router_table_id": assErTab.Get("enterprise_router_table_id").Value(),
"enterprise_router_id": assErTab.Get("enterprise_router_id").Value(),
}
},
),
"exclude_er_connections": schemas.SliceToList(planes.Get("exclude_er_connections"),
func(excErCon gjson.Result) any {
return map[string]any{
"exclude_er_instances": schemas.SliceToList(excErCon,
func(er gjson.Result) any {
return map[string]any{
"project_id": er.Get("project_id").Value(),
"region_id": er.Get("region_id").Value(),
"enterprise_router_id": er.Get("enterprise_router_id").Value(),
}
},
),
}
},
),
}
})
}
func (*CentralNetworkPoliciesDSWrapper) setCenNetPolDocErIns(doc gjson.Result) any {
return schemas.SliceToList(doc.Get("er_instances"), func(erIns gjson.Result) any {
return map[string]any{
"project_id": erIns.Get("project_id").Value(),
"region_id": erIns.Get("region_id").Value(),
"enterprise_router_id": erIns.Get("enterprise_router_id").Value(),
}
})
}