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