forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_main_route_table_association.go
169 lines (139 loc) · 4.76 KB
/
resource_aws_main_route_table_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
162
163
164
165
166
167
168
169
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsMainRouteTableAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsMainRouteTableAssociationCreate,
Read: resourceAwsMainRouteTableAssociationRead,
Update: resourceAwsMainRouteTableAssociationUpdate,
Delete: resourceAwsMainRouteTableAssociationDelete,
Schema: map[string]*schema.Schema{
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"route_table_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
// We use this field to record the main route table that is automatically
// created when the VPC is created. We need this to be able to "destroy"
// our main route table association, which we do by returning this route
// table to its original place as the Main Route Table for the VPC.
"original_route_table_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsMainRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
vpcId := d.Get("vpc_id").(string)
routeTableId := d.Get("route_table_id").(string)
log.Printf("[INFO] Creating main route table association: %s => %s", vpcId, routeTableId)
mainAssociation, err := findMainRouteTableAssociation(conn, vpcId)
if err != nil {
return err
}
resp, err := conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationInput{
AssociationId: mainAssociation.RouteTableAssociationId,
RouteTableId: aws.String(routeTableId),
})
if err != nil {
return err
}
d.Set("original_route_table_id", mainAssociation.RouteTableId)
d.SetId(*resp.NewAssociationId)
log.Printf("[INFO] New main route table association ID: %s", d.Id())
return nil
}
func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
mainAssociation, err := findMainRouteTableAssociation(
conn,
d.Get("vpc_id").(string))
if err != nil {
return err
}
if mainAssociation == nil || *mainAssociation.RouteTableAssociationId != d.Id() {
// It seems it doesn't exist anymore, so clear the ID
d.SetId("")
}
return nil
}
// Update is almost exactly like Create, except we want to retain the
// original_route_table_id - this needs to stay recorded as the AWS-created
// table from VPC creation.
func resourceAwsMainRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
vpcId := d.Get("vpc_id").(string)
routeTableId := d.Get("route_table_id").(string)
log.Printf("[INFO] Updating main route table association: %s => %s", vpcId, routeTableId)
resp, err := conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationInput{
AssociationId: aws.String(d.Id()),
RouteTableId: aws.String(routeTableId),
})
if err != nil {
return err
}
d.SetId(*resp.NewAssociationId)
log.Printf("[INFO] New main route table association ID: %s", d.Id())
return nil
}
func resourceAwsMainRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
vpcId := d.Get("vpc_id").(string)
originalRouteTableId := d.Get("original_route_table_id").(string)
log.Printf("[INFO] Deleting main route table association by resetting Main Route Table for VPC: %s to its original Route Table: %s",
vpcId,
originalRouteTableId)
resp, err := conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationInput{
AssociationId: aws.String(d.Id()),
RouteTableId: aws.String(originalRouteTableId),
})
if err != nil {
return err
}
log.Printf("[INFO] Resulting Association ID: %s", *resp.NewAssociationId)
return nil
}
func findMainRouteTableAssociation(conn *ec2.EC2, vpcId string) (*ec2.RouteTableAssociation, error) {
mainRouteTable, err := findMainRouteTable(conn, vpcId)
if err != nil {
return nil, err
}
if mainRouteTable == nil {
return nil, nil
}
for _, a := range mainRouteTable.Associations {
if *a.Main {
return a, nil
}
}
return nil, fmt.Errorf("Could not find main routing table association for VPC: %s", vpcId)
}
func findMainRouteTable(conn *ec2.EC2, vpcId string) (*ec2.RouteTable, error) {
mainFilter := &ec2.Filter{
Name: aws.String("association.main"),
Values: []*string{aws.String("true")},
}
vpcFilter := &ec2.Filter{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcId)},
}
routeResp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{mainFilter, vpcFilter},
})
if err != nil {
return nil, err
} else if len(routeResp.RouteTables) != 1 {
return nil, nil
}
return routeResp.RouteTables[0], nil
}