forked from hashicorp/terraform
/
resource_aws_autoscaling_attachment.go
108 lines (86 loc) · 2.94 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
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": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"elb": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
},
}
}
func resourceAwsAutoscalingAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
asgconn := meta.(*AWSClient).autoscalingconn
asgName := d.Get("autoscaling_group_name").(string)
elbName := d.Get("elb").(string)
attachElbInput := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: aws.String(asgName),
LoadBalancerNames: []*string{aws.String(elbName)},
}
log.Printf("[INFO] registering asg %s with ELBs %s", asgName, elbName)
if _, err := asgconn.AttachLoadBalancers(attachElbInput); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, elbName), 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)
elbName := d.Get("elb").(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
}
found := false
for _, i := range asg.LoadBalancerNames {
if elbName == *i {
d.Set("elb", elbName)
found = true
break
}
}
if !found {
log.Printf("[WARN] Association for %s was not found in ASG assocation", elbName)
d.SetId("")
}
return nil
}
func resourceAwsAutoscalingAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
asgconn := meta.(*AWSClient).autoscalingconn
asgName := d.Get("autoscaling_group_name").(string)
elbName := d.Get("elb").(string)
log.Printf("[INFO] Deleting ELB %s association from: %s", elbName, asgName)
detachOpts := &autoscaling.DetachLoadBalancersInput{
AutoScalingGroupName: aws.String(asgName),
LoadBalancerNames: []*string{aws.String(elbName)},
}
if _, err := asgconn.DetachLoadBalancers(detachOpts); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, elbName), err)
}
return nil
}