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