github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_customer_gateway.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsCustomerGateway() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsCustomerGatewayCreate,
    19  		Read:   resourceAwsCustomerGatewayRead,
    20  		Update: resourceAwsCustomerGatewayUpdate,
    21  		Delete: resourceAwsCustomerGatewayDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"bgp_asn": &schema.Schema{
    25  				Type:     schema.TypeInt,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"ip_address": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"type": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  
    42  			"tags": tagsSchema(),
    43  		},
    44  	}
    45  }
    46  
    47  func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    48  	conn := meta.(*AWSClient).ec2conn
    49  
    50  	createOpts := &ec2.CreateCustomerGatewayInput{
    51  		BgpAsn:   aws.Int64(int64(d.Get("bgp_asn").(int))),
    52  		PublicIp: aws.String(d.Get("ip_address").(string)),
    53  		Type:     aws.String(d.Get("type").(string)),
    54  	}
    55  
    56  	// Create the Customer Gateway.
    57  	log.Printf("[DEBUG] Creating customer gateway")
    58  	resp, err := conn.CreateCustomerGateway(createOpts)
    59  	if err != nil {
    60  		return fmt.Errorf("Error creating customer gateway: %s", err)
    61  	}
    62  
    63  	// Store the ID
    64  	customerGateway := resp.CustomerGateway
    65  	d.SetId(*customerGateway.CustomerGatewayId)
    66  	log.Printf("[INFO] Customer gateway ID: %s", *customerGateway.CustomerGatewayId)
    67  
    68  	// Wait for the CustomerGateway to be available.
    69  	stateConf := &resource.StateChangeConf{
    70  		Pending:    []string{"pending"},
    71  		Target:     "available",
    72  		Refresh:    customerGatewayRefreshFunc(conn, *customerGateway.CustomerGatewayId),
    73  		Timeout:    10 * time.Minute,
    74  		Delay:      10 * time.Second,
    75  		MinTimeout: 3 * time.Second,
    76  	}
    77  
    78  	_, stateErr := stateConf.WaitForState()
    79  	if stateErr != nil {
    80  		return fmt.Errorf(
    81  			"Error waiting for customer gateway (%s) to become ready: %s",
    82  			*customerGateway.CustomerGatewayId, err)
    83  	}
    84  
    85  	// Create tags.
    86  	if err := setTags(conn, d); err != nil {
    87  		return err
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func customerGatewayRefreshFunc(conn *ec2.EC2, gatewayId string) resource.StateRefreshFunc {
    94  	return func() (interface{}, string, error) {
    95  		gatewayFilter := &ec2.Filter{
    96  			Name:   aws.String("customer-gateway-id"),
    97  			Values: []*string{aws.String(gatewayId)},
    98  		}
    99  
   100  		resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
   101  			Filters: []*ec2.Filter{gatewayFilter},
   102  		})
   103  		if err != nil {
   104  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
   105  				resp = nil
   106  			} else {
   107  				log.Printf("Error on CustomerGatewayRefresh: %s", err)
   108  				return nil, "", err
   109  			}
   110  		}
   111  
   112  		if resp == nil || len(resp.CustomerGateways) == 0 {
   113  			// handle consistency issues
   114  			return nil, "", nil
   115  		}
   116  
   117  		gateway := resp.CustomerGateways[0]
   118  		return gateway, *gateway.State, nil
   119  	}
   120  }
   121  
   122  func resourceAwsCustomerGatewayRead(d *schema.ResourceData, meta interface{}) error {
   123  	conn := meta.(*AWSClient).ec2conn
   124  
   125  	gatewayFilter := &ec2.Filter{
   126  		Name:   aws.String("customer-gateway-id"),
   127  		Values: []*string{aws.String(d.Id())},
   128  	}
   129  
   130  	resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
   131  		Filters: []*ec2.Filter{gatewayFilter},
   132  	})
   133  	if err != nil {
   134  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
   135  			d.SetId("")
   136  			return nil
   137  		} else {
   138  			log.Printf("[ERROR] Error finding CustomerGateway: %s", err)
   139  			return err
   140  		}
   141  	}
   142  
   143  	if len(resp.CustomerGateways) != 1 {
   144  		return fmt.Errorf("[ERROR] Error finding CustomerGateway: %s", d.Id())
   145  	}
   146  
   147  	customerGateway := resp.CustomerGateways[0]
   148  	d.Set("bgp_asn", customerGateway.BgpAsn)
   149  	d.Set("ip_address", customerGateway.IpAddress)
   150  	d.Set("type", customerGateway.Type)
   151  	d.Set("tags", tagsToMap(customerGateway.Tags))
   152  
   153  	return nil
   154  }
   155  
   156  func resourceAwsCustomerGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
   157  	conn := meta.(*AWSClient).ec2conn
   158  
   159  	// Update tags if required.
   160  	if err := setTags(conn, d); err != nil {
   161  		return err
   162  	}
   163  
   164  	d.SetPartial("tags")
   165  
   166  	return resourceAwsCustomerGatewayRead(d, meta)
   167  }
   168  
   169  func resourceAwsCustomerGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   170  	conn := meta.(*AWSClient).ec2conn
   171  
   172  	_, err := conn.DeleteCustomerGateway(&ec2.DeleteCustomerGatewayInput{
   173  		CustomerGatewayId: aws.String(d.Id()),
   174  	})
   175  	if err != nil {
   176  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
   177  			d.SetId("")
   178  			return nil
   179  		} else {
   180  			log.Printf("[ERROR] Error deleting CustomerGateway: %s", err)
   181  			return err
   182  		}
   183  	}
   184  
   185  	return nil
   186  }