github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_vpc.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/mitchellh/goamz/ec2" 11 ) 12 13 func resourceAwsVpc() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsVpcCreate, 16 Read: resourceAwsVpcRead, 17 Update: resourceAwsVpcUpdate, 18 Delete: resourceAwsVpcDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "cidr_block": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "instance_tenancy": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 ForceNew: true, 31 }, 32 33 "enable_dns_hostnames": &schema.Schema{ 34 Type: schema.TypeBool, 35 Optional: true, 36 Computed: true, 37 }, 38 39 "enable_dns_support": &schema.Schema{ 40 Type: schema.TypeBool, 41 Optional: true, 42 Computed: true, 43 }, 44 45 "main_route_table_id": &schema.Schema{ 46 Type: schema.TypeString, 47 Computed: true, 48 }, 49 50 "default_network_acl_id": &schema.Schema{ 51 Type: schema.TypeString, 52 Computed: true, 53 }, 54 55 "default_security_group_id": &schema.Schema{ 56 Type: schema.TypeString, 57 Computed: true, 58 }, 59 60 61 "tags": tagsSchema(), 62 }, 63 } 64 } 65 66 func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error { 67 ec2conn := meta.(*AWSClient).ec2conn 68 69 // Create the VPC 70 createOpts := &ec2.CreateVpc{ 71 CidrBlock: d.Get("cidr_block").(string), 72 InstanceTenancy: d.Get("instance_tenancy").(string), 73 } 74 log.Printf("[DEBUG] VPC create config: %#v", createOpts) 75 vpcResp, err := ec2conn.CreateVpc(createOpts) 76 if err != nil { 77 return fmt.Errorf("Error creating VPC: %s", err) 78 } 79 80 // Get the ID and store it 81 vpc := &vpcResp.VPC 82 log.Printf("[INFO] VPC ID: %s", vpc.VpcId) 83 d.SetId(vpc.VpcId) 84 85 // Set partial mode and say that we setup the cidr block 86 d.Partial(true) 87 d.SetPartial("cidr_block") 88 89 // Wait for the VPC to become available 90 log.Printf( 91 "[DEBUG] Waiting for VPC (%s) to become available", 92 d.Id()) 93 stateConf := &resource.StateChangeConf{ 94 Pending: []string{"pending"}, 95 Target: "available", 96 Refresh: VPCStateRefreshFunc(ec2conn, d.Id()), 97 Timeout: 10 * time.Minute, 98 } 99 if _, err := stateConf.WaitForState(); err != nil { 100 return fmt.Errorf( 101 "Error waiting for VPC (%s) to become available: %s", 102 d.Id(), err) 103 } 104 105 // Update our attributes and return 106 return resourceAwsVpcUpdate(d, meta) 107 } 108 109 func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error { 110 ec2conn := meta.(*AWSClient).ec2conn 111 112 // Refresh the VPC state 113 vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, d.Id())() 114 if err != nil { 115 return err 116 } 117 if vpcRaw == nil { 118 return nil 119 } 120 121 // VPC stuff 122 vpc := vpcRaw.(*ec2.VPC) 123 d.Set("cidr_block", vpc.CidrBlock) 124 125 // Tags 126 d.Set("tags", tagsToMap(vpc.Tags)) 127 128 // Attributes 129 resp, err := ec2conn.VpcAttribute(d.Id(), "enableDnsSupport") 130 if err != nil { 131 return err 132 } 133 d.Set("enable_dns_support", resp.EnableDnsSupport) 134 135 resp, err = ec2conn.VpcAttribute(d.Id(), "enableDnsHostnames") 136 if err != nil { 137 return err 138 } 139 d.Set("enable_dns_hostnames", resp.EnableDnsHostnames) 140 141 // Get the main routing table for this VPC 142 filter := ec2.NewFilter() 143 filter.Add("association.main", "true") 144 filter.Add("vpc-id", d.Id()) 145 routeResp, err := ec2conn.DescribeRouteTables(nil, filter) 146 if err != nil { 147 return err 148 } 149 if v := routeResp.RouteTables; len(v) > 0 { 150 d.Set("main_route_table_id", v[0].RouteTableId) 151 } 152 153 resourceAwsVpcSetDefaultNetworkAcl(ec2conn, d) 154 resourceAwsVpcSetDefaultSecurityGroup(ec2conn, d) 155 156 return nil 157 } 158 159 func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error { 160 ec2conn := meta.(*AWSClient).ec2conn 161 162 // Turn on partial mode 163 d.Partial(true) 164 165 if d.HasChange("enable_dns_hostnames") { 166 options := new(ec2.ModifyVpcAttribute) 167 options.EnableDnsHostnames = d.Get("enable_dns_hostnames").(bool) 168 options.SetEnableDnsHostnames = true 169 170 log.Printf( 171 "[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v", 172 d.Id(), options) 173 if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil { 174 return err 175 } 176 177 d.SetPartial("enable_dns_hostnames") 178 } 179 180 if d.HasChange("enable_dns_support") { 181 options := new(ec2.ModifyVpcAttribute) 182 options.EnableDnsSupport = d.Get("enable_dns_support").(bool) 183 options.SetEnableDnsSupport = true 184 185 log.Printf( 186 "[INFO] Modifying enable_dns_support vpc attribute for %s: %#v", 187 d.Id(), options) 188 if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil { 189 return err 190 } 191 192 d.SetPartial("enable_dns_support") 193 } 194 195 if err := setTags(ec2conn, d); err != nil { 196 return err 197 } else { 198 d.SetPartial("tags") 199 } 200 201 d.Partial(false) 202 return resourceAwsVpcRead(d, meta) 203 } 204 205 func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error { 206 ec2conn := meta.(*AWSClient).ec2conn 207 208 log.Printf("[INFO] Deleting VPC: %s", d.Id()) 209 if _, err := ec2conn.DeleteVpc(d.Id()); err != nil { 210 ec2err, ok := err.(*ec2.Error) 211 if ok && ec2err.Code == "InvalidVpcID.NotFound" { 212 return nil 213 } 214 215 return fmt.Errorf("Error deleting VPC: %s", err) 216 } 217 218 return nil 219 } 220 221 // VPCStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 222 // a VPC. 223 func VPCStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { 224 return func() (interface{}, string, error) { 225 resp, err := conn.DescribeVpcs([]string{id}, ec2.NewFilter()) 226 if err != nil { 227 if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidVpcID.NotFound" { 228 resp = nil 229 } else { 230 log.Printf("Error on VPCStateRefresh: %s", err) 231 return nil, "", err 232 } 233 } 234 235 if resp == nil { 236 // Sometimes AWS just has consistency issues and doesn't see 237 // our instance yet. Return an empty state. 238 return nil, "", nil 239 } 240 241 vpc := &resp.VPCs[0] 242 return vpc, vpc.State, nil 243 } 244 } 245 246 247 func resourceAwsVpcSetDefaultNetworkAcl(conn *ec2.EC2, d *schema.ResourceData) error { 248 filter := ec2.NewFilter() 249 filter.Add("default", "true") 250 filter.Add("vpc-id", d.Id()) 251 networkAclResp, err := conn.NetworkAcls(nil, filter) 252 253 if err != nil { 254 return err 255 } 256 if v := networkAclResp.NetworkAcls; len(v) > 0 { 257 d.Set("default_network_acl_id", v[0].NetworkAclId) 258 } 259 260 return nil 261 } 262 263 func resourceAwsVpcSetDefaultSecurityGroup(conn *ec2.EC2, d *schema.ResourceData) error { 264 filter := ec2.NewFilter() 265 filter.Add("group-name", "default") 266 filter.Add("vpc-id", d.Id()) 267 securityGroupResp, err := conn.SecurityGroups(nil, filter) 268 269 if err != nil { 270 return err 271 } 272 if v := securityGroupResp.Groups; len(v) > 0 { 273 d.Set("default_security_group_id", v[0].Id) 274 } 275 276 return nil 277 }