| 1 |
|
| 2 |
|
| 3 |
package ecs |
| 4 |
|
| 5 |
import ( |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/denverdino/aliyungo/common" |
| 9 |
"github.com/denverdino/aliyungo/util" |
| 10 |
) |
| 11 |
|
| 12 |
type AllocatePublicIpAddressArgs struct { |
| 13 |
InstanceId string |
| 14 |
} |
| 15 |
|
| 16 |
type AllocatePublicIpAddressResponse struct { |
| 17 |
common.Response |
| 18 |
|
| 19 |
IpAddress string |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
func (client *Client) AllocatePublicIpAddress(instanceId string) (ipAddress string, err error) { |
| 26 |
args := AllocatePublicIpAddressArgs{ |
| 27 |
InstanceId: instanceId, |
| 28 |
} |
| 29 |
response := AllocatePublicIpAddressResponse{} |
| 30 |
err = client.Invoke("AllocatePublicIpAddress", &args, &response) |
| 31 |
if err != nil { |
| 32 |
return "", err |
| 33 |
} |
| 34 |
return response.IpAddress, nil |
| 35 |
} |
| 36 |
|
| 37 |
type ModifyInstanceNetworkSpec struct { |
| 38 |
InstanceId string |
| 39 |
InternetMaxBandwidthOut *int |
| 40 |
InternetMaxBandwidthIn *int |
| 41 |
NetworkChargeType common.InternetChargeType |
| 42 |
} |
| 43 |
|
| 44 |
type ModifyInstanceNetworkSpecResponse struct { |
| 45 |
common.Response |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func (client *Client) ModifyInstanceNetworkSpec(args *ModifyInstanceNetworkSpec) error { |
| 52 |
|
| 53 |
response := ModifyInstanceNetworkSpecResponse{} |
| 54 |
return client.Invoke("ModifyInstanceNetworkSpec", args, &response) |
| 55 |
} |
| 56 |
|
| 57 |
type AllocateEipAddressArgs struct { |
| 58 |
RegionId common.Region |
| 59 |
Bandwidth int |
| 60 |
InternetChargeType common.InternetChargeType |
| 61 |
ISP string |
| 62 |
ClientToken string |
| 63 |
} |
| 64 |
|
| 65 |
type AllocateEipAddressResponse struct { |
| 66 |
common.Response |
| 67 |
EipAddress string |
| 68 |
AllocationId string |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func (client *Client) AllocateEipAddress(args *AllocateEipAddressArgs) (EipAddress string, AllocationId string, err error) { |
| 75 |
if args.Bandwidth == 0 { |
| 76 |
args.Bandwidth = 5 |
| 77 |
} |
| 78 |
response := AllocateEipAddressResponse{} |
| 79 |
err = client.Invoke("AllocateEipAddress", args, &response) |
| 80 |
if err != nil { |
| 81 |
return "", "", err |
| 82 |
} |
| 83 |
return response.EipAddress, response.AllocationId, nil |
| 84 |
} |
| 85 |
|
| 86 |
type EipInstanceType string |
| 87 |
|
| 88 |
const ( |
| 89 |
EcsInstance = "EcsInstance" |
| 90 |
SlbInstance = "SlbInstance" |
| 91 |
Nat = "Nat" |
| 92 |
HaVip = "HaVip" |
| 93 |
) |
| 94 |
|
| 95 |
type AssociateEipAddressArgs struct { |
| 96 |
AllocationId string |
| 97 |
InstanceId string |
| 98 |
InstanceType EipInstanceType |
| 99 |
} |
| 100 |
|
| 101 |
type AssociateEipAddressResponse struct { |
| 102 |
common.Response |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
func (client *Client) AssociateEipAddress(allocationId string, instanceId string) error { |
| 109 |
args := AssociateEipAddressArgs{ |
| 110 |
AllocationId: allocationId, |
| 111 |
InstanceId: instanceId, |
| 112 |
} |
| 113 |
response := ModifyInstanceNetworkSpecResponse{} |
| 114 |
return client.Invoke("AssociateEipAddress", &args, &response) |
| 115 |
} |
| 116 |
|
| 117 |
func (client *Client) NewAssociateEipAddress(args *AssociateEipAddressArgs) error { |
| 118 |
response := ModifyInstanceNetworkSpecResponse{} |
| 119 |
return client.Invoke("AssociateEipAddress", args, &response) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
type EipStatus string |
| 124 |
|
| 125 |
const ( |
| 126 |
EipStatusAssociating = EipStatus("Associating") |
| 127 |
EipStatusUnassociating = EipStatus("Unassociating") |
| 128 |
EipStatusInUse = EipStatus("InUse") |
| 129 |
EipStatusAvailable = EipStatus("Available") |
| 130 |
) |
| 131 |
|
| 132 |
type AssociatedInstanceType string |
| 133 |
|
| 134 |
const ( |
| 135 |
AssociatedInstanceTypeEcsInstance = AssociatedInstanceType("EcsInstance") |
| 136 |
AssociatedInstanceTypeSlbInstance = AssociatedInstanceType("SlbInstance") |
| 137 |
AssociatedInstanceTypeNat = AssociatedInstanceType("Nat") |
| 138 |
AssociatedInstanceTypeHaVip = AssociatedInstanceType("HaVip") |
| 139 |
) |
| 140 |
|
| 141 |
type DescribeEipAddressesArgs struct { |
| 142 |
RegionId common.Region |
| 143 |
Status EipStatus |
| 144 |
EipAddress string |
| 145 |
AllocationId string |
| 146 |
AssociatedInstanceType AssociatedInstanceType |
| 147 |
AssociatedInstanceId string 此处填写ECS实例Id。若InstanceType为SlbInstance,则此处填写VPC类型的私网SLB 的实例ID。若InstanceType为Nat,则此处填写NAT 的实例ID。。若InstanceType为HaVip,则此处填写HaVipId。 |
| 148 |
common.Pagination |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
type EipAddressSetType struct { |
| 154 |
RegionId common.Region |
| 155 |
IpAddress string |
| 156 |
AllocationId string |
| 157 |
Status EipStatus |
| 158 |
InstanceId string |
| 159 |
InstanceType string |
| 160 |
Bandwidth string |
| 161 |
InternetChargeType common.InternetChargeType |
| 162 |
OperationLocks OperationLocksType |
| 163 |
AllocationTime util.ISO6801Time |
| 164 |
} |
| 165 |
|
| 166 |
type DescribeEipAddressesResponse struct { |
| 167 |
common.Response |
| 168 |
common.PaginationResult |
| 169 |
EipAddresses struct { |
| 170 |
EipAddress []EipAddressSetType |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
func (client *Client) DescribeEipAddresses(args *DescribeEipAddressesArgs) (eipAddresses []EipAddressSetType, pagination *common.PaginationResult, err error) { |
| 178 |
response, err := client.DescribeEipAddressesWithRaw(args) |
| 179 |
if err == nil { |
| 180 |
return response.EipAddresses.EipAddress, &response.PaginationResult, nil |
| 181 |
} |
| 182 |
|
| 183 |
return nil, nil, err |
| 184 |
} |
| 185 |
|
| 186 |
func (client *Client) DescribeEipAddressesWithRaw(args *DescribeEipAddressesArgs) (response *DescribeEipAddressesResponse, err error) { |
| 187 |
args.Validate() |
| 188 |
response = &DescribeEipAddressesResponse{} |
| 189 |
|
| 190 |
err = client.Invoke("DescribeEipAddresses", args, response) |
| 191 |
|
| 192 |
if err == nil { |
| 193 |
return response, nil |
| 194 |
} |
| 195 |
|
| 196 |
return nil, err |
| 197 |
} |
| 198 |
|
| 199 |
type ModifyEipAddressAttributeArgs struct { |
| 200 |
AllocationId string |
| 201 |
Bandwidth int |
| 202 |
} |
| 203 |
|
| 204 |
type ModifyEipAddressAttributeResponse struct { |
| 205 |
common.Response |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func (client *Client) ModifyEipAddressAttribute(allocationId string, bandwidth int) error { |
| 212 |
args := ModifyEipAddressAttributeArgs{ |
| 213 |
AllocationId: allocationId, |
| 214 |
Bandwidth: bandwidth, |
| 215 |
} |
| 216 |
response := ModifyEipAddressAttributeResponse{} |
| 217 |
return client.Invoke("ModifyEipAddressAttribute", &args, &response) |
| 218 |
} |
| 219 |
|
| 220 |
type UnallocateEipAddressArgs struct { |
| 221 |
AllocationId string |
| 222 |
InstanceId string |
| 223 |
} |
| 224 |
|
| 225 |
type UnallocateEipAddressResponse struct { |
| 226 |
common.Response |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
func (client *Client) UnassociateEipAddress(allocationId string, instanceId string) error { |
| 233 |
args := UnallocateEipAddressArgs{ |
| 234 |
AllocationId: allocationId, |
| 235 |
InstanceId: instanceId, |
| 236 |
} |
| 237 |
response := UnallocateEipAddressResponse{} |
| 238 |
return client.Invoke("UnassociateEipAddress", &args, &response) |
| 239 |
} |
| 240 |
|
| 241 |
type ReleaseEipAddressArgs struct { |
| 242 |
AllocationId string |
| 243 |
} |
| 244 |
|
| 245 |
type ReleaseEipAddressResponse struct { |
| 246 |
common.Response |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
func (client *Client) ReleaseEipAddress(allocationId string) error { |
| 253 |
args := ReleaseEipAddressArgs{ |
| 254 |
AllocationId: allocationId, |
| 255 |
} |
| 256 |
response := ReleaseEipAddressResponse{} |
| 257 |
return client.Invoke("ReleaseEipAddress", &args, &response) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
func (client *Client) WaitForEip(regionId common.Region, allocationId string, status EipStatus, timeout int) error { |
| 262 |
if timeout <= 0 { |
| 263 |
timeout = DefaultTimeout |
| 264 |
} |
| 265 |
args := DescribeEipAddressesArgs{ |
| 266 |
RegionId: regionId, |
| 267 |
AllocationId: allocationId, |
| 268 |
} |
| 269 |
for { |
| 270 |
eips, _, err := client.DescribeEipAddresses(&args) |
| 271 |
if err != nil { |
| 272 |
return err |
| 273 |
} |
| 274 |
if len(eips) == 0 { |
| 275 |
return common.GetClientErrorFromString("Not found") |
| 276 |
} |
| 277 |
if eips[0].Status == status { |
| 278 |
break |
| 279 |
} |
| 280 |
timeout = timeout - DefaultWaitForInterval |
| 281 |
if timeout <= 0 { |
| 282 |
return common.GetClientErrorFromString("Timeout") |
| 283 |
} |
| 284 |
time.Sleep(DefaultWaitForInterval * time.Second) |
| 285 |
} |
| 286 |
return nil |
| 287 |
} |