github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/builtin/providers/aws/resource_aws_customer_gateway.go (about)

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