-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun_vpc.go
495 lines (355 loc) · 9.5 KB
/
aliyun_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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package aliyun
import (
"fmt"
"strings"
"sync"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/sirupsen/logrus"
)
func (p *Aliyun) describeVPCs(vpcIds ...string) (resp *vpc.DescribeVpcsResponse, err error) {
describeReq := vpc.CreateDescribeVpcsRequest()
describeReq.RegionId = p.Region
describeReq.VpcId = strings.Join(vpcIds, ",")
resp, err = p.VPCClient().DescribeVpcs(describeReq)
if err != nil {
return
}
return
}
func (p *Aliyun) describeVSwitches(vpcId string, switchIds ...string) (resp *vpc.DescribeVSwitchesResponse, err error) {
describeReq := vpc.CreateDescribeVSwitchesRequest()
describeReq.RegionId = p.Region
describeReq.VpcId = vpcId
describeReq.VSwitchId = strings.Join(switchIds, ",")
resp, err = p.VPCClient().DescribeVSwitches(describeReq)
if err != nil {
return
}
return
}
func (p *Aliyun) CreateVPCs() (err error) {
vpcsConf := p.Config.GetConfig("aliyun.vpc.vpc")
if vpcsConf.IsEmpty() {
return
}
vpcDescribeResp, err := p.describeVPCs()
if err != nil {
return
}
var createReqList []*vpc.CreateVpcRequest
for _, vpcName := range vpcsConf.Keys() {
vpcConf := vpcsConf.GetConfig(vpcName)
vpcId := vpcConf.GetString("id")
if len(vpcId) > 0 {
continue
}
created := false
desc := vpcConf.GetString("description")
req := vpc.CreateCreateVpcRequest()
req.RegionId = p.Region
req.VpcName = vpcName
req.CidrBlock = vpcConf.GetString("cidr-block", "172.16.0.0/16")
req.Description = p.signWithCode(desc)
for _, s := range vpcDescribeResp.Vpcs.Vpc {
if s.VpcName == req.VpcName &&
s.CidrBlock == req.CidrBlock &&
s.RegionId == req.RegionId &&
p.isSignd(s.Description) {
created = true
vpcId = s.VpcId
break
}
}
if created == true {
logrus.WithField("CODE", p.Code).WithField("VPCID", vpcId).Infoln("VPC already created")
continue
}
createReqList = append(createReqList, req)
}
for _, arg := range createReqList {
resp, e := p.VPCClient().CreateVpc(arg)
if e != nil {
return e
}
logrus.WithField("CODE", p.Code).
WithField("ECS-VPC-NAME", arg.VpcName).
WithField("ECS-VPC-ID", resp.VpcId).
WithField("ECS-VPC-REGION", arg.RegionId).
Infoln("VPC created")
}
return
}
func (p *Aliyun) DeleteVPC() (err error) {
vpcsConf := p.Config.GetConfig("aliyun.vpc.vpc")
if vpcsConf.IsEmpty() {
return
}
vpcDescribeResp, err := p.describeVPCs()
if err != nil {
return
}
var deleteReqList []*vpc.DeleteVpcRequest
for _, vpcName := range vpcsConf.Keys() {
vpcConf := vpcsConf.GetConfig(vpcName)
vpcId := vpcConf.GetString("id")
if len(vpcId) == 0 {
for _, s := range vpcDescribeResp.Vpcs.Vpc {
if s.CidrBlock == vpcConf.GetString("cidr-block", "172.16.0.0/16") &&
s.RegionId == p.Region &&
s.VpcName == vpcName &&
p.isSignd(s.Description) {
vpcId = s.VpcId
logrus.WithField("CODE", p.Code).WithField("VPCID", vpcId).WithField("NAME", s.VpcName).Infoln("VPC found at aliyun")
break
}
}
}
if len(vpcId) > 0 {
req := vpc.CreateDeleteVpcRequest()
req.VpcId = vpcId
deleteReqList = append(deleteReqList, req)
}
}
for _, req := range deleteReqList {
_, err = p.VPCClient().DeleteVpc(req)
if err != nil {
return
}
logrus.WithField("CODE", p.Code).
WithField("ECS-VPC-ID", req.VpcId).
Infoln("VPC deleted")
}
return
}
func (p *Aliyun) WaitForAllVpcRunning(timeout int) (err error) {
vpcsConf := p.Config.GetConfig("aliyun.vpc.vpc")
if vpcsConf.IsEmpty() {
return
}
vpcDescribeResp, err := p.describeVPCs()
if err != nil {
return
}
mapVpcs := map[string]string{}
for _, s := range vpcDescribeResp.Vpcs.Vpc {
mapVpcs[s.VpcName] = s.VpcId
}
var vpcIds []string
for _, vpcName := range vpcsConf.Keys() {
vpcId, exist := mapVpcs[vpcName]
if !exist {
continue
}
vpcIds = append(vpcIds, vpcId)
}
if len(vpcIds) == 0 {
return
}
wg := &sync.WaitGroup{}
wg.Add(len(vpcIds))
for i := 0; i < len(vpcIds); i++ {
go func(vpcId string) {
defer wg.Done()
p.WaitForVpcAvailable(vpcId, timeout)
}(vpcIds[i])
}
logrus.WithField("CODE", p.Code).Infoln("Wait for all VPC available")
wg.Wait()
return
}
func (p *Aliyun) FindVPC(vpcName string) (ret *vpc.Vpc, err error) {
vpcDescribeResp, err := p.describeVPCs()
for i, vpc := range vpcDescribeResp.Vpcs.Vpc {
if vpcName == vpc.VpcName &&
p.isSignd(vpc.Description) {
ret = &vpcDescribeResp.Vpcs.Vpc[i]
return
}
}
return
}
func (p *Aliyun) FindVSwitch(vpcName, vSwitchName string) (ret *vpc.VSwitch, err error) {
vpcInst, err := p.FindVPC(vpcName)
if err != nil {
return
}
if vpcInst == nil {
return
}
vswitchesDescribe, err := p.describeVSwitches(vpcInst.VpcId)
if err != nil {
return
}
for i, vswitch := range vswitchesDescribe.VSwitches.VSwitch {
if vswitch.VSwitchName == vSwitchName &&
p.isSignd(vswitch.Description) {
ret = &vswitchesDescribe.VSwitches.VSwitch[i]
return
}
}
return
}
func (p *Aliyun) CreateVSwitch() (err error) {
vSwitchesConf := p.Config.GetConfig("aliyun.vpc.vswitch")
if vSwitchesConf.IsEmpty() {
return
}
var createReqList []*vpc.CreateVSwitchRequest
for _, vSwitchName := range vSwitchesConf.Keys() {
vSwitchConf := vSwitchesConf.GetConfig(vSwitchName)
vpcName := vSwitchConf.GetString("vpc-name")
if len(vpcName) == 0 {
err = fmt.Errorf("vswitch config of %s's vpc-name is not set", vSwitchName)
return
}
vpcId := ""
var vpcInst *vpc.Vpc
vpcInst, err = p.FindVPC(vpcName)
if err != nil {
return
}
if vpcInst == nil {
err = fmt.Errorf("vswitch config of %s's vpc-name: %s is not found at aliyun", vSwitchName, vpcName)
return
}
vpcId = vpcInst.VpcId
logrus.WithField("CODE", p.Code).
WithField("VPCID", vpcId).
WithField("VSWITCH", vSwitchName).Infof("Found vswitch @ %s", vpcInst.VpcId)
if len(vpcId) == 0 {
err = fmt.Errorf("vswitch config of %s's vpc-name: %s is not found at aliyun", vSwitchName, vpcName)
return
}
var vSwitch *vpc.VSwitch
vSwitch, err = p.FindVSwitch(vpcName, vSwitchName)
if err != nil {
return
}
// already created, ignore
if vSwitch != nil {
logrus.WithField("CODE", p.Code).
WithField("VPCID", vpcId).
WithField("VSWITCH", vSwitchName).WithField("VSWITCH-ID", vSwitch.VSwitchId).Infoln("VSwitch already created")
continue
}
zoneId := vSwitchConf.GetString("zone-id")
if len(zoneId) == 0 {
err = fmt.Errorf("the config of zone-id is empty in vswitch of %s", vSwitchName)
return
}
if len(zoneId) > 0 {
if !strings.HasPrefix(zoneId, string(p.Region)) {
err = fmt.Errorf("zone-id is illegal, zone-id's prefix should be region")
return
}
}
cidr := vSwitchConf.GetString("cidr-block", "172.16.0.0/24")
desc := vSwitchConf.GetString("description")
req := vpc.CreateCreateVSwitchRequest()
req.VpcId = vpcId
req.ZoneId = zoneId
req.CidrBlock = cidr
req.VSwitchName = vSwitchName
req.Description = p.signWithCode(desc)
createReqList = append(createReqList, req)
}
for _, req := range createReqList {
var resp *vpc.CreateVSwitchResponse
resp, err = p.VPCClient().CreateVSwitch(req)
if err != nil {
return
}
logrus.WithField("CODE", p.Code).
WithField("ECS-VSWITCH-NAME", req.VSwitchName).
WithField("ECS-VSWITCH-ID", resp.VSwitchId).
Infoln("VSwitch created")
}
return
}
func (p *Aliyun) DeleteVSwitch() (err error) {
vSwitchsConf := p.Config.GetConfig("aliyun.vpc.vswitch")
if vSwitchsConf.IsEmpty() {
return
}
var deleteReqList []*vpc.DeleteVSwitchRequest
for _, vSwitchName := range vSwitchsConf.Keys() {
vSwitchConf := vSwitchsConf.GetConfig(vSwitchName)
vpcName := vSwitchConf.GetString("vpc-name")
if len(vpcName) == 0 {
err = fmt.Errorf("vswitch config of %s's vpc-name is not set", vSwitchName)
return
}
var vSwtich *vpc.VSwitch
vSwtich, err = p.FindVSwitch(vpcName, vSwitchName)
if err != nil {
return
}
if vSwtich == nil {
continue
}
req := vpc.CreateDeleteVSwitchRequest()
req.RegionId = p.Region
req.VSwitchId = vSwtich.VSwitchId
deleteReqList = append(deleteReqList, req)
}
for _, req := range deleteReqList {
_, err = p.VPCClient().DeleteVSwitch(req)
if err != nil {
return
}
time.Sleep(time.Second * 2) // waitting for router list to be deleted, or else, it will error while delete vpc
logrus.WithField("CODE", p.Code).
WithField("ECS-VSWITCH-ID", req.VSwitchId).
Infoln("VSwitch deleted")
}
return
}
func (p *Aliyun) WaitForVpcAvailable(vpcId string, timeout int) (err error) {
if timeout <= 0 {
timeout = 60
}
for {
var resp *vpc.DescribeVpcsResponse
resp, err = p.describeVPCs(vpcId)
if err != nil {
return err
}
if len(resp.Vpcs.Vpc) > 0 && resp.Vpcs.Vpc[0].Status == "Available" {
break
}
timeout = timeout - 5
if timeout <= 0 {
err = fmt.Errorf("wait for vpc '%s' available timeout", vpcId)
return
}
time.Sleep(5 * time.Second)
}
return nil
}
func (p *Aliyun) WaitForVSwitchAvailable(vpcId string, vswitchId string, timeout int) (err error) {
if timeout <= 0 {
timeout = 60
}
for {
var resp *vpc.DescribeVSwitchesResponse
resp, err = p.describeVSwitches(vpcId, vswitchId)
if err != nil {
return
}
if len(resp.VSwitches.VSwitch) == 0 {
err = fmt.Errorf("vswitch %s not found", vswitchId)
return
}
if resp.VSwitches.VSwitch[0].Status == "Available" {
break
}
timeout = timeout - 5
if timeout <= 0 {
err = fmt.Errorf("wait for vsiwtch '%s' available timeout", vswitchId)
return
}
time.Sleep(5 * time.Second)
}
return nil
}