github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_route_table_association.go (about)

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