forked from hashicorp/terraform-provider-aws
/
dx_vif.go
132 lines (112 loc) · 3.93 KB
/
dx_vif.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
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"
)
func dxVirtualInterfaceRead(id string, conn *directconnect.DirectConnect) (*directconnect.VirtualInterface, error) {
resp, state, err := dxVirtualInterfaceStateRefresh(conn, id)()
if err != nil {
return nil, fmt.Errorf("Error reading Direct Connect virtual interface: %s", err)
}
if state == directconnect.VirtualInterfaceStateDeleted {
return nil, nil
}
return resp.(*directconnect.VirtualInterface), nil
}
func dxVirtualInterfaceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
req := &directconnect.UpdateVirtualInterfaceAttributesInput{
VirtualInterfaceId: aws.String(d.Id()),
}
requestUpdate := false
if d.HasChange("mtu") {
req.Mtu = aws.Int64(int64(d.Get("mtu").(int)))
requestUpdate = true
}
if requestUpdate {
log.Printf("[DEBUG] Modifying Direct Connect virtual interface attributes: %#v", req)
_, err := conn.UpdateVirtualInterfaceAttributes(req)
if err != nil {
return fmt.Errorf("Error modifying Direct Connect virtual interface (%s) attributes, error: %s", d.Id(), err)
}
}
if err := setTagsDX(conn, d, d.Get("arn").(string)); err != nil {
return err
}
return nil
}
func dxVirtualInterfaceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
log.Printf("[DEBUG] Deleting Direct Connect virtual interface: %s", d.Id())
_, err := conn.DeleteVirtualInterface(&directconnect.DeleteVirtualInterfaceInput{
VirtualInterfaceId: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, directconnect.ErrCodeClientException, "does not exist") {
return nil
}
return fmt.Errorf("Error deleting Direct Connect virtual interface: %s", err)
}
deleteStateConf := &resource.StateChangeConf{
Pending: []string{
directconnect.VirtualInterfaceStateAvailable,
directconnect.VirtualInterfaceStateConfirming,
directconnect.VirtualInterfaceStateDeleting,
directconnect.VirtualInterfaceStateDown,
directconnect.VirtualInterfaceStatePending,
directconnect.VirtualInterfaceStateRejected,
directconnect.VirtualInterfaceStateVerifying,
},
Target: []string{
directconnect.VirtualInterfaceStateDeleted,
},
Refresh: dxVirtualInterfaceStateRefresh(conn, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = deleteStateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Direct Connect virtual interface (%s) to be deleted: %s", d.Id(), err)
}
return nil
}
func dxVirtualInterfaceStateRefresh(conn *directconnect.DirectConnect, vifId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{
VirtualInterfaceId: aws.String(vifId),
})
if err != nil {
return nil, "", err
}
n := len(resp.VirtualInterfaces)
switch n {
case 0:
return "", directconnect.VirtualInterfaceStateDeleted, nil
case 1:
vif := resp.VirtualInterfaces[0]
return vif, aws.StringValue(vif.VirtualInterfaceState), nil
default:
return nil, "", fmt.Errorf("Found %d Direct Connect virtual interfaces for %s, expected 1", n, vifId)
}
}
}
func dxVirtualInterfaceWaitUntilAvailable(conn *directconnect.DirectConnect, vifId string, timeout time.Duration, pending, target []string) error {
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: target,
Refresh: dxVirtualInterfaceStateRefresh(conn, vifId),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Direct Connect virtual interface (%s) to become available: %s", vifId, err)
}
return nil
}