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