| 1 |
package ecs |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
|
| 6 |
"github.com/denverdino/aliyungo/common" |
| 7 |
"github.com/denverdino/aliyungo/util" |
| 8 |
) |
| 9 |
|
| 10 |
type CreateVpcArgs struct { |
| 11 |
RegionId common.Region |
| 12 |
CidrBlock string |
| 13 |
VpcName string |
| 14 |
Description string |
| 15 |
ClientToken string |
| 16 |
} |
| 17 |
|
| 18 |
type CreateVpcResponse struct { |
| 19 |
common.Response |
| 20 |
VpcId string |
| 21 |
VRouterId string |
| 22 |
RouteTableId string |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
func (client *Client) CreateVpc(args *CreateVpcArgs) (resp *CreateVpcResponse, err error) { |
| 29 |
response := CreateVpcResponse{} |
| 30 |
err = client.Invoke("CreateVpc", args, &response) |
| 31 |
if err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
return &response, err |
| 35 |
} |
| 36 |
|
| 37 |
type DeleteVpcArgs struct { |
| 38 |
VpcId string |
| 39 |
} |
| 40 |
|
| 41 |
type DeleteVpcResponse struct { |
| 42 |
common.Response |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func (client *Client) DeleteVpc(vpcId string) error { |
| 49 |
args := DeleteVpcArgs{ |
| 50 |
VpcId: vpcId, |
| 51 |
} |
| 52 |
response := DeleteVpcResponse{} |
| 53 |
return client.Invoke("DeleteVpc", &args, &response) |
| 54 |
} |
| 55 |
|
| 56 |
type VpcStatus string |
| 57 |
|
| 58 |
const ( |
| 59 |
VpcStatusPending = VpcStatus("Pending") |
| 60 |
VpcStatusAvailable = VpcStatus("Available") |
| 61 |
) |
| 62 |
|
| 63 |
type DescribeVpcsArgs struct { |
| 64 |
VpcId string |
| 65 |
RegionId common.Region |
| 66 |
common.Pagination |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
type VpcSetType struct { |
| 72 |
VpcId string |
| 73 |
RegionId common.Region |
| 74 |
Status VpcStatus |
| 75 |
VpcName string |
| 76 |
VSwitchIds struct { |
| 77 |
VSwitchId []string |
| 78 |
} |
| 79 |
CidrBlock string |
| 80 |
VRouterId string |
| 81 |
Description string |
| 82 |
IsDefault bool |
| 83 |
CreationTime util.ISO6801Time |
| 84 |
} |
| 85 |
|
| 86 |
type DescribeVpcsResponse struct { |
| 87 |
common.Response |
| 88 |
common.PaginationResult |
| 89 |
Vpcs struct { |
| 90 |
Vpc []VpcSetType |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
func (client *Client) DescribeVpcs(args *DescribeVpcsArgs) (vpcs []VpcSetType, pagination *common.PaginationResult, err error) { |
| 98 |
response, err := client.DescribeVpcsWithRaw(args) |
| 99 |
if err != nil { |
| 100 |
return nil, nil, err |
| 101 |
} |
| 102 |
|
| 103 |
return response.Vpcs.Vpc, &response.PaginationResult, nil |
| 104 |
} |
| 105 |
|
| 106 |
func (client *Client) DescribeVpcsWithRaw(args *DescribeVpcsArgs) (response *DescribeVpcsResponse, err error) { |
| 107 |
args.Validate() |
| 108 |
response = &DescribeVpcsResponse{} |
| 109 |
|
| 110 |
err = client.Invoke("DescribeVpcs", args, response) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
|
| 115 |
return response, err |
| 116 |
} |
| 117 |
|
| 118 |
type ModifyVpcAttributeArgs struct { |
| 119 |
VpcId string |
| 120 |
VpcName string |
| 121 |
Description string |
| 122 |
} |
| 123 |
|
| 124 |
type ModifyVpcAttributeResponse struct { |
| 125 |
common.Response |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func (client *Client) ModifyVpcAttribute(args *ModifyVpcAttributeArgs) error { |
| 132 |
response := ModifyVpcAttributeResponse{} |
| 133 |
return client.Invoke("ModifyVpcAttribute", args, &response) |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
func (client *Client) WaitForVpcAvailable(regionId common.Region, vpcId string, timeout int) error { |
| 138 |
if timeout <= 0 { |
| 139 |
timeout = DefaultTimeout |
| 140 |
} |
| 141 |
args := DescribeVpcsArgs{ |
| 142 |
RegionId: regionId, |
| 143 |
VpcId: vpcId, |
| 144 |
} |
| 145 |
for { |
| 146 |
vpcs, _, err := client.DescribeVpcs(&args) |
| 147 |
if err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
if len(vpcs) > 0 && vpcs[0].Status == VpcStatusAvailable { |
| 151 |
break |
| 152 |
} |
| 153 |
timeout = timeout - DefaultWaitForInterval |
| 154 |
if timeout <= 0 { |
| 155 |
return common.GetClientErrorFromString("Timeout") |
| 156 |
} |
| 157 |
time.Sleep(DefaultWaitForInterval * time.Second) |
| 158 |
} |
| 159 |
return nil |
| 160 |
} |