forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_customer_gateway.go
204 lines (168 loc) · 5.16 KB
/
resource_aws_customer_gateway.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
package aws
import (
"fmt"
"log"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCustomerGateway() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCustomerGatewayCreate,
Read: resourceAwsCustomerGatewayRead,
Update: resourceAwsCustomerGatewayUpdate,
Delete: resourceAwsCustomerGatewayDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bgp_asn": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tags": tagsSchema(),
},
}
}
func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
createOpts := &ec2.CreateCustomerGatewayInput{
BgpAsn: aws.Int64(int64(d.Get("bgp_asn").(int))),
PublicIp: aws.String(d.Get("ip_address").(string)),
Type: aws.String(d.Get("type").(string)),
}
// Create the Customer Gateway.
log.Printf("[DEBUG] Creating customer gateway")
resp, err := conn.CreateCustomerGateway(createOpts)
if err != nil {
return fmt.Errorf("Error creating customer gateway: %s", err)
}
// Store the ID
customerGateway := resp.CustomerGateway
d.SetId(*customerGateway.CustomerGatewayId)
log.Printf("[INFO] Customer gateway ID: %s", *customerGateway.CustomerGatewayId)
// Wait for the CustomerGateway to be available.
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"available"},
Refresh: customerGatewayRefreshFunc(conn, *customerGateway.CustomerGatewayId),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, stateErr := stateConf.WaitForState()
if stateErr != nil {
return fmt.Errorf(
"Error waiting for customer gateway (%s) to become ready: %s",
*customerGateway.CustomerGatewayId, err)
}
// Create tags.
if err := setTags(conn, d); err != nil {
return err
}
return nil
}
func customerGatewayRefreshFunc(conn *ec2.EC2, gatewayId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
gatewayFilter := &ec2.Filter{
Name: aws.String("customer-gateway-id"),
Values: []*string{aws.String(gatewayId)},
}
resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
Filters: []*ec2.Filter{gatewayFilter},
})
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
resp = nil
} else {
log.Printf("Error on CustomerGatewayRefresh: %s", err)
return nil, "", err
}
}
if resp == nil || len(resp.CustomerGateways) == 0 {
// handle consistency issues
return nil, "", nil
}
gateway := resp.CustomerGateways[0]
return gateway, *gateway.State, nil
}
}
func resourceAwsCustomerGatewayRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
gatewayFilter := &ec2.Filter{
Name: aws.String("customer-gateway-id"),
Values: []*string{aws.String(d.Id())},
}
resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
Filters: []*ec2.Filter{gatewayFilter},
})
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
d.SetId("")
return nil
} else {
log.Printf("[ERROR] Error finding CustomerGateway: %s", err)
return err
}
}
if len(resp.CustomerGateways) != 1 {
return fmt.Errorf("[ERROR] Error finding CustomerGateway: %s", d.Id())
}
if *resp.CustomerGateways[0].State == "deleted" {
log.Printf("[INFO] Customer Gateway is in `deleted` state: %s", d.Id())
d.SetId("")
return nil
}
customerGateway := resp.CustomerGateways[0]
d.Set("ip_address", customerGateway.IpAddress)
d.Set("type", customerGateway.Type)
d.Set("tags", tagsToMap(customerGateway.Tags))
if *customerGateway.BgpAsn != "" {
val, err := strconv.ParseInt(*customerGateway.BgpAsn, 0, 0)
if err != nil {
return fmt.Errorf("error parsing bgp_asn: %s", err)
}
d.Set("bgp_asn", int(val))
}
return nil
}
func resourceAwsCustomerGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
// Update tags if required.
if err := setTags(conn, d); err != nil {
return err
}
d.SetPartial("tags")
return resourceAwsCustomerGatewayRead(d, meta)
}
func resourceAwsCustomerGatewayDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
_, err := conn.DeleteCustomerGateway(&ec2.DeleteCustomerGatewayInput{
CustomerGatewayId: aws.String(d.Id()),
})
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
d.SetId("")
return nil
} else {
log.Printf("[ERROR] Error deleting CustomerGateway: %s", err)
return err
}
}
return nil
}