github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_egress_only_internet_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  	"github.com/hashicorp/errwrap"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsEgressOnlyInternetGateway() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsEgressOnlyInternetGatewayCreate,
    19  		Read:   resourceAwsEgressOnlyInternetGatewayRead,
    20  		Delete: resourceAwsEgressOnlyInternetGatewayDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"vpc_id": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func resourceAwsEgressOnlyInternetGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    33  	conn := meta.(*AWSClient).ec2conn
    34  
    35  	resp, err := conn.CreateEgressOnlyInternetGateway(&ec2.CreateEgressOnlyInternetGatewayInput{
    36  		VpcId: aws.String(d.Get("vpc_id").(string)),
    37  	})
    38  	if err != nil {
    39  		return fmt.Errorf("Error creating egress internet gateway: %s", err)
    40  	}
    41  
    42  	d.SetId(*resp.EgressOnlyInternetGateway.EgressOnlyInternetGatewayId)
    43  
    44  	err = resource.Retry(5*time.Minute, func() *resource.RetryError {
    45  		igRaw, _, err := EIGWStateRefreshFunc(conn, d.Id())()
    46  		if igRaw != nil {
    47  			return nil
    48  		}
    49  		if err == nil {
    50  			return resource.RetryableError(err)
    51  		} else {
    52  			return resource.NonRetryableError(err)
    53  		}
    54  	})
    55  
    56  	if err != nil {
    57  		return errwrap.Wrapf("{{err}}", err)
    58  	}
    59  
    60  	return resourceAwsEgressOnlyInternetGatewayRead(d, meta)
    61  }
    62  
    63  func EIGWStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
    64  	return func() (interface{}, string, error) {
    65  		resp, err := conn.DescribeEgressOnlyInternetGateways(&ec2.DescribeEgressOnlyInternetGatewaysInput{
    66  			EgressOnlyInternetGatewayIds: []*string{aws.String(id)},
    67  		})
    68  		if err != nil {
    69  			ec2err, ok := err.(awserr.Error)
    70  			if ok && ec2err.Code() == "InvalidEgressInternetGatewayID.NotFound" {
    71  				resp = nil
    72  			} else {
    73  				log.Printf("[ERROR] Error on EIGWStateRefreshFunc: %s", err)
    74  				return nil, "", err
    75  			}
    76  		}
    77  
    78  		if resp == nil {
    79  			// Sometimes AWS just has consistency issues and doesn't see
    80  			// our instance yet. Return an empty state.
    81  			return nil, "", nil
    82  		}
    83  
    84  		ig := resp.EgressOnlyInternetGateways[0]
    85  		return ig, "available", nil
    86  	}
    87  }
    88  
    89  func resourceAwsEgressOnlyInternetGatewayRead(d *schema.ResourceData, meta interface{}) error {
    90  	conn := meta.(*AWSClient).ec2conn
    91  
    92  	resp, err := conn.DescribeEgressOnlyInternetGateways(&ec2.DescribeEgressOnlyInternetGatewaysInput{
    93  		EgressOnlyInternetGatewayIds: []*string{aws.String(d.Id())},
    94  	})
    95  	if err != nil {
    96  		return fmt.Errorf("Error describing egress internet gateway: %s", err)
    97  	}
    98  
    99  	found := false
   100  	for _, igw := range resp.EgressOnlyInternetGateways {
   101  		if *igw.EgressOnlyInternetGatewayId == d.Id() {
   102  			found = true
   103  		}
   104  	}
   105  
   106  	if !found {
   107  		log.Printf("[Error] Cannot find Egress Only Internet Gateway: %q", d.Id())
   108  		d.SetId("")
   109  		return nil
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func resourceAwsEgressOnlyInternetGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   116  	conn := meta.(*AWSClient).ec2conn
   117  
   118  	_, err := conn.DeleteEgressOnlyInternetGateway(&ec2.DeleteEgressOnlyInternetGatewayInput{
   119  		EgressOnlyInternetGatewayId: aws.String(d.Id()),
   120  	})
   121  	if err != nil {
   122  		return fmt.Errorf("Error deleting egress internet gateway: %s", err)
   123  	}
   124  
   125  	return nil
   126  }