forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_autoscaling_attachment.go
157 lines (128 loc) · 4.48 KB
/
resource_aws_autoscaling_attachment.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsAutoscalingAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAutoscalingAttachmentCreate,
Read: resourceAwsAutoscalingAttachmentRead,
Delete: resourceAwsAutoscalingAttachmentDelete,
Schema: map[string]*schema.Schema{
"autoscaling_group_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"elb": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
"alb_target_group_arn": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
},
}
}
func resourceAwsAutoscalingAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
asgconn := meta.(*AWSClient).autoscalingconn
asgName := d.Get("autoscaling_group_name").(string)
if v, ok := d.GetOk("elb"); ok {
attachOpts := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: aws.String(asgName),
LoadBalancerNames: []*string{aws.String(v.(string))},
}
log.Printf("[INFO] registering asg %s with ELBs %s", asgName, v.(string))
if _, err := asgconn.AttachLoadBalancers(attachOpts); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, v.(string)), err)
}
}
if v, ok := d.GetOk("alb_target_group_arn"); ok {
attachOpts := &autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(asgName),
TargetGroupARNs: []*string{aws.String(v.(string))},
}
log.Printf("[INFO] registering asg %s with ALB Target Group %s", asgName, v.(string))
if _, err := asgconn.AttachLoadBalancerTargetGroups(attachOpts); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with ALB Target Group: %s: {{err}}", asgName, v.(string)), err)
}
}
d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", asgName)))
return resourceAwsAutoscalingAttachmentRead(d, meta)
}
func resourceAwsAutoscalingAttachmentRead(d *schema.ResourceData, meta interface{}) error {
asgconn := meta.(*AWSClient).autoscalingconn
asgName := d.Get("autoscaling_group_name").(string)
// Retrieve the ASG properites to get list of associated ELBs
asg, err := getAwsAutoscalingGroup(asgName, asgconn)
if err != nil {
return err
}
if asg == nil {
log.Printf("[INFO] Autoscaling Group %q not found", asgName)
d.SetId("")
return nil
}
if v, ok := d.GetOk("elb"); ok {
found := false
for _, i := range asg.LoadBalancerNames {
if v.(string) == *i {
d.Set("elb", v.(string))
found = true
break
}
}
if !found {
log.Printf("[WARN] Association for %s was not found in ASG assocation", v.(string))
d.SetId("")
}
}
if v, ok := d.GetOk("alb_target_group_arn"); ok {
found := false
for _, i := range asg.TargetGroupARNs {
if v.(string) == *i {
d.Set("alb_target_group_arn", v.(string))
found = true
break
}
}
if !found {
log.Printf("[WARN] Association for %s was not found in ASG assocation", v.(string))
d.SetId("")
}
}
return nil
}
func resourceAwsAutoscalingAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
asgconn := meta.(*AWSClient).autoscalingconn
asgName := d.Get("autoscaling_group_name").(string)
if v, ok := d.GetOk("elb"); ok {
detachOpts := &autoscaling.DetachLoadBalancersInput{
AutoScalingGroupName: aws.String(asgName),
LoadBalancerNames: []*string{aws.String(v.(string))},
}
log.Printf("[INFO] Deleting ELB %s association from: %s", v.(string), asgName)
if _, err := asgconn.DetachLoadBalancers(detachOpts); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, v.(string)), err)
}
}
if v, ok := d.GetOk("alb_target_group_arn"); ok {
detachOpts := &autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(asgName),
TargetGroupARNs: []*string{aws.String(v.(string))},
}
log.Printf("[INFO] Deleting ALB Target Group %s association from: %s", v.(string), asgName)
if _, err := asgconn.DetachLoadBalancerTargetGroups(detachOpts); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with ALB Target Group: %s: {{err}}", asgName, v.(string)), err)
}
}
return nil
}