-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathvpcs.go
166 lines (144 loc) · 3.8 KB
/
vpcs.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
package ecs
import (
"time"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/util"
)
type CreateVpcArgs struct {
RegionId common.Region
CidrBlock string //192.168.0.0/16 or 172.16.0.0/16 (default)
VpcName string
Description string
ClientToken string
}
type CreateVpcResponse struct {
common.Response
VpcId string
VRouterId string
RouteTableId string
}
// CreateVpc creates Virtual Private Cloud
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vpc&createvpc
func (client *Client) CreateVpc(args *CreateVpcArgs) (resp *CreateVpcResponse, err error) {
response := CreateVpcResponse{}
err = client.Invoke("CreateVpc", args, &response)
if err != nil {
return nil, err
}
return &response, err
}
type DeleteVpcArgs struct {
VpcId string
}
type DeleteVpcResponse struct {
common.Response
}
// DeleteVpc deletes Virtual Private Cloud
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vpc&deletevpc
func (client *Client) DeleteVpc(vpcId string) error {
args := DeleteVpcArgs{
VpcId: vpcId,
}
response := DeleteVpcResponse{}
return client.Invoke("DeleteVpc", &args, &response)
}
type VpcStatus string
const (
VpcStatusPending = VpcStatus("Pending")
VpcStatusAvailable = VpcStatus("Available")
)
type DescribeVpcsArgs struct {
VpcId string
RegionId common.Region
common.Pagination
}
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&vpcsettype
type VpcSetType struct {
VpcId string
RegionId common.Region
Status VpcStatus // enum Pending | Available
VpcName string
VSwitchIds struct {
VSwitchId []string
}
CidrBlock string
VRouterId string
Description string
IsDefault bool
CreationTime util.ISO6801Time
SecondaryCidrBlocks struct {
SecondaryCidrBlock []string
}
RouterTableIds struct {
RouterTableIds []string
}
}
type DescribeVpcsResponse struct {
common.Response
common.PaginationResult
Vpcs struct {
Vpc []VpcSetType
}
}
// DescribeInstanceStatus describes instance status
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vpc&describevpcs
func (client *Client) DescribeVpcs(args *DescribeVpcsArgs) (vpcs []VpcSetType, pagination *common.PaginationResult, err error) {
response, err := client.DescribeVpcsWithRaw(args)
if err != nil {
return nil, nil, err
}
return response.Vpcs.Vpc, &response.PaginationResult, nil
}
func (client *Client) DescribeVpcsWithRaw(args *DescribeVpcsArgs) (response *DescribeVpcsResponse, err error) {
args.Validate()
response = &DescribeVpcsResponse{}
err = client.Invoke("DescribeVpcs", args, response)
if err != nil {
return nil, err
}
return response, err
}
type ModifyVpcAttributeArgs struct {
VpcId string
VpcName string
Description string
}
type ModifyVpcAttributeResponse struct {
common.Response
}
// ModifyVpcAttribute modifies attribute of Virtual Private Cloud
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vpc&modifyvpcattribute
func (client *Client) ModifyVpcAttribute(args *ModifyVpcAttributeArgs) error {
response := ModifyVpcAttributeResponse{}
return client.Invoke("ModifyVpcAttribute", args, &response)
}
// WaitForInstance waits for instance to given status
func (client *Client) WaitForVpcAvailable(regionId common.Region, vpcId string, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
args := DescribeVpcsArgs{
RegionId: regionId,
VpcId: vpcId,
}
for {
vpcs, _, err := client.DescribeVpcs(&args)
if err != nil {
return err
}
if len(vpcs) > 0 && vpcs[0].Status == VpcStatusAvailable {
break
}
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return common.GetClientErrorFromString("Timeout")
}
time.Sleep(DefaultWaitForInterval * time.Second)
}
return nil
}