forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_eip_association.go
161 lines (134 loc) · 3.87 KB
/
resource_aws_eip_association.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsEipAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEipAssociationCreate,
Read: resourceAwsEipAssociationRead,
Delete: resourceAwsEipAssociationDelete,
Schema: map[string]*schema.Schema{
"allocation_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"allow_reassociation": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"instance_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"network_interface_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"private_ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"public_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceAwsEipAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.AssociateAddressInput{}
if v, ok := d.GetOk("allocation_id"); ok {
request.AllocationId = aws.String(v.(string))
}
if v, ok := d.GetOk("allow_reassociation"); ok {
request.AllowReassociation = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("instance_id"); ok {
request.InstanceId = aws.String(v.(string))
}
if v, ok := d.GetOk("network_interface_id"); ok {
request.NetworkInterfaceId = aws.String(v.(string))
}
if v, ok := d.GetOk("private_ip_address"); ok {
request.PrivateIpAddress = aws.String(v.(string))
}
if v, ok := d.GetOk("public_ip"); ok {
request.PublicIp = aws.String(v.(string))
}
log.Printf("[DEBUG] EIP association configuration: %#v", request)
resp, err := conn.AssociateAddress(request)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
return fmt.Errorf("[WARN] Error attaching EIP, message: \"%s\", code: \"%s\"",
awsErr.Message(), awsErr.Code())
}
return err
}
d.SetId(*resp.AssociationId)
return resourceAwsEipAssociationRead(d, meta)
}
func resourceAwsEipAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("association-id"),
Values: []*string{aws.String(d.Id())},
},
},
}
response, err := conn.DescribeAddresses(request)
if err != nil {
return fmt.Errorf("Error reading EC2 Elastic IP %s: %#v", d.Get("allocation_id").(string), err)
}
if response.Addresses == nil || len(response.Addresses) == 0 {
return fmt.Errorf("Unable to find EIP Association: %s", d.Id())
}
return readAwsEipAssociation(d, response.Addresses[0])
}
func resourceAwsEipAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
opts := &ec2.DisassociateAddressInput{
AssociationId: aws.String(d.Id()),
}
_, err := conn.DisassociateAddress(opts)
if err != nil {
return fmt.Errorf("Error deleting Elastic IP association: %s", err)
}
return nil
}
func readAwsEipAssociation(d *schema.ResourceData, address *ec2.Address) error {
if err := d.Set("allocation_id", address.AllocationId); err != nil {
return err
}
if err := d.Set("instance_id", address.InstanceId); err != nil {
return err
}
if err := d.Set("network_interface_id", address.NetworkInterfaceId); err != nil {
return err
}
if err := d.Set("private_ip_address", address.PrivateIpAddress); err != nil {
return err
}
if err := d.Set("public_ip", address.PublicIp); err != nil {
return err
}
return nil
}