forked from hashicorp/terraform-provider-aws
/
resource_aws_ec2_transit_gateway_route_table_association.go
141 lines (113 loc) · 5.33 KB
/
resource_aws_ec2_transit_gateway_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
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"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsEc2TransitGatewayRouteTableAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEc2TransitGatewayRouteTableAssociationCreate,
Read: resourceAwsEc2TransitGatewayRouteTableAssociationRead,
Delete: resourceAwsEc2TransitGatewayRouteTableAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Computed: true,
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
},
"transit_gateway_attachment_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"transit_gateway_route_table_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
},
}
}
func resourceAwsEc2TransitGatewayRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
transitGatewayAttachmentID := d.Get("transit_gateway_attachment_id").(string)
transitGatewayRouteTableID := d.Get("transit_gateway_route_table_id").(string)
input := &ec2.AssociateTransitGatewayRouteTableInput{
TransitGatewayAttachmentId: aws.String(transitGatewayAttachmentID),
TransitGatewayRouteTableId: aws.String(transitGatewayRouteTableID),
}
_, err := conn.AssociateTransitGatewayRouteTable(input)
if err != nil {
return fmt.Errorf("error associating EC2 Transit Gateway Route Table (%s) association (%s): %s", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}
d.SetId(fmt.Sprintf("%s_%s", transitGatewayRouteTableID, transitGatewayAttachmentID))
if err := waitForEc2TransitGatewayRouteTableAssociationCreation(conn, transitGatewayRouteTableID, transitGatewayAttachmentID); err != nil {
return fmt.Errorf("error waiting for EC2 Transit Gateway Route Table (%s) association (%s): %s", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}
return resourceAwsEc2TransitGatewayRouteTableAssociationRead(d, meta)
}
func resourceAwsEc2TransitGatewayRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
transitGatewayRouteTableID, transitGatewayAttachmentID, err := decodeEc2TransitGatewayRouteTableAssociationID(d.Id())
if err != nil {
return err
}
transitGatewayAssociation, err := ec2DescribeTransitGatewayRouteTableAssociation(conn, transitGatewayRouteTableID, transitGatewayAttachmentID)
if isAWSErr(err, "InvalidRouteTableID.NotFound", "") {
log.Printf("[WARN] EC2 Transit Gateway Route Table (%s) not found, removing from state", transitGatewayRouteTableID)
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error reading EC2 Transit Gateway Route Table (%s) Association (%s): %s", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}
if transitGatewayAssociation == nil {
log.Printf("[WARN] EC2 Transit Gateway Route Table (%s) Association (%s) not found, removing from state", transitGatewayRouteTableID, transitGatewayAttachmentID)
d.SetId("")
return nil
}
if aws.StringValue(transitGatewayAssociation.State) == ec2.TransitGatewayAssociationStateDisassociating {
log.Printf("[WARN] EC2 Transit Gateway Route Table (%s) Association (%s) in deleted state (%s), removing from state", transitGatewayRouteTableID, transitGatewayAttachmentID, aws.StringValue(transitGatewayAssociation.State))
d.SetId("")
return nil
}
d.Set("resource_id", aws.StringValue(transitGatewayAssociation.ResourceId))
d.Set("resource_type", aws.StringValue(transitGatewayAssociation.ResourceType))
d.Set("transit_gateway_attachment_id", aws.StringValue(transitGatewayAssociation.TransitGatewayAttachmentId))
d.Set("transit_gateway_route_table_id", transitGatewayRouteTableID)
return nil
}
func resourceAwsEc2TransitGatewayRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
transitGatewayRouteTableID, transitGatewayAttachmentID, err := decodeEc2TransitGatewayRouteTableAssociationID(d.Id())
if err != nil {
return err
}
input := &ec2.DisassociateTransitGatewayRouteTableInput{
TransitGatewayAttachmentId: aws.String(transitGatewayAttachmentID),
TransitGatewayRouteTableId: aws.String(transitGatewayRouteTableID),
}
log.Printf("[DEBUG] Disassociating EC2 Transit Gateway Route Table (%s) Association (%s): %s", transitGatewayRouteTableID, transitGatewayAttachmentID, input)
_, err = conn.DisassociateTransitGatewayRouteTable(input)
if isAWSErr(err, "InvalidRouteTableID.NotFound", "") {
return nil
}
if err != nil {
return fmt.Errorf("error disassociating EC2 Transit Gateway Route Table (%s) Association (%s): %s", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}
if err := waitForEc2TransitGatewayRouteTableAssociationDeletion(conn, transitGatewayRouteTableID, transitGatewayAttachmentID); err != nil {
return fmt.Errorf("error waiting for EC2 Transit Gateway Route Table (%s) Association (%s) disassociation: %s", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}
return nil
}