forked from hashicorp/terraform-provider-aws
/
resource_aws_dx_bgp_peer.go
210 lines (185 loc) · 5.95 KB
/
resource_aws_dx_bgp_peer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsDxBgpPeer() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDxBgpPeerCreate,
Read: resourceAwsDxBgpPeerRead,
Delete: resourceAwsDxBgpPeerDelete,
Schema: map[string]*schema.Schema{
"address_family": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{directconnect.AddressFamilyIpv4, directconnect.AddressFamilyIpv6}, false),
},
"bgp_asn": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"virtual_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"amazon_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"bgp_auth_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"customer_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"bgp_status": {
Type: schema.TypeString,
Computed: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}
func resourceAwsDxBgpPeerCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
vifId := d.Get("virtual_interface_id").(string)
addrFamily := d.Get("address_family").(string)
asn := int64(d.Get("bgp_asn").(int))
req := &directconnect.CreateBGPPeerInput{
VirtualInterfaceId: aws.String(vifId),
NewBGPPeer: &directconnect.NewBGPPeer{
AddressFamily: aws.String(addrFamily),
Asn: aws.Int64(asn),
},
}
if v, ok := d.GetOk("amazon_address"); ok && v.(string) != "" {
req.NewBGPPeer.AmazonAddress = aws.String(v.(string))
}
if v, ok := d.GetOk("bgp_auth_key"); ok && v.(string) != "" {
req.NewBGPPeer.AuthKey = aws.String(v.(string))
}
if v, ok := d.GetOk("customer_address"); ok && v.(string) != "" {
req.NewBGPPeer.CustomerAddress = aws.String(v.(string))
}
log.Printf("[DEBUG] Creating Direct Connect BGP peer: %#v", req)
_, err := conn.CreateBGPPeer(req)
if err != nil {
return fmt.Errorf("Error creating Direct Connect BGP peer: %s", err)
}
d.SetId(fmt.Sprintf("%s-%s-%d", vifId, addrFamily, asn))
stateConf := &resource.StateChangeConf{
Pending: []string{
directconnect.BGPPeerStatePending,
},
Target: []string{
directconnect.BGPPeerStateAvailable,
directconnect.BGPPeerStateVerifying,
},
Refresh: dxBgpPeerStateRefresh(conn, vifId, addrFamily, asn),
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 BGP peer (%s) to be available: %s", d.Id(), err)
}
return resourceAwsDxBgpPeerRead(d, meta)
}
func resourceAwsDxBgpPeerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
vifId := d.Get("virtual_interface_id").(string)
addrFamily := d.Get("address_family").(string)
asn := int64(d.Get("bgp_asn").(int))
bgpPeerRaw, state, err := dxBgpPeerStateRefresh(conn, vifId, addrFamily, asn)()
if err != nil {
return fmt.Errorf("Error reading Direct Connect BGP peer: %s", err)
}
if state == directconnect.BGPPeerStateDeleted {
log.Printf("[WARN] Direct Connect BGP peer (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
bgpPeer := bgpPeerRaw.(*directconnect.BGPPeer)
d.Set("amazon_address", bgpPeer.AmazonAddress)
d.Set("bgp_auth_key", bgpPeer.AuthKey)
d.Set("customer_address", bgpPeer.CustomerAddress)
d.Set("bgp_status", bgpPeer.BgpStatus)
return nil
}
func resourceAwsDxBgpPeerDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
vifId := d.Get("virtual_interface_id").(string)
addrFamily := d.Get("address_family").(string)
asn := int64(d.Get("bgp_asn").(int))
log.Printf("[DEBUG] Deleting Direct Connect BGP peer: %s", d.Id())
_, err := conn.DeleteBGPPeer(&directconnect.DeleteBGPPeerInput{
Asn: aws.Int64(asn),
CustomerAddress: aws.String(d.Get("customer_address").(string)),
VirtualInterfaceId: aws.String(vifId),
})
if err != nil {
// This is the error returned if the BGP peering has already gone.
if isAWSErr(err, "DirectConnectClientException", "The last BGP Peer on a Virtual Interface cannot be deleted") {
return nil
}
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{
directconnect.BGPPeerStateAvailable,
directconnect.BGPPeerStateDeleting,
directconnect.BGPPeerStatePending,
directconnect.BGPPeerStateVerifying,
},
Target: []string{
directconnect.BGPPeerStateDeleted,
},
Refresh: dxBgpPeerStateRefresh(conn, vifId, addrFamily, asn),
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 BGP peer (%s) to be deleted: %s", d.Id(), err)
}
return nil
}
func dxBgpPeerStateRefresh(conn *directconnect.DirectConnect, vifId, addrFamily string, asn int64) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
vif, err := dxVirtualInterfaceRead(vifId, conn)
if err != nil {
return nil, "", err
}
if vif == nil {
return "", directconnect.BGPPeerStateDeleted, nil
}
for _, bgpPeer := range vif.BgpPeers {
if aws.StringValue(bgpPeer.AddressFamily) == addrFamily && aws.Int64Value(bgpPeer.Asn) == asn {
return bgpPeer, aws.StringValue(bgpPeer.BgpPeerState), nil
}
}
return "", directconnect.BGPPeerStateDeleted, nil
}
}