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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsRouteTableAssociation() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsRouteTableAssociationCreate,
    16  		Read:   resourceAwsRouteTableAssociationRead,
    17  		Update: resourceAwsRouteTableAssociationUpdate,
    18  		Delete: resourceAwsRouteTableAssociationDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"subnet_id": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"route_table_id": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func resourceAwsRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
    36  	conn := meta.(*AWSClient).ec2conn
    37  
    38  	log.Printf(
    39  		"[INFO] Creating route table association: %s => %s",
    40  		d.Get("subnet_id").(string),
    41  		d.Get("route_table_id").(string))
    42  
    43  	resp, err := conn.AssociateRouteTable(&ec2.AssociateRouteTableInput{
    44  		RouteTableId: aws.String(d.Get("route_table_id").(string)),
    45  		SubnetId:     aws.String(d.Get("subnet_id").(string)),
    46  	})
    47  
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	// Set the ID and return
    53  	d.SetId(*resp.AssociationId)
    54  	log.Printf("[INFO] Association ID: %s", d.Id())
    55  
    56  	return nil
    57  }
    58  
    59  func resourceAwsRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
    60  	conn := meta.(*AWSClient).ec2conn
    61  
    62  	// Get the routing table that this association belongs to
    63  	rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(
    64  		conn, d.Get("route_table_id").(string))()
    65  	if err != nil {
    66  		return err
    67  	}
    68  	if rtRaw == nil {
    69  		return nil
    70  	}
    71  	rt := rtRaw.(*ec2.RouteTable)
    72  
    73  	// Inspect that the association exists
    74  	found := false
    75  	for _, a := range rt.Associations {
    76  		if *a.RouteTableAssociationId == d.Id() {
    77  			found = true
    78  			d.Set("subnet_id", *a.SubnetId)
    79  			break
    80  		}
    81  	}
    82  
    83  	if !found {
    84  		// It seems it doesn't exist anymore, so clear the ID
    85  		d.SetId("")
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func resourceAwsRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
    92  	conn := meta.(*AWSClient).ec2conn
    93  
    94  	log.Printf(
    95  		"[INFO] Creating route table association: %s => %s",
    96  		d.Get("subnet_id").(string),
    97  		d.Get("route_table_id").(string))
    98  
    99  	req := &ec2.ReplaceRouteTableAssociationInput{
   100  		AssociationId: aws.String(d.Id()),
   101  		RouteTableId:  aws.String(d.Get("route_table_id").(string)),
   102  	}
   103  	resp, err := conn.ReplaceRouteTableAssociation(req)
   104  
   105  	if err != nil {
   106  		ec2err, ok := err.(awserr.Error)
   107  		if ok && ec2err.Code() == "InvalidAssociationID.NotFound" {
   108  			// Not found, so just create a new one
   109  			return resourceAwsRouteTableAssociationCreate(d, meta)
   110  		}
   111  
   112  		return err
   113  	}
   114  
   115  	// Update the ID
   116  	d.SetId(*resp.NewAssociationId)
   117  	log.Printf("[INFO] Association ID: %s", d.Id())
   118  
   119  	return nil
   120  }
   121  
   122  func resourceAwsRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
   123  	conn := meta.(*AWSClient).ec2conn
   124  
   125  	log.Printf("[INFO] Deleting route table association: %s", d.Id())
   126  	_, err := conn.DisassociateRouteTable(&ec2.DisassociateRouteTableInput{
   127  		AssociationId: aws.String(d.Id()),
   128  	})
   129  	if err != nil {
   130  		ec2err, ok := err.(awserr.Error)
   131  		if ok && ec2err.Code() == "InvalidAssociationID.NotFound" {
   132  			return nil
   133  		}
   134  
   135  		return fmt.Errorf("Error deleting route table association: %s", err)
   136  	}
   137  
   138  	return nil
   139  }