github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/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.ResourceState,
    14  	d *terraform.ResourceDiff,
    15  	meta interface{}) (*terraform.ResourceState, 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  	rs.Dependencies = []terraform.ResourceDependency{
    36  		terraform.ResourceDependency{ID: rs.Attributes["route_table_id"]},
    37  	}
    38  
    39  	return rs, nil
    40  }
    41  
    42  func resource_aws_route_table_association_update(
    43  	s *terraform.ResourceState,
    44  	d *terraform.ResourceDiff,
    45  	meta interface{}) (*terraform.ResourceState, error) {
    46  	p := meta.(*ResourceProvider)
    47  	ec2conn := p.ec2conn
    48  
    49  	rs := s.MergeDiff(d)
    50  	log.Printf(
    51  		"[INFO] Replacing route table association: %s => %s",
    52  		rs.Attributes["subnet_id"],
    53  		rs.Attributes["route_table_id"])
    54  	resp, err := ec2conn.ReassociateRouteTable(
    55  		rs.ID,
    56  		rs.Attributes["route_table_id"])
    57  	if err != nil {
    58  		ec2err, ok := err.(*ec2.Error)
    59  		if ok && ec2err.Code == "InvalidAssociationID.NotFound" {
    60  			// Not found, so just create a new one
    61  			return resource_aws_route_table_association_create(s, d, meta)
    62  		}
    63  
    64  		return s, err
    65  	}
    66  
    67  	// Update the ID
    68  	rs.ID = resp.AssociationId
    69  	log.Printf("[INFO] Association ID: %s", rs.ID)
    70  
    71  	rs.Dependencies = []terraform.ResourceDependency{
    72  		terraform.ResourceDependency{ID: rs.Attributes["route_table_id"]},
    73  	}
    74  
    75  	return rs, nil
    76  }
    77  
    78  func resource_aws_route_table_association_destroy(
    79  	s *terraform.ResourceState,
    80  	meta interface{}) error {
    81  	p := meta.(*ResourceProvider)
    82  	ec2conn := p.ec2conn
    83  
    84  	log.Printf("[INFO] Deleting route table association: %s", s.ID)
    85  	if _, err := ec2conn.DisassociateRouteTable(s.ID); err != nil {
    86  		ec2err, ok := err.(*ec2.Error)
    87  		if ok && ec2err.Code == "InvalidAssociationID.NotFound" {
    88  			return nil
    89  		}
    90  
    91  		return fmt.Errorf("Error deleting route table association: %s", err)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func resource_aws_route_table_association_refresh(
    98  	s *terraform.ResourceState,
    99  	meta interface{}) (*terraform.ResourceState, error) {
   100  	p := meta.(*ResourceProvider)
   101  	ec2conn := p.ec2conn
   102  
   103  	// Get the routing table that this association belongs to
   104  	rtRaw, _, err := RouteTableStateRefreshFunc(
   105  		ec2conn, s.Attributes["route_table_id"])()
   106  	if err != nil {
   107  		return s, err
   108  	}
   109  	if rtRaw == nil {
   110  		return nil, nil
   111  	}
   112  	rt := rtRaw.(*ec2.RouteTable)
   113  
   114  	// Inspect that the association exists
   115  	found := false
   116  	for _, a := range rt.Associations {
   117  		if a.AssociationId == s.ID {
   118  			found = true
   119  			s.Attributes["subnet_id"] = a.SubnetId
   120  			break
   121  		}
   122  	}
   123  	if !found {
   124  		return nil, nil
   125  	}
   126  
   127  	return s, nil
   128  }
   129  
   130  func resource_aws_route_table_association_diff(
   131  	s *terraform.ResourceState,
   132  	c *terraform.ResourceConfig,
   133  	meta interface{}) (*terraform.ResourceDiff, error) {
   134  	b := &diff.ResourceBuilder{
   135  		Attrs: map[string]diff.AttrType{
   136  			"subnet_id":      diff.AttrTypeCreate,
   137  			"route_table_id": diff.AttrTypeUpdate,
   138  		},
   139  	}
   140  
   141  	return b.Diff(s, c)
   142  }