forked from hashicorp/terraform-provider-aws
/
resource_aws_dx_gateway_association.go
158 lines (132 loc) · 4.85 KB
/
resource_aws_dx_gateway_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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
const (
GatewayAssociationStateDeleted = "deleted"
)
func resourceAwsDxGatewayAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDxGatewayAssociationCreate,
Read: resourceAwsDxGatewayAssociationRead,
Delete: resourceAwsDxGatewayAssociationDelete,
Schema: map[string]*schema.Schema{
"dx_gateway_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vpn_gateway_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}
func resourceAwsDxGatewayAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
dxgwId := d.Get("dx_gateway_id").(string)
vgwId := d.Get("vpn_gateway_id").(string)
req := &directconnect.CreateDirectConnectGatewayAssociationInput{
DirectConnectGatewayId: aws.String(dxgwId),
VirtualGatewayId: aws.String(vgwId),
}
log.Printf("[DEBUG] Creating Direct Connect gateway association: %#v", req)
_, err := conn.CreateDirectConnectGatewayAssociation(req)
if err != nil {
return fmt.Errorf("Error creating Direct Connect gateway association: %s", err)
}
d.SetId(dxGatewayAssociationId(dxgwId, vgwId))
stateConf := &resource.StateChangeConf{
Pending: []string{directconnect.GatewayAssociationStateAssociating},
Target: []string{directconnect.GatewayAssociationStateAssociated},
Refresh: dxGatewayAssociationStateRefresh(conn, dxgwId, vgwId),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Direct Connect gateway association (%s) to become available: %s", d.Id(), err)
}
return nil
}
func resourceAwsDxGatewayAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
dxgwId := d.Get("dx_gateway_id").(string)
vgwId := d.Get("vpn_gateway_id").(string)
_, state, err := dxGatewayAssociationStateRefresh(conn, dxgwId, vgwId)()
if err != nil {
return fmt.Errorf("Error reading Direct Connect gateway association: %s", err)
}
if state == GatewayAssociationStateDeleted {
log.Printf("[WARN] Direct Connect gateway association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return nil
}
func resourceAwsDxGatewayAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
dxgwId := d.Get("dx_gateway_id").(string)
vgwId := d.Get("vpn_gateway_id").(string)
log.Printf("[DEBUG] Deleting Direct Connect gateway association: %s", d.Id())
_, err := conn.DeleteDirectConnectGatewayAssociation(&directconnect.DeleteDirectConnectGatewayAssociationInput{
DirectConnectGatewayId: aws.String(dxgwId),
VirtualGatewayId: aws.String(vgwId),
})
if err != nil {
if isAWSErr(err, "DirectConnectClientException", "No association exists") {
return nil
}
return fmt.Errorf("Error deleting Direct Connect gateway association: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{directconnect.GatewayAssociationStateDisassociating},
Target: []string{directconnect.GatewayAssociationStateDisassociated, GatewayAssociationStateDeleted},
Refresh: dxGatewayAssociationStateRefresh(conn, dxgwId, vgwId),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Direct Connect gateway association (%s) to be deleted: %s", d.Id(), err.Error())
}
return nil
}
func dxGatewayAssociationStateRefresh(conn *directconnect.DirectConnect, dxgwId, vgwId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeDirectConnectGatewayAssociations(&directconnect.DescribeDirectConnectGatewayAssociationsInput{
DirectConnectGatewayId: aws.String(dxgwId),
VirtualGatewayId: aws.String(vgwId),
})
if err != nil {
return nil, "", err
}
n := len(resp.DirectConnectGatewayAssociations)
switch n {
case 0:
return "", GatewayAssociationStateDeleted, nil
case 1:
assoc := resp.DirectConnectGatewayAssociations[0]
return assoc, aws.StringValue(assoc.AssociationState), nil
default:
return nil, "", fmt.Errorf("Found %d Direct Connect gateway associations for %s, expected 1", n, dxGatewayAssociationId(dxgwId, vgwId))
}
}
}
func dxGatewayAssociationId(dxgwId, vgwId string) string {
return fmt.Sprintf("ga-%s%s", dxgwId, vgwId)
}