/
autoscaling_attach.go
64 lines (51 loc) · 1.72 KB
/
autoscaling_attach.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
package myaws
import (
"fmt"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/pkg/errors"
)
// AutoscalingAttachOptions customize the behavior of the Attach command.
type AutoscalingAttachOptions struct {
AsgName string
InstanceIds []*string
LoadBalancerNames []*string
Wait bool
}
// AutoscalingAttach attaches instances or load balancers from autoscaling group.
func (client *Client) AutoscalingAttach(options AutoscalingAttachOptions) error {
if len(options.InstanceIds) > 0 {
if err := client.autoscalingAttachInstances(options.AsgName, options.InstanceIds); err != nil {
return err
}
}
if len(options.LoadBalancerNames) > 0 {
if err := client.autoscalingAttachLoadBalancers(options.AsgName, options.LoadBalancerNames); err != nil {
return err
}
}
if options.Wait {
fmt.Fprintln(client.stdout, "Wait until desired capacity instances are InService...")
return client.WaitUntilAutoScalingGroupStable(options.AsgName)
}
return nil
}
func (client *Client) autoscalingAttachInstances(asgName string, instanceIds []*string) error {
params := &autoscaling.AttachInstancesInput{
AutoScalingGroupName: &asgName,
InstanceIds: instanceIds,
}
if _, err := client.AutoScaling.AttachInstances(params); err != nil {
return errors.Wrap(err, "AttachInstances failed:")
}
return nil
}
func (client *Client) autoscalingAttachLoadBalancers(asgName string, loadBalancerNames []*string) error {
params := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: &asgName,
LoadBalancerNames: loadBalancerNames,
}
if _, err := client.AutoScaling.AttachLoadBalancers(params); err != nil {
return errors.Wrap(err, "AttachLoadBalancers failed:")
}
return nil
}