forked from hashicorp/terraform-provider-aws
/
resource_aws_elb_attachment.go
135 lines (106 loc) · 3.44 KB
/
resource_aws_elb_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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsElbAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsElbAttachmentCreate,
Read: resourceAwsElbAttachmentRead,
Delete: resourceAwsElbAttachmentDelete,
Schema: map[string]*schema.Schema{
"elb": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"instance": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
},
}
}
func resourceAwsElbAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)
instance := d.Get("instance").(string)
registerInstancesOpts := elb.RegisterInstancesWithLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Instances: []*elb.Instance{{InstanceId: aws.String(instance)}},
}
log.Printf("[INFO] registering instance %s with ELB %s", instance, elbName)
err := resource.Retry(10*time.Minute, func() *resource.RetryError {
_, err := elbconn.RegisterInstancesWithLoadBalancer(®isterInstancesOpts)
if isAWSErr(err, "InvalidTarget", "") {
return resource.RetryableError(fmt.Errorf("Error attaching instance to ELB, retrying: %s", err))
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Failure registering instances with ELB: %s", err)
}
d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", elbName)))
return nil
}
func resourceAwsElbAttachmentRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)
// only add the instance that was previously defined for this resource
expected := d.Get("instance").(string)
// Retrieve the ELB properties to get a list of attachments
describeElbOpts := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{aws.String(elbName)},
}
resp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
if err != nil {
if isLoadBalancerNotFound(err) {
log.Printf("[ERROR] ELB %s not found", elbName)
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving ELB: %s", err)
}
if len(resp.LoadBalancerDescriptions) != 1 {
log.Printf("[ERROR] Unable to find ELB: %s", resp.LoadBalancerDescriptions)
d.SetId("")
return nil
}
// only set the instance Id that this resource manages
found := false
for _, i := range resp.LoadBalancerDescriptions[0].Instances {
if expected == *i.InstanceId {
d.Set("instance", expected)
found = true
}
}
if !found {
log.Printf("[WARN] instance %s not found in elb attachments", expected)
d.SetId("")
}
return nil
}
func resourceAwsElbAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbName := d.Get("elb").(string)
instance := d.Get("instance").(string)
log.Printf("[INFO] Deleting Attachment %s from: %s", instance, elbName)
deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancerInput{
LoadBalancerName: aws.String(elbName),
Instances: []*elb.Instance{{InstanceId: aws.String(instance)}},
}
_, err := elbconn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts)
if err != nil {
return fmt.Errorf("Failure deregistering instances from ELB: %s", err)
}
return nil
}