2014-07-07 21:16:58 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-10-09 02:54:00 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-07 21:16:58 +02:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func resourceAwsVpc() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsVpcCreate,
|
|
|
|
Read: resourceAwsVpcRead,
|
|
|
|
Update: resourceAwsVpcUpdate,
|
|
|
|
Delete: resourceAwsVpcDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"cidr_block": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"enable_dns_hostnames": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"enable_dns_support": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-10-09 02:54:00 +02:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2014-10-09 01:43:13 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 21:16:58 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
// Create the VPC
|
|
|
|
createOpts := &ec2.CreateVpc{
|
2014-10-09 01:43:13 +02:00
|
|
|
CidrBlock: d.Get("cidr_block").(string),
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] VPC create config: %#v", createOpts)
|
|
|
|
vpcResp, err := ec2conn.CreateVpc(createOpts)
|
|
|
|
if err != nil {
|
2014-10-09 01:43:13 +02:00
|
|
|
return fmt.Errorf("Error creating VPC: %s", err)
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
|
|
|
vpc := &vpcResp.VPC
|
2014-07-07 23:20:36 +02:00
|
|
|
log.Printf("[INFO] VPC ID: %s", vpc.VpcId)
|
2014-10-09 01:43:13 +02:00
|
|
|
d.SetId(vpc.VpcId)
|
|
|
|
|
|
|
|
// Set partial mode and say that we setup the cidr block
|
|
|
|
d.Partial(true)
|
|
|
|
d.SetPartial("cidr_block")
|
2014-07-07 21:16:58 +02:00
|
|
|
|
|
|
|
// Wait for the VPC to become available
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for VPC (%s) to become available",
|
2014-10-09 01:43:13 +02:00
|
|
|
d.Id())
|
2014-07-07 21:16:58 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "available",
|
2014-10-09 01:43:13 +02:00
|
|
|
Refresh: VPCStateRefreshFunc(ec2conn, d.Id()),
|
2014-07-07 21:16:58 +02:00
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
2014-07-07 21:16:58 +02:00
|
|
|
"Error waiting for VPC (%s) to become available: %s",
|
2014-10-09 01:43:13 +02:00
|
|
|
d.Id(), err)
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 21:16:58 +02:00
|
|
|
// Update our attributes and return
|
2014-10-09 01:43:13 +02:00
|
|
|
return resourceAwsVpcUpdate(d, meta)
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error {
|
2014-08-09 14:43:00 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
// Turn on partial mode
|
|
|
|
d.Partial(true)
|
|
|
|
defer d.Partial(false)
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
if d.HasChange("enable_dns_hostnames") {
|
2014-08-09 14:43:00 +02:00
|
|
|
options := new(ec2.ModifyVpcAttribute)
|
2014-10-09 01:43:13 +02:00
|
|
|
options.EnableDnsHostnames = d.Get("enable_dns_hostnames").(bool)
|
|
|
|
options.SetEnableDnsHostnames = true
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
log.Printf(
|
|
|
|
"[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v",
|
|
|
|
d.Id(), options)
|
|
|
|
if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil {
|
|
|
|
return err
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
|
|
|
|
d.SetPartial("enable_dns_hostnames")
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
if d.HasChange("enable_dns_support") {
|
2014-08-09 14:43:00 +02:00
|
|
|
options := new(ec2.ModifyVpcAttribute)
|
2014-10-09 01:43:13 +02:00
|
|
|
options.EnableDnsSupport = d.Get("enable_dns_support").(bool)
|
|
|
|
options.SetEnableDnsSupport = true
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
log.Printf(
|
|
|
|
"[INFO] Modifying enable_dns_support vpc attribute for %s: %#v",
|
|
|
|
d.Id(), options)
|
|
|
|
if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil {
|
|
|
|
return err
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
|
|
|
|
d.SetPartial("enable_dns_support")
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 02:54:00 +02:00
|
|
|
if err := setTags(ec2conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
return nil
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 21:16:58 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
log.Printf("[INFO] Deleting VPC: %s", d.Id())
|
|
|
|
if _, err := ec2conn.DeleteVpc(d.Id()); err != nil {
|
2014-07-08 02:07:31 +02:00
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
if ok && ec2err.Code == "InvalidVpcID.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:46:31 +02:00
|
|
|
return fmt.Errorf("Error deleting VPC: %s", err)
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
|
2014-07-07 21:16:58 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
// Refresh the VPC state
|
|
|
|
vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, d.Id())()
|
2014-07-07 21:16:58 +02:00
|
|
|
if err != nil {
|
2014-10-09 01:43:13 +02:00
|
|
|
return err
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
if vpcRaw == nil {
|
2014-10-09 01:43:13 +02:00
|
|
|
return nil
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
// VPC stuff
|
|
|
|
vpc := vpcRaw.(*ec2.VPC)
|
|
|
|
d.Set("cidr_block", vpc.CidrBlock)
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 02:54:00 +02:00
|
|
|
// Tags
|
|
|
|
d.Set("tags", tagsToMap(vpc.Tags))
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
// Attributes
|
|
|
|
resp, err := ec2conn.VpcAttribute(d.Id(), "enableDnsSupport")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
d.Set("enable_dns_support", resp.EnableDnsSupport)
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
resp, err = ec2conn.VpcAttribute(d.Id(), "enableDnsHostnames")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
d.Set("enable_dns_hostnames", resp.EnableDnsHostnames)
|
2014-07-07 21:16:58 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
return nil
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// VPCStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// a VPC.
|
|
|
|
func VPCStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.DescribeVpcs([]string{id}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
2014-07-07 21:23:04 +02:00
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidVpcID.NotFound" {
|
|
|
|
resp = nil
|
|
|
|
} else {
|
|
|
|
log.Printf("Error on VPCStateRefresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
// Sometimes AWS just has consistency issues and doesn't see
|
|
|
|
// our instance yet. Return an empty state.
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vpc := &resp.VPCs[0]
|
|
|
|
return vpc, vpc.State, nil
|
|
|
|
}
|
|
|
|
}
|