forked from hashicorp/terraform-provider-aws
/
resource_aws_dx_hosted_private_virtual_interface_accepter.go
166 lines (145 loc) · 5.16 KB
/
resource_aws_dx_hosted_private_virtual_interface_accepter.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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsDxHostedPrivateVirtualInterfaceAccepter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDxHostedPrivateVirtualInterfaceAccepterCreate,
Read: resourceAwsDxHostedPrivateVirtualInterfaceAccepterRead,
Update: resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate,
Delete: resourceAwsDxHostedPrivateVirtualInterfaceAccepterDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsDxHostedPrivateVirtualInterfaceAccepterImport,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"virtual_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vpn_gateway_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"dx_gateway_id"},
},
"dx_gateway_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"vpn_gateway_id"},
},
"tags": tagsSchema(),
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}
func resourceAwsDxHostedPrivateVirtualInterfaceAccepterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
vgwIdRaw, vgwOk := d.GetOk("vpn_gateway_id")
dxgwIdRaw, dxgwOk := d.GetOk("dx_gateway_id")
if vgwOk == dxgwOk {
return fmt.Errorf(
"One of ['vpn_gateway_id', 'dx_gateway_id'] must be set to create a Direct Connect private virtual interface accepter")
}
vifId := d.Get("virtual_interface_id").(string)
req := &directconnect.ConfirmPrivateVirtualInterfaceInput{
VirtualInterfaceId: aws.String(vifId),
}
if vgwOk && vgwIdRaw.(string) != "" {
req.VirtualGatewayId = aws.String(vgwIdRaw.(string))
}
if dxgwOk && dxgwIdRaw.(string) != "" {
req.DirectConnectGatewayId = aws.String(dxgwIdRaw.(string))
}
log.Printf("[DEBUG] Accepting Direct Connect hosted private virtual interface: %#v", req)
_, err := conn.ConfirmPrivateVirtualInterface(req)
if err != nil {
return fmt.Errorf("Error accepting Direct Connect hosted private virtual interface: %s", err.Error())
}
d.SetId(vifId)
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
d.Set("arn", arn)
if err := dxHostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return err
}
return resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate(d, meta)
}
func resourceAwsDxHostedPrivateVirtualInterfaceAccepterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
vif, err := dxVirtualInterfaceRead(d.Id(), conn)
if err != nil {
return err
}
if vif == nil {
log.Printf("[WARN] Direct Connect virtual interface (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
vifState := aws.StringValue(vif.VirtualInterfaceState)
if vifState != directconnect.VirtualInterfaceStateAvailable &&
vifState != directconnect.VirtualInterfaceStateDown {
log.Printf("[WARN] Direct Connect virtual interface (%s) is '%s', removing from state", vifState, d.Id())
d.SetId("")
return nil
}
d.Set("virtual_interface_id", vif.VirtualInterfaceId)
d.Set("vpn_gateway_id", vif.VirtualGatewayId)
d.Set("dx_gateway_id", vif.DirectConnectGatewayId)
err1 := getTagsDX(conn, d, d.Get("arn").(string))
return err1
}
func resourceAwsDxHostedPrivateVirtualInterfaceAccepterUpdate(d *schema.ResourceData, meta interface{}) error {
if err := dxVirtualInterfaceUpdate(d, meta); err != nil {
return err
}
return resourceAwsDxHostedPrivateVirtualInterfaceAccepterRead(d, meta)
}
func resourceAwsDxHostedPrivateVirtualInterfaceAccepterDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Will not delete Direct Connect virtual interface. Terraform will remove this resource from the state file, however resources may remain.")
return nil
}
func resourceAwsDxHostedPrivateVirtualInterfaceAccepterImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "directconnect",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("dxvif/%s", d.Id()),
}.String()
d.Set("arn", arn)
return []*schema.ResourceData{d}, nil
}
func dxHostedPrivateVirtualInterfaceAccepterWaitUntilAvailable(conn *directconnect.DirectConnect, vifId string, timeout time.Duration) error {
return dxVirtualInterfaceWaitUntilAvailable(
conn,
vifId,
timeout,
[]string{
directconnect.VirtualInterfaceStateConfirming,
directconnect.VirtualInterfaceStatePending,
},
[]string{
directconnect.VirtualInterfaceStateAvailable,
directconnect.VirtualInterfaceStateDown,
})
}