/
data_source_huaweicloud_cc_central_network_attachments.go
336 lines (322 loc) · 11.8 KB
/
data_source_huaweicloud_cc_central_network_attachments.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
// Generated by PMS #130
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 DataSourceCcCentralNetworkAttachments() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCcCentralNetworkAttachmentsRead,
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 central network ID.`,
},
"attachment_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the attachment ID.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the attachment name.`,
},
"attachment_instance_type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the type of attachment instance.`,
},
"state": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the attachment status.`,
},
"attachment_instance_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the instance ID.`,
},
"central_network_attachments": {
Type: schema.TypeList,
Computed: true,
Description: `List of attachments on the central network.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment name.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment description.`,
},
"domain_id": {
Type: schema.TypeString,
Computed: true,
Description: `The domain ID.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment status.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Time when the resource was created.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `Time when the resource was updated.`,
},
"central_network_id": {
Type: schema.TypeString,
Computed: true,
Description: `The central network ID.`,
},
"central_network_plane_id": {
Type: schema.TypeString,
Computed: true,
Description: `The central network plane ID.`,
},
"global_connection_bandwidth_id": {
Type: schema.TypeString,
Computed: true,
Description: `The global connection bandwidth ID.`,
},
"bandwidth_type": {
Type: schema.TypeString,
Computed: true,
Description: `The bandwidth type.`,
},
"bandwidth_size": {
Type: schema.TypeInt,
Computed: true,
Description: `The bandwidth size.`,
},
"is_frozen": {
Type: schema.TypeBool,
Computed: true,
Description: `Whether the resource is frozen.`,
},
"enterprise_router_id": {
Type: schema.TypeString,
Computed: true,
Description: `The enterprise router ID.`,
},
"enterprise_router_project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The project ID to which the enterprise router belongs.`,
},
"enterprise_router_region_id": {
Type: schema.TypeString,
Computed: true,
Description: `The region ID to which the enterprise router belongs.`,
},
"enterprise_router_attachment_id": {
Type: schema.TypeString,
Computed: true,
Description: `The enterprise router attachment ID.`,
},
"attachment_instance_type": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment instance type.`,
},
"attachment_instance_id": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment instance ID.`,
},
"attachment_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the enterprise router connection.`,
},
"attachment_instance_project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The project ID of the attachment instance.`,
},
"attachment_instance_region_id": {
Type: schema.TypeString,
Computed: true,
Description: `The Region ID of the attachment instance.`,
},
"attachment_instance_site_code": {
Type: schema.TypeString,
Computed: true,
Description: `The attachment instance site code.`,
},
"enterprise_router_site_code": {
Type: schema.TypeString,
Computed: true,
Description: `The enterprise router site code.`,
},
"specification_value": {
Type: schema.TypeList,
Computed: true,
Description: `Additional information about an attachment.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enterprise_router_table_id": {
Type: schema.TypeString,
Computed: true,
Description: `The enterprise router table ID.`,
},
"attached_er_id": {
Type: schema.TypeString,
Computed: true,
Description: `The attached enterprise router ID.`,
},
"approved_state": {
Type: schema.TypeString,
Computed: true,
Description: `Approval status.`,
},
"hosted_cloud": {
Type: schema.TypeString,
Computed: true,
Description: `Huawei Cloud or partner cloud.`,
},
"reason": {
Type: schema.TypeString,
Computed: true,
Description: `Reason for rejecting attachment creation.`,
},
},
},
},
},
},
},
},
}
}
type CentralNetworkAttachmentsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newCentralNetworkAttachmentsDSWrapper(d *schema.ResourceData, meta interface{}) *CentralNetworkAttachmentsDSWrapper {
return &CentralNetworkAttachmentsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceCcCentralNetworkAttachmentsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newCentralNetworkAttachmentsDSWrapper(d, meta)
lisCenNetAttRst, err := wrapper.ListCentralNetworkAttachments()
if err != nil {
return diag.FromErr(err)
}
id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
err = wrapper.listCentralNetworkAttachmentsToSchema(lisCenNetAttRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API CC GET /v3/{domain_id}/gcn/central-network/{central_network_id}/attachments
func (w *CentralNetworkAttachmentsDSWrapper) ListCentralNetworkAttachments() (*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}/attachments"
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("attachment_id"),
"name": w.Get("name"),
"attachment_instance_type": w.Get("attachment_instance_type"),
"state": w.Get("state"),
"attachment_instance_id": w.Get("attachment_instance_id"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("central_network_attachments", "page_info.next_marker", "marker").
Request().
Result()
}
func (w *CentralNetworkAttachmentsDSWrapper) listCentralNetworkAttachmentsToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("central_network_attachments", schemas.SliceToList(body.Get("central_network_attachments"),
func(cenNetAtt gjson.Result) any {
return map[string]any{
"id": cenNetAtt.Get("id").Value(),
"name": cenNetAtt.Get("name").Value(),
"description": cenNetAtt.Get("description").Value(),
"domain_id": cenNetAtt.Get("domain_id").Value(),
"state": cenNetAtt.Get("state").Value(),
"created_at": cenNetAtt.Get("created_at").Value(),
"updated_at": cenNetAtt.Get("updated_at").Value(),
"central_network_id": cenNetAtt.Get("central_network_id").Value(),
"central_network_plane_id": cenNetAtt.Get("central_network_plane_id").Value(),
"global_connection_bandwidth_id": cenNetAtt.Get("global_connection_bandwidth_id").Value(),
"bandwidth_type": cenNetAtt.Get("bandwidth_type").Value(),
"bandwidth_size": cenNetAtt.Get("bandwidth_size").Value(),
"is_frozen": cenNetAtt.Get("is_frozen").Value(),
"enterprise_router_id": cenNetAtt.Get("enterprise_router_id").Value(),
"enterprise_router_project_id": cenNetAtt.Get("enterprise_router_project_id").Value(),
"enterprise_router_region_id": cenNetAtt.Get("enterprise_router_region_id").Value(),
"enterprise_router_attachment_id": cenNetAtt.Get("enterprise_router_attachment_id").Value(),
"attachment_instance_type": cenNetAtt.Get("attachment_instance_type").Value(),
"attachment_instance_id": cenNetAtt.Get("attachment_instance_id").Value(),
"attachment_id": cenNetAtt.Get("attachment_id").Value(),
"attachment_instance_project_id": cenNetAtt.Get("attachment_instance_project_id").Value(),
"attachment_instance_region_id": cenNetAtt.Get("attachment_instance_region_id").Value(),
"attachment_instance_site_code": cenNetAtt.Get("attachment_instance_site_code").Value(),
"enterprise_router_site_code": cenNetAtt.Get("enterprise_router_site_code").Value(),
"specification_value": schemas.SliceToList(cenNetAtt.Get("specification_value"),
func(speVal gjson.Result) any {
return map[string]any{
"enterprise_router_table_id": speVal.Get("enterprise_router_table_id").Value(),
"attached_er_id": speVal.Get("attached_er_id").Value(),
"approved_state": speVal.Get("approved_state").Value(),
"hosted_cloud": speVal.Get("hosted_cloud").Value(),
"reason": speVal.Get("reason").Value(),
}
},
),
}
},
)),
)
return mErr.ErrorOrNil()
}