/
data_source_huaweicloud_cc_network_instances.go
240 lines (226 loc) · 7.51 KB
/
data_source_huaweicloud_cc_network_instances.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
// Generated by PMS #91
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 DataSourceCcNetworkInstances() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCcNetworkInstancesRead,
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.`,
},
"network_instance_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the network instance ID.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the network instance name.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the network instance description.`,
},
"cloud_connection_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the cloud connection ID.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the status of the network instance.`,
},
"type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the type of the network instance.`,
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the ID of the VPC or virtual gateway to be loaded to the cloud connection.`,
},
"region_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the region ID of the network instance.`,
},
"network_instances": {
Type: schema.TypeList,
Computed: true,
Description: `Network instance list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `Network instance ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Network instance name.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `Network instance description.`,
},
"domain_id": {
Type: schema.TypeString,
Computed: true,
Description: `ID of the account that the network instance belongs to.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Time when the network instance was created.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `Time when the network instance was updated.`,
},
"cloud_connection_id": {
Type: schema.TypeString,
Computed: true,
Description: `The cloud connection ID.`,
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the VPC or virtual gateway to be loaded to the cloud connection.`,
},
"instance_domain_id": {
Type: schema.TypeString,
Computed: true,
Description: `Account ID of the VPC or virtual gateway.`,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: `Region ID of the network instance.`,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
Description: `Project ID of the network instance.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `Status of the network instance.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `Type of the network instance.`,
},
"cidrs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of routes advertised by the network instance.`,
},
},
},
},
},
}
}
type NetworkInstancesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newNetworkInstancesDSWrapper(d *schema.ResourceData, meta interface{}) *NetworkInstancesDSWrapper {
return &NetworkInstancesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceCcNetworkInstancesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newNetworkInstancesDSWrapper(d, meta)
lisNetInsRst, err := wrapper.ListNetworkInstances()
if err != nil {
return diag.FromErr(err)
}
id, _ := uuid.GenerateUUID()
d.SetId(id)
err = wrapper.listNetworkInstancesToSchema(lisNetInsRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API CC GET /v3/{domain_id}/ccaas/network-instances
func (w *NetworkInstancesDSWrapper) ListNetworkInstances() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "cc")
if err != nil {
return nil, err
}
uri := "/v3/{domain_id}/ccaas/network-instances"
uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID)
params := map[string]any{
"id": w.Get("network_instance_id"),
"name": w.Get("name"),
"description": w.Get("description"),
"cloud_connection_id": w.Get("cloud_connection_id"),
"status": w.Get("status"),
"type": w.Get("type"),
"instance_id": w.Get("instance_id"),
"region_id": w.Get("region_id"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("network_instances", "page_info.next_marker", "marker").
Request().
Result()
}
func (w *NetworkInstancesDSWrapper) listNetworkInstancesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("network_instances", schemas.SliceToList(body.Get("network_instances"),
func(netIns gjson.Result) any {
return map[string]any{
"id": netIns.Get("id").Value(),
"name": netIns.Get("name").Value(),
"description": netIns.Get("description").Value(),
"domain_id": netIns.Get("domain_id").Value(),
"created_at": netIns.Get("created_at").Value(),
"updated_at": netIns.Get("updated_at").Value(),
"cloud_connection_id": netIns.Get("cloud_connection_id").Value(),
"instance_id": netIns.Get("instance_id").Value(),
"instance_domain_id": netIns.Get("instance_domain_id").Value(),
"region_id": netIns.Get("region_id").Value(),
"project_id": netIns.Get("project_id").Value(),
"status": netIns.Get("status").Value(),
"type": netIns.Get("type").Value(),
"cidrs": schemas.SliceToStrList(netIns.Get("cidrs")),
}
},
)),
)
return mErr.ErrorOrNil()
}