github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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/diff"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func resource_aws_route_table_association_create(
    13  	s *terraform.InstanceState,
    14  	d *terraform.InstanceDiff,
    15  	meta interface{}) (*terraform.InstanceState, error) {
    16  	p := meta.(*ResourceProvider)
    17  	ec2conn := p.ec2conn
    18  	rs := s.MergeDiff(d)
    19  
    20  	log.Printf(
    21  		"[INFO] Creating route table association: %s => %s",
    22  		rs.Attributes["subnet_id"],
    23  		rs.Attributes["route_table_id"])
    24  	resp, err := ec2conn.AssociateRouteTable(
    25  		rs.Attributes["route_table_id"],
    26  		rs.Attributes["subnet_id"])
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	// Set the ID and return
    32  	rs.ID = resp.AssociationId
    33  	log.Printf("[INFO] Association ID: %s", rs.ID)
    34  
    35  	return rs, nil
    36  }
    37  
    38  func resource_aws_route_table_association_update(
    39  	s *terraform.InstanceState,
    40  	d *terraform.InstanceDiff,
    41  	meta interface{}) (*terraform.InstanceState, error) {
    42  	p := meta.(*ResourceProvider)
    43  	ec2conn := p.ec2conn
    44  
    45  	rs := s.MergeDiff(d)
    46  	log.Printf(
    47  		"[INFO] Replacing route table association: %s => %s",
    48  		rs.Attributes["subnet_id"],
    49  		rs.Attributes["route_table_id"])
    50  	resp, err := ec2conn.ReassociateRouteTable(
    51  		rs.ID,
    52  		rs.Attributes["route_table_id"])
    53  	if err != nil {
    54  		ec2err, ok := err.(*ec2.Error)
    55  		if ok && ec2err.Code == "InvalidAssociationID.NotFound" {
    56  			// Not found, so just create a new one
    57  			return resource_aws_route_table_association_create(s, d, meta)
    58  		}
    59  
    60  		return s, err
    61  	}
    62  
    63  	// Update the ID
    64  	rs.ID = resp.AssociationId
    65  	log.Printf("[INFO] Association ID: %s", rs.ID)
    66  
    67  	return rs, nil
    68  }
    69  
    70  func resource_aws_route_table_association_destroy(
    71  	s *terraform.InstanceState,
    72  	meta interface{}) error {
    73  	p := meta.(*ResourceProvider)
    74  	ec2conn := p.ec2conn
    75  
    76  	log.Printf("[INFO] Deleting route table association: %s", s.ID)
    77  	if _, err := ec2conn.DisassociateRouteTable(s.ID); err != nil {
    78  		ec2err, ok := err.(*ec2.Error)
    79  		if ok && ec2err.Code == "InvalidAssociationID.NotFound" {
    80  			return nil
    81  		}
    82  
    83  		return fmt.Errorf("Error deleting route table association: %s", err)
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func resource_aws_route_table_association_refresh(
    90  	s *terraform.InstanceState,
    91  	meta interface{}) (*terraform.InstanceState, error) {
    92  	p := meta.(*ResourceProvider)
    93  	ec2conn := p.ec2conn
    94  
    95  	// Get the routing table that this association belongs to
    96  	rtRaw, _, err := RouteTableStateRefreshFunc(
    97  		ec2conn, s.Attributes["route_table_id"])()
    98  	if err != nil {
    99  		return s, err
   100  	}
   101  	if rtRaw == nil {
   102  		return nil, nil
   103  	}
   104  	rt := rtRaw.(*ec2.RouteTable)
   105  
   106  	// Inspect that the association exists
   107  	found := false
   108  	for _, a := range rt.Associations {
   109  		if a.AssociationId == s.ID {
   110  			found = true
   111  			s.Attributes["subnet_id"] = a.SubnetId
   112  			break
   113  		}
   114  	}
   115  	if !found {
   116  		return nil, nil
   117  	}
   118  
   119  	return s, nil
   120  }
   121  
   122  func resource_aws_route_table_association_diff(
   123  	s *terraform.InstanceState,
   124  	c *terraform.ResourceConfig,
   125  	meta interface{}) (*terraform.InstanceDiff, error) {
   126  	b := &diff.ResourceBuilder{
   127  		Attrs: map[string]diff.AttrType{
   128  			"subnet_id":      diff.AttrTypeCreate,
   129  			"route_table_id": diff.AttrTypeUpdate,
   130  		},
   131  	}
   132  
   133  	return b.Diff(s, c)
   134  }