2015-04-28 16:28:52 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-20 13:21:23 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
2015-04-28 16:28:52 +02:00
|
|
|
"github.com/awslabs/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,
|
|
|
|
|
|
|
|
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.Long(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: "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.
|
2015-05-12 21:58:10 +02:00
|
|
|
if err := setTags(conn, d); err != nil {
|
2015-04-28 16:28:52 +02:00
|
|
|
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 {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
|
2015-04-28 16:28:52 +02:00
|
|
|
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 {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
|
2015-04-28 16:28:52 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
customerGateway := resp.CustomerGateways[0]
|
|
|
|
d.Set("bgp_asn", customerGateway.BGPASN)
|
|
|
|
d.Set("ip_address", customerGateway.IPAddress)
|
|
|
|
d.Set("type", customerGateway.Type)
|
2015-05-12 21:58:10 +02:00
|
|
|
d.Set("tags", tagsToMap(customerGateway.Tags))
|
2015-04-28 16:28:52 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsCustomerGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
// Update tags if required.
|
2015-05-12 21:58:10 +02:00
|
|
|
if err := setTags(conn, d); err != nil {
|
2015-04-28 16:28:52 +02:00
|
|
|
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 {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
|
2015-04-28 16:28:52 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
log.Printf("[ERROR] Error deleting CustomerGateway: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|