| 1 |
package ess |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
|
| 6 |
"github.com/denverdino/aliyungo/common" |
| 7 |
) |
| 8 |
|
| 9 |
type LifecycleState string |
| 10 |
|
| 11 |
const ( |
| 12 |
Active = LifecycleState("Active") |
| 13 |
Inacitve = LifecycleState("Inactive") |
| 14 |
Deleting = LifecycleState("Deleting") |
| 15 |
InService = LifecycleState("InService") |
| 16 |
Pending = LifecycleState("Pending") |
| 17 |
Removing = LifecycleState("Removing") |
| 18 |
) |
| 19 |
|
| 20 |
type CreateScalingGroupArgs struct { |
| 21 |
RegionId common.Region |
| 22 |
ScalingGroupName string |
| 23 |
LoadBalancerIds string |
| 24 |
VpcId string |
| 25 |
VSwitchId string |
| 26 |
VSwitchIds common.FlattenArray |
| 27 |
|
| 28 |
MinSize *int |
| 29 |
MaxSize *int |
| 30 |
DefaultCooldown *int |
| 31 |
RemovalPolicy common.FlattenArray |
| 32 |
DBInstanceIds string |
| 33 |
} |
| 34 |
|
| 35 |
type CreateScalingGroupResponse struct { |
| 36 |
common.Response |
| 37 |
ScalingGroupId string |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func (client *Client) CreateScalingGroup(args *CreateScalingGroupArgs) (resp *CreateScalingGroupResponse, err error) { |
| 44 |
response := CreateScalingGroupResponse{} |
| 45 |
err = client.InvokeByFlattenMethod("CreateScalingGroup", args, &response) |
| 46 |
|
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
return &response, nil |
| 51 |
} |
| 52 |
|
| 53 |
type ModifyScalingGroupArgs struct { |
| 54 |
ScalingGroupId string |
| 55 |
ScalingGroupName string |
| 56 |
ActiveScalingConfigurationId string |
| 57 |
|
| 58 |
MinSize *int |
| 59 |
MaxSize *int |
| 60 |
DefaultCooldown *int |
| 61 |
RemovalPolicy common.FlattenArray |
| 62 |
} |
| 63 |
|
| 64 |
type ModifyScalingGroupResponse struct { |
| 65 |
common.Response |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func (client *Client) ModifyScalingGroup(args *ModifyScalingGroupArgs) (resp *ModifyScalingGroupResponse, err error) { |
| 72 |
response := ModifyScalingGroupResponse{} |
| 73 |
err = client.InvokeByFlattenMethod("ModifyScalingGroup", args, &response) |
| 74 |
|
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
return &response, nil |
| 79 |
} |
| 80 |
|
| 81 |
type DescribeScalingGroupsArgs struct { |
| 82 |
RegionId common.Region |
| 83 |
ScalingGroupId common.FlattenArray |
| 84 |
ScalingGroupName common.FlattenArray |
| 85 |
common.Pagination |
| 86 |
} |
| 87 |
|
| 88 |
type DescribeInstancesResponse struct { |
| 89 |
common.Response |
| 90 |
common.PaginationResult |
| 91 |
ScalingGroups struct { |
| 92 |
ScalingGroup []ScalingGroupItemType |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
type ScalingGroupItemType struct { |
| 97 |
ScalingGroupId string |
| 98 |
ScalingGroupName string |
| 99 |
ActiveScalingConfigurationId string |
| 100 |
RegionId string |
| 101 |
LoadBalancerId string |
| 102 |
VSwitchId string |
| 103 |
VSwitchIds VSwitchIdsSetType |
| 104 |
CreationTime string |
| 105 |
LifecycleState LifecycleState |
| 106 |
MinSize int |
| 107 |
MaxSize int |
| 108 |
DefaultCooldown int |
| 109 |
TotalCapacity int |
| 110 |
ActiveCapacity int |
| 111 |
PendingCapacity int |
| 112 |
RemovingCapacity int |
| 113 |
RemovalPolicies RemovalPolicySetType |
| 114 |
DBInstanceIds DBInstanceIdSetType |
| 115 |
LoadBalancerIds LoadBalancerIdSetType |
| 116 |
} |
| 117 |
|
| 118 |
type VSwitchIdsSetType struct { |
| 119 |
VSwitchId []string |
| 120 |
} |
| 121 |
|
| 122 |
type RemovalPolicySetType struct { |
| 123 |
RemovalPolicy []string |
| 124 |
} |
| 125 |
|
| 126 |
type DBInstanceIdSetType struct { |
| 127 |
DBInstanceId []string |
| 128 |
} |
| 129 |
type LoadBalancerIdSetType struct { |
| 130 |
LoadBalancerId []string |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
func (client *Client) DescribeScalingGroups(args *DescribeScalingGroupsArgs) (groups []ScalingGroupItemType, pagination *common.PaginationResult, err error) { |
| 137 |
args.Validate() |
| 138 |
response := DescribeInstancesResponse{} |
| 139 |
|
| 140 |
err = client.InvokeByFlattenMethod("DescribeScalingGroups", args, &response) |
| 141 |
|
| 142 |
if err == nil { |
| 143 |
return response.ScalingGroups.ScalingGroup, &response.PaginationResult, nil |
| 144 |
} |
| 145 |
|
| 146 |
return nil, nil, err |
| 147 |
} |
| 148 |
|
| 149 |
type DescribeScalingInstancesArgs struct { |
| 150 |
RegionId common.Region |
| 151 |
ScalingGroupId string |
| 152 |
ScalingConfigurationId string |
| 153 |
HealthStatus string |
| 154 |
CreationType string |
| 155 |
LifecycleState LifecycleState |
| 156 |
InstanceId common.FlattenArray |
| 157 |
common.Pagination |
| 158 |
} |
| 159 |
|
| 160 |
type DescribeScalingInstancesResponse struct { |
| 161 |
common.Response |
| 162 |
common.PaginationResult |
| 163 |
ScalingInstances struct { |
| 164 |
ScalingInstance []ScalingInstanceItemType |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
type ScalingInstanceItemType struct { |
| 169 |
InstanceId string |
| 170 |
ScalingGroupId string |
| 171 |
ScalingConfigurationId string |
| 172 |
HealthStatus string |
| 173 |
CreationTime string |
| 174 |
CreationType string |
| 175 |
LifecycleState LifecycleState |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
func (client *Client) DescribeScalingInstances(args *DescribeScalingInstancesArgs) (instances []ScalingInstanceItemType, pagination *common.PaginationResult, err error) { |
| 182 |
args.Validate() |
| 183 |
response := DescribeScalingInstancesResponse{} |
| 184 |
|
| 185 |
err = client.InvokeByFlattenMethod("DescribeScalingInstances", args, &response) |
| 186 |
|
| 187 |
if err == nil { |
| 188 |
return response.ScalingInstances.ScalingInstance, &response.PaginationResult, nil |
| 189 |
} |
| 190 |
|
| 191 |
return nil, nil, err |
| 192 |
} |
| 193 |
|
| 194 |
type EnableScalingGroupArgs struct { |
| 195 |
ScalingGroupId string |
| 196 |
ActiveScalingConfigurationId string |
| 197 |
InstanceId common.FlattenArray |
| 198 |
} |
| 199 |
|
| 200 |
type EnableScalingGroupResponse struct { |
| 201 |
common.Response |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
func (client *Client) EnableScalingGroup(args *EnableScalingGroupArgs) (resp *EnableScalingGroupResponse, err error) { |
| 208 |
response := EnableScalingGroupResponse{} |
| 209 |
err = client.InvokeByFlattenMethod("EnableScalingGroup", args, &response) |
| 210 |
|
| 211 |
if err != nil { |
| 212 |
return nil, err |
| 213 |
} |
| 214 |
return &response, nil |
| 215 |
} |
| 216 |
|
| 217 |
type DisableScalingGroupArgs struct { |
| 218 |
ScalingGroupId string |
| 219 |
} |
| 220 |
|
| 221 |
type DisableScalingGroupResponse struct { |
| 222 |
common.Response |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
func (client *Client) DisableScalingGroup(args *DisableScalingGroupArgs) (resp *DisableScalingGroupResponse, err error) { |
| 229 |
response := DisableScalingGroupResponse{} |
| 230 |
err = client.InvokeByFlattenMethod("DisableScalingGroup", args, &response) |
| 231 |
|
| 232 |
if err != nil { |
| 233 |
return nil, err |
| 234 |
} |
| 235 |
return &response, nil |
| 236 |
} |
| 237 |
|
| 238 |
type DeleteScalingGroupArgs struct { |
| 239 |
ScalingGroupId string |
| 240 |
ForceDelete bool |
| 241 |
} |
| 242 |
|
| 243 |
type DeleteScalingGroupResponse struct { |
| 244 |
common.Response |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
func (client *Client) DeleteScalingGroup(args *DeleteScalingGroupArgs) (resp *DeleteScalingGroupResponse, err error) { |
| 251 |
response := DeleteScalingGroupResponse{} |
| 252 |
err = client.InvokeByFlattenMethod("DeleteScalingGroup", args, &response) |
| 253 |
|
| 254 |
if err != nil { |
| 255 |
return nil, err |
| 256 |
} |
| 257 |
return &response, nil |
| 258 |
} |
| 259 |
|
| 260 |
type AttachInstancesArgs struct { |
| 261 |
ScalingGroupId string |
| 262 |
InstanceId common.FlattenArray |
| 263 |
} |
| 264 |
|
| 265 |
type AttachInstancesResponse struct { |
| 266 |
common.Response |
| 267 |
ScalingActivityId string |
| 268 |
} |
| 269 |
|
| 270 |
type RemoveInstancesArgs struct { |
| 271 |
ScalingGroupId string |
| 272 |
InstanceId common.FlattenArray |
| 273 |
} |
| 274 |
|
| 275 |
type RemoveInstancesResponse struct { |
| 276 |
common.Response |
| 277 |
ScalingActivityId string |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
func (client *Client) AttachInstances(args *AttachInstancesArgs) (resp *AttachInstancesResponse, err error) { |
| 284 |
response := AttachInstancesResponse{} |
| 285 |
err = client.InvokeByFlattenMethod("AttachInstances", args, &response) |
| 286 |
|
| 287 |
if err != nil { |
| 288 |
return nil, err |
| 289 |
} |
| 290 |
return &response, nil |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
func (client *Client) RemoveInstances(args *RemoveInstancesArgs) (resp *RemoveInstancesResponse, err error) { |
| 297 |
response := RemoveInstancesResponse{} |
| 298 |
err = client.InvokeByFlattenMethod("RemoveInstances", args, &response) |
| 299 |
|
| 300 |
if err != nil { |
| 301 |
return nil, err |
| 302 |
} |
| 303 |
return &response, nil |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
const DefaultWaitTimeout = 120 |
| 308 |
const DefaultWaitForInterval = 5 |
| 309 |
|
| 310 |
|
| 311 |
func (client *Client) WaitForScalingGroup(regionId common.Region, groupId string, status LifecycleState, timeout int) error { |
| 312 |
if timeout <= 0 { |
| 313 |
timeout = DefaultWaitTimeout |
| 314 |
} |
| 315 |
for { |
| 316 |
sgs, _, err := client.DescribeScalingGroups(&DescribeScalingGroupsArgs{ |
| 317 |
RegionId: regionId, |
| 318 |
ScalingGroupId: []string{groupId}, |
| 319 |
}) |
| 320 |
if err != nil { |
| 321 |
return err |
| 322 |
} |
| 323 |
|
| 324 |
if timeout <= 0 { |
| 325 |
return common.GetClientErrorFromString("Timeout") |
| 326 |
} |
| 327 |
|
| 328 |
timeout = timeout - DefaultWaitForInterval |
| 329 |
time.Sleep(DefaultWaitForInterval * time.Second) |
| 330 |
|
| 331 |
if len(sgs) < 1 { |
| 332 |
return common.GetClientErrorFromString("Not found") |
| 333 |
} |
| 334 |
if sgs[0].LifecycleState == status { |
| 335 |
break |
| 336 |
} |
| 337 |
|
| 338 |
} |
| 339 |
return nil |
| 340 |
} |