forked from hashicorp/terraform
/
resource_aws_eip.go
197 lines (159 loc) · 4.61 KB
/
resource_aws_eip.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package aws
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
//"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
)
func resourceAwsEip() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEipCreate,
Read: resourceAwsEipRead,
Update: resourceAwsEipUpdate,
Delete: resourceAwsEipDelete,
Schema: map[string]*schema.Schema{
"vpc": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"instance": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"allocation_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"domain": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"public_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"private_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
// By default, we're not in a VPC
domainOpt := ""
if v := d.Get("vpc"); v != nil && v.(bool) {
domainOpt = "vpc"
}
allocOpts := ec2.AllocateAddress{
Domain: domainOpt,
}
log.Printf("[DEBUG] EIP create configuration: %#v", allocOpts)
allocResp, err := ec2conn.AllocateAddress(&allocOpts)
if err != nil {
return fmt.Errorf("Error creating EIP: %s", err)
}
// The domain tells us if we're in a VPC or not
d.Set("domain", allocResp.Domain)
// Assign the eips (unique) allocation id for use later
// the EIP api has a conditional unique ID (really), so
// if we're in a VPC we need to save the ID as such, otherwise
// it defaults to using the public IP
log.Printf("[DEBUG] EIP Allocate: %#v", allocResp)
if d.Get("domain").(string) == "vpc" {
d.SetId(allocResp.AllocationId)
} else {
d.SetId(allocResp.PublicIp)
}
log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), allocResp.Domain)
return resourceAwsEipUpdate(d, meta)
}
func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
domain := resourceAwsEipDomain(d)
// Only register with an instance if we have one
if v := d.Get("instance"); v != nil {
instanceId := v.(string)
assocOpts := ec2.AssociateAddress{
InstanceId: instanceId,
PublicIp: d.Id(),
}
// more unique ID conditionals
if domain == "vpc" {
assocOpts = ec2.AssociateAddress{
InstanceId: instanceId,
AllocationId: d.Id(),
PublicIp: "",
}
}
log.Printf("[DEBUG] EIP associate configuration: %#v (domain: %v)", assocOpts, domain)
_, err := ec2conn.AssociateAddress(&assocOpts)
if err != nil {
return fmt.Errorf("Failure associating instances: %s", err)
}
}
return resourceAwsEipRead(d, meta)
}
func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
domain := resourceAwsEipDomain(d)
var err error
if domain == "vpc" {
log.Printf("[DEBUG] EIP release (destroy) address allocation: %v", d.Id())
_, err = ec2conn.ReleaseAddress(d.Id())
return err
}
log.Printf("[DEBUG] EIP release (destroy) address: %v", d.Id())
_, err = ec2conn.ReleasePublicAddress(d.Id())
return err
}
func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
domain := resourceAwsEipDomain(d)
id := d.Id()
assocIds := []string{}
publicIps := []string{}
if domain == "vpc" {
assocIds = []string{id}
} else {
publicIps = []string{id}
}
log.Printf(
"[DEBUG] EIP describe configuration: %#v, %#v (domain: %s)",
assocIds, publicIps, domain)
describeAddresses, err := ec2conn.Addresses(publicIps, assocIds, nil)
if err != nil {
return fmt.Errorf("Error retrieving EIP: %s", err)
}
// Verify AWS returned our EIP
if len(describeAddresses.Addresses) != 1 ||
describeAddresses.Addresses[0].AllocationId != id ||
describeAddresses.Addresses[0].PublicIp != id {
if err != nil {
return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses)
}
}
address := describeAddresses.Addresses[0]
d.Set("instance", address.InstanceId)
d.Set("public_ip", address.PublicIp)
d.Set("private_ip", address.PrivateIpAddress)
return nil
}
func resourceAwsEipDomain(d *schema.ResourceData) string {
if v := d.Get("domain"); v != nil {
return v.(string)
} else if strings.Contains(d.Id(), "eipalloc") {
// We have to do this for backwards compatibility since TF 0.1
// didn't have the "domain" computed attribute.
return "vpc"
}
return "standard"
}