| 1 |
package slb |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/denverdino/aliyungo/common" |
| 8 |
"github.com/denverdino/aliyungo/util" |
| 9 |
) |
| 10 |
|
| 11 |
type AddressType string |
| 12 |
|
| 13 |
const ( |
| 14 |
InternetAddressType = AddressType("internet") |
| 15 |
IntranetAddressType = AddressType("intranet") |
| 16 |
) |
| 17 |
|
| 18 |
type InternetChargeType string |
| 19 |
|
| 20 |
const ( |
| 21 |
PayByBandwidth = InternetChargeType("paybybandwidth") |
| 22 |
PayByTraffic = InternetChargeType("paybytraffic") |
| 23 |
) |
| 24 |
|
| 25 |
type LoadBalancerSpecType string |
| 26 |
|
| 27 |
const ( |
| 28 |
S1Small = "slb.s1.small" |
| 29 |
S2Small = "slb.s2.small" |
| 30 |
S2Medium = "slb.s2.medium" |
| 31 |
S3Small = "slb.s3.small" |
| 32 |
S3Medium = "slb.s3.medium" |
| 33 |
S3Large = "slb.s3.large" |
| 34 |
) |
| 35 |
|
| 36 |
type CreateLoadBalancerArgs struct { |
| 37 |
RegionId common.Region |
| 38 |
LoadBalancerName string |
| 39 |
AddressType AddressType |
| 40 |
VSwitchId string |
| 41 |
InternetChargeType InternetChargeType |
| 42 |
Bandwidth int |
| 43 |
ClientToken string |
| 44 |
MasterZoneId string |
| 45 |
SlaveZoneId string |
| 46 |
LoadBalancerSpec LoadBalancerSpecType |
| 47 |
} |
| 48 |
|
| 49 |
type CreateLoadBalancerResponse struct { |
| 50 |
common.Response |
| 51 |
LoadBalancerId string |
| 52 |
Address string |
| 53 |
NetworkType string |
| 54 |
VpcId string |
| 55 |
VSwitchId string |
| 56 |
LoadBalancerName string |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
func (client *Client) CreateLoadBalancer(args *CreateLoadBalancerArgs) (response *CreateLoadBalancerResponse, err error) { |
| 63 |
response = &CreateLoadBalancerResponse{} |
| 64 |
err = client.Invoke("CreateLoadBalancer", args, response) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
return response, err |
| 69 |
} |
| 70 |
|
| 71 |
type DeleteLoadBalancerArgs struct { |
| 72 |
LoadBalancerId string |
| 73 |
} |
| 74 |
|
| 75 |
type DeleteLoadBalancerResponse struct { |
| 76 |
common.Response |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func (client *Client) DeleteLoadBalancer(loadBalancerId string) (err error) { |
| 83 |
args := &DeleteLoadBalancerArgs{ |
| 84 |
LoadBalancerId: loadBalancerId, |
| 85 |
} |
| 86 |
response := &DeleteLoadBalancerResponse{} |
| 87 |
err = client.Invoke("DeleteLoadBalancer", args, response) |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
type ModifyLoadBalancerInternetSpecArgs struct { |
| 92 |
LoadBalancerId string |
| 93 |
InternetChargeType InternetChargeType |
| 94 |
Bandwidth int |
| 95 |
} |
| 96 |
|
| 97 |
type ModifyLoadBalancerInternetSpecResponse struct { |
| 98 |
common.Response |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
func (client *Client) ModifyLoadBalancerInternetSpec(args *ModifyLoadBalancerInternetSpecArgs) (err error) { |
| 106 |
response := &ModifyLoadBalancerInternetSpecResponse{} |
| 107 |
err = client.Invoke("ModifyLoadBalancerInternetSpec", args, response) |
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
type ModifyLoadBalancerInstanceSpecArgs struct { |
| 112 |
RegionId common.Region |
| 113 |
LoadBalancerId string |
| 114 |
LoadBalancerSpec LoadBalancerSpecType |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func (client *Client) ModifyLoadBalancerInstanceSpec(args *ModifyLoadBalancerInstanceSpecArgs) (err error) { |
| 122 |
response := &common.Response{} |
| 123 |
err = client.Invoke("ModifyLoadBalancerInstanceSpec", args, response) |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
type Status string |
| 128 |
|
| 129 |
const InactiveStatus = Status("inactive") |
| 130 |
const ActiveStatus = Status("active") |
| 131 |
const LockedStatus = Status("locked") |
| 132 |
|
| 133 |
type SetLoadBalancerStatusArgs struct { |
| 134 |
LoadBalancerId string |
| 135 |
LoadBalancerStatus Status |
| 136 |
} |
| 137 |
|
| 138 |
type SetLoadBalancerStatusResponse struct { |
| 139 |
common.Response |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
func (client *Client) SetLoadBalancerStatus(loadBalancerId string, status Status) (err error) { |
| 147 |
args := &SetLoadBalancerStatusArgs{ |
| 148 |
LoadBalancerId: loadBalancerId, |
| 149 |
LoadBalancerStatus: status, |
| 150 |
} |
| 151 |
response := &SetLoadBalancerStatusResponse{} |
| 152 |
err = client.Invoke("SetLoadBalancerStatus", args, response) |
| 153 |
return err |
| 154 |
} |
| 155 |
|
| 156 |
type SetLoadBalancerNameArgs struct { |
| 157 |
LoadBalancerId string |
| 158 |
LoadBalancerName string |
| 159 |
} |
| 160 |
|
| 161 |
type SetLoadBalancerNameResponse struct { |
| 162 |
common.Response |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
func (client *Client) SetLoadBalancerName(loadBalancerId string, name string) (err error) { |
| 170 |
args := &SetLoadBalancerNameArgs{ |
| 171 |
LoadBalancerId: loadBalancerId, |
| 172 |
LoadBalancerName: name, |
| 173 |
} |
| 174 |
response := &SetLoadBalancerNameResponse{} |
| 175 |
err = client.Invoke("SetLoadBalancerName", args, response) |
| 176 |
return err |
| 177 |
} |
| 178 |
|
| 179 |
type DescribeLoadBalancersArgs struct { |
| 180 |
RegionId common.Region |
| 181 |
LoadBalancerId string |
| 182 |
LoadBalancerName string |
| 183 |
AddressType AddressType |
| 184 |
NetworkType string |
| 185 |
VpcId string |
| 186 |
VSwitchId string |
| 187 |
Address string |
| 188 |
InternetChargeType InternetChargeType |
| 189 |
ServerId string |
| 190 |
Tags string |
| 191 |
} |
| 192 |
|
| 193 |
type ListenerPortAndProtocolType struct { |
| 194 |
ListenerPort int |
| 195 |
ListenerProtocol string |
| 196 |
} |
| 197 |
|
| 198 |
type BackendServerType struct { |
| 199 |
ServerId string |
| 200 |
Weight int |
| 201 |
Type string |
| 202 |
} |
| 203 |
|
| 204 |
type LoadBalancerType struct { |
| 205 |
LoadBalancerId string |
| 206 |
LoadBalancerName string |
| 207 |
LoadBalancerStatus string |
| 208 |
Address string |
| 209 |
RegionId common.Region |
| 210 |
RegionIdAlias string |
| 211 |
AddressType AddressType |
| 212 |
VSwitchId string |
| 213 |
VpcId string |
| 214 |
NetworkType string |
| 215 |
Bandwidth int |
| 216 |
InternetChargeType InternetChargeType |
| 217 |
CreateTime string |
| 218 |
CreateTimeStamp util.ISO6801Time |
| 219 |
ListenerPorts struct { |
| 220 |
ListenerPort []int |
| 221 |
} |
| 222 |
ListenerPortsAndProtocol struct { |
| 223 |
ListenerPortAndProtocol []ListenerPortAndProtocolType |
| 224 |
} |
| 225 |
BackendServers struct { |
| 226 |
BackendServer []BackendServerType |
| 227 |
} |
| 228 |
LoadBalancerSpec LoadBalancerSpecType |
| 229 |
} |
| 230 |
|
| 231 |
type DescribeLoadBalancersResponse struct { |
| 232 |
common.Response |
| 233 |
LoadBalancers struct { |
| 234 |
LoadBalancer []LoadBalancerType |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
func (client *Client) DescribeLoadBalancers(args *DescribeLoadBalancersArgs) (loadBalancers []LoadBalancerType, err error) { |
| 243 |
response := &DescribeLoadBalancersResponse{} |
| 244 |
err = client.Invoke("DescribeLoadBalancers", args, response) |
| 245 |
if err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
return response.LoadBalancers.LoadBalancer, err |
| 249 |
} |
| 250 |
|
| 251 |
type DescribeLoadBalancerAttributeArgs struct { |
| 252 |
LoadBalancerId string |
| 253 |
} |
| 254 |
|
| 255 |
type DescribeLoadBalancerAttributeResponse struct { |
| 256 |
common.Response |
| 257 |
LoadBalancerType |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
func (client *Client) DescribeLoadBalancerAttribute(loadBalancerId string) (loadBalancer *LoadBalancerType, err error) { |
| 265 |
args := &DescribeLoadBalancersArgs{ |
| 266 |
LoadBalancerId: loadBalancerId, |
| 267 |
} |
| 268 |
response := &DescribeLoadBalancerAttributeResponse{} |
| 269 |
err = client.Invoke("DescribeLoadBalancerAttribute", args, response) |
| 270 |
if err != nil { |
| 271 |
return nil, err |
| 272 |
} |
| 273 |
return &response.LoadBalancerType, err |
| 274 |
} |
| 275 |
|
| 276 |
type NewDescribeLoadBalancerAttributeArgs struct { |
| 277 |
LoadBalancerId string |
| 278 |
RegionId common.Region |
| 279 |
MasterZoneId string |
| 280 |
SlaveZoneId string |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
func (client *Client) NewDescribeLoadBalancerAttribute(args *NewDescribeLoadBalancerAttributeArgs) (loadBalancer *LoadBalancerType, err error) { |
| 288 |
|
| 289 |
response := &DescribeLoadBalancerAttributeResponse{} |
| 290 |
err = client.Invoke("DescribeLoadBalancerAttribute", args, response) |
| 291 |
if err != nil { |
| 292 |
return nil, err |
| 293 |
} |
| 294 |
return &response.LoadBalancerType, err |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func (client *Client) WaitForLoadBalancerAsyn(loadBalancerId string, status Status, timeout int) error { |
| 299 |
if timeout <= 0 { |
| 300 |
timeout = DefaultTimeout |
| 301 |
} |
| 302 |
|
| 303 |
for { |
| 304 |
lb, err := client.DescribeLoadBalancerAttribute(loadBalancerId) |
| 305 |
|
| 306 |
e, _ := err.(*common.Error) |
| 307 |
if e != nil { |
| 308 |
if e.StatusCode == 404 || e.Code == "InvalidLoadBalancerId.NotFound" { |
| 309 |
continue |
| 310 |
} |
| 311 |
return err |
| 312 |
} else if lb != nil && Status(lb.LoadBalancerStatus) == status { |
| 313 |
|
| 314 |
break |
| 315 |
} |
| 316 |
timeout = timeout - DefaultWaitForInterval |
| 317 |
if timeout <= 0 { |
| 318 |
return common.GetClientErrorFromString(fmt.Sprintf("Timeout waitting for load balacner %#v", status)) |
| 319 |
} |
| 320 |
time.Sleep(DefaultWaitForInterval * time.Second) |
| 321 |
} |
| 322 |
return nil |
| 323 |
} |