forked from aliyun/terraform-provider-alicloud
/
service_alicloud_vpc.go
402 lines (349 loc) · 11.3 KB
/
service_alicloud_vpc.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package alicloud
import (
"time"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
)
func (client *AliyunClient) BuildVpcCommonRequest(region string) *requests.CommonRequest {
request := requests.NewCommonRequest()
endpoint := LoadEndpoint(client.RegionId, VPCCode)
if region == "" {
region = client.RegionId
}
if endpoint == "" {
endpoint, _ = client.DescribeEndpointByCode(region, VPCCode)
}
if endpoint == "" {
endpoint = fmt.Sprintf("vpc.%s.aliyuncs.com", region)
}
request.Domain = endpoint
request.Version = ApiVersion20160428
request.RegionId = region
return request
}
func (client *AliyunClient) DescribeEipAddress(allocationId string) (eip vpc.EipAddress, err error) {
args := vpc.CreateDescribeEipAddressesRequest()
args.RegionId = string(client.Region)
args.AllocationId = allocationId
invoker := NewInvoker()
err = invoker.Run(func() error {
eips, err := client.vpcconn.DescribeEipAddresses(args)
if err != nil {
return err
}
if eips == nil || len(eips.EipAddresses.EipAddress) <= 0 {
return GetNotFoundErrorFromString(GetNotFoundMessage("EIP", allocationId))
}
eip = eips.EipAddresses.EipAddress[0]
return nil
})
return
}
func (client *AliyunClient) DescribeNatGateway(natGatewayId string) (nat vpc.NatGateway, err error) {
args := vpc.CreateDescribeNatGatewaysRequest()
args.RegionId = string(client.Region)
args.NatGatewayId = natGatewayId
invoker := NewInvoker()
err = invoker.Run(func() error {
resp, err := client.vpcconn.DescribeNatGateways(args)
if err != nil {
if IsExceptedError(err, InvalidNatGatewayIdNotFound) {
return GetNotFoundErrorFromString(GetNotFoundMessage("Nat Gateway", natGatewayId))
}
return err
}
if resp == nil || len(resp.NatGateways.NatGateway) <= 0 {
return GetNotFoundErrorFromString(GetNotFoundMessage("Nat Gateway", natGatewayId))
}
nat = resp.NatGateways.NatGateway[0]
return nil
})
return
}
func (client *AliyunClient) DescribeVpc(vpcId string) (v vpc.DescribeVpcAttributeResponse, err error) {
request := vpc.CreateDescribeVpcAttributeRequest()
request.VpcId = vpcId
invoker := NewInvoker()
err = invoker.Run(func() error {
resp, err := client.vpcconn.DescribeVpcAttribute(request)
if err != nil {
if IsExceptedErrors(err, []string{InvalidVpcIDNotFound, ForbiddenVpcNotFound}) {
return GetNotFoundErrorFromString(GetNotFoundMessage("VPC", vpcId))
}
return err
}
if resp == nil || resp.VpcId != vpcId {
return GetNotFoundErrorFromString(GetNotFoundMessage("VPC", vpcId))
}
v = *resp
return nil
})
return
}
func (client *AliyunClient) DescribeVswitch(vswitchId string) (v vpc.DescribeVSwitchAttributesResponse, err error) {
request := vpc.CreateDescribeVSwitchAttributesRequest()
request.VSwitchId = vswitchId
invoker := NewInvoker()
err = invoker.Run(func() error {
resp, err := client.vpcconn.DescribeVSwitchAttributes(request)
if err != nil {
if IsExceptedError(err, InvalidVswitchIDNotFound) {
return GetNotFoundErrorFromString(GetNotFoundMessage("VSwitch", vswitchId))
}
return err
}
if resp == nil || resp.VSwitchId != vswitchId {
return GetNotFoundErrorFromString(GetNotFoundMessage("VSwitch", vswitchId))
}
v = *resp
return nil
})
return
}
func (client *AliyunClient) DescribeSnatEntry(snatTableId string, snatEntryId string) (snat vpc.SnatTableEntry, err error) {
request := vpc.CreateDescribeSnatTableEntriesRequest()
request.RegionId = string(client.Region)
request.SnatTableId = snatTableId
request.PageSize = requests.NewInteger(PageSizeLarge)
for {
invoker := NewInvoker()
var snatEntries *vpc.DescribeSnatTableEntriesResponse
err = invoker.Run(func() error {
resp, err := client.vpcconn.DescribeSnatTableEntries(request)
snatEntries = resp
return err
})
//this special deal cause the DescribeSnatEntry can't find the records would be throw "cant find the snatTable error"
//so judge the snatEntries length priority
if err != nil {
if IsExceptedError(err, InvalidSnatTableIdNotFound) {
return snat, GetNotFoundErrorFromString(GetNotFoundMessage("Snat Entry", snatEntryId))
}
return snat, err
}
if snatEntries == nil || len(snatEntries.SnatTableEntries.SnatTableEntry) < 1 {
break
}
for _, snat := range snatEntries.SnatTableEntries.SnatTableEntry {
if snat.SnatEntryId == snatEntryId {
return snat, nil
}
}
if len(snatEntries.SnatTableEntries.SnatTableEntry) < PageSizeLarge {
break
}
if page, err := getNextpageNumber(request.PageNumber); err != nil {
return snat, err
} else {
request.PageNumber = page
}
}
return snat, GetNotFoundErrorFromString(GetNotFoundMessage("Snat Entry", snatEntryId))
}
func (client *AliyunClient) DescribeForwardEntry(forwardTableId string, forwardEntryId string) (entry vpc.ForwardTableEntry, err error) {
args := vpc.CreateDescribeForwardTableEntriesRequest()
args.RegionId = string(client.Region)
args.ForwardTableId = forwardTableId
invoker := NewInvoker()
err = invoker.Run(func() error {
resp, err := client.vpcconn.DescribeForwardTableEntries(args)
//this special deal cause the DescribeSnatEntry can't find the records would be throw "cant find the snatTable error"
//so judge the snatEntries length priority
if err != nil {
if IsExceptedErrors(err, []string{InvalidForwardEntryIdNotFound, InvalidForwardTableIdNotFound}) {
return GetNotFoundErrorFromString(GetNotFoundMessage("Forward Entry", forwardTableId))
}
return err
}
if resp == nil || len(resp.ForwardTableEntries.ForwardTableEntry) <= 0 {
return GetNotFoundErrorFromString(GetNotFoundMessage("Forward Entry", forwardTableId))
}
for _, forward := range resp.ForwardTableEntries.ForwardTableEntry {
if forward.ForwardEntryId == forwardEntryId {
entry = forward
return nil
}
}
return GetNotFoundErrorFromString(GetNotFoundMessage("Forward Entry", forwardTableId))
})
return
}
func (client *AliyunClient) QueryRouteTableById(routeTableId string) (rt vpc.RouteTable, err error) {
request := vpc.CreateDescribeRouteTablesRequest()
request.RouteTableId = routeTableId
invoker := NewInvoker()
err = invoker.Run(func() error {
rts, err := client.vpcconn.DescribeRouteTables(request)
if err != nil {
return err
}
if rts == nil || len(rts.RouteTables.RouteTable) == 0 ||
rts.RouteTables.RouteTable[0].RouteTableId != routeTableId {
return GetNotFoundErrorFromString(GetNotFoundMessage("Route Table", routeTableId))
}
rt = rts.RouteTables.RouteTable[0]
return nil
})
return
}
func (client *AliyunClient) QueryRouteEntry(routeTableId, cidrBlock, nextHopType, nextHopId string) (rn vpc.RouteEntry, err error) {
rt, err := client.QueryRouteTableById(routeTableId)
if err != nil {
return
}
for _, e := range rt.RouteEntrys.RouteEntry {
if e.DestinationCidrBlock == cidrBlock && e.NextHopType == nextHopType && e.InstanceId == nextHopId {
return e, nil
}
}
return rn, GetNotFoundErrorFromString(GetNotFoundMessage("Route Entry", routeTableId))
}
func (client *AliyunClient) DescribeRouterInterface(regionId, interfaceId string) (ri vpc.RouterInterfaceTypeInDescribeRouterInterfaces, err error) {
request := vpc.CreateDescribeRouterInterfacesRequest()
request.RegionId = regionId
values := []string{interfaceId}
filter := []vpc.DescribeRouterInterfacesFilter{vpc.DescribeRouterInterfacesFilter{
Key: "RouterInterfaceId",
Value: &values,
},
}
request.Filter = &filter
invoker := NewInvoker()
err = invoker.Run(func() error {
resp, err := client.vpcconn.DescribeRouterInterfaces(request)
if err != nil {
return err
}
if resp == nil || len(resp.RouterInterfaceSet.RouterInterfaceType) <= 0 ||
resp.RouterInterfaceSet.RouterInterfaceType[0].RouterInterfaceId != interfaceId {
return GetNotFoundErrorFromString(GetNotFoundMessage("Router Interface", interfaceId))
}
ri = resp.RouterInterfaceSet.RouterInterfaceType[0]
return nil
})
return
}
func (client *AliyunClient) WaitForVpc(vpcId string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
vpc, err := client.DescribeVpc(vpcId)
if err != nil {
return err
}
if vpc.Status == string(status) {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("VPC", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (client *AliyunClient) WaitForVSwitch(vswitchId string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
vswitch, err := client.DescribeVswitch(vswitchId)
if err != nil {
return err
}
if vswitch.Status == string(status) {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("VSwitch", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (client *AliyunClient) WaitForAllRouteEntries(routeTableId string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
table, err := client.QueryRouteTableById(routeTableId)
if err != nil {
return err
}
success := true
for _, routeEntry := range table.RouteEntrys.RouteEntry {
if routeEntry.Status != string(status) {
success = false
break
}
}
if success {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("All Route Entries", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (client *AliyunClient) WaitForRouterInterface(regionId, interfaceId string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
result, err := client.DescribeRouterInterface(regionId, interfaceId)
if err != nil {
return err
} else if result.Status == string(status) {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("Router Interface", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (client *AliyunClient) WaitForEip(allocationId string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
eip, err := client.DescribeEipAddress(allocationId)
if err != nil {
if !NotFoundError(err) {
return err
}
} else if eip.Status == string(status) {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("EIP", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (client *AliyunClient) DeactivateRouterInterface(interfaceId string) error {
req := vpc.CreateDeactivateRouterInterfaceRequest()
req.RouterInterfaceId = interfaceId
if _, err := client.vpcconn.DeactivateRouterInterface(req); err != nil {
return fmt.Errorf("Deactivating RouterInterface %s got an error: %#v.", interfaceId, err)
}
return nil
}
func (client *AliyunClient) ActivateRouterInterface(interfaceId string) error {
req := vpc.CreateActivateRouterInterfaceRequest()
req.RouterInterfaceId = interfaceId
if _, err := client.vpcconn.ActivateRouterInterface(req); err != nil {
return fmt.Errorf("Activating RouterInterface %s got an error: %#v.", interfaceId, err)
}
return nil
}