2014-07-07 21:16:58 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-04-10 23:09:12 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-20 13:21:23 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
2015-04-10 23:09:12 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2014-07-07 21:16:58 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
2014-12-10 11:39:14 +01:00
|
|
|
"instance_tenancy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
"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
|
|
|
|
2014-10-11 01:57:32 +02:00
|
|
|
"main_route_table_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-12-22 18:38:15 +01:00
|
|
|
"default_network_acl_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-04-28 21:57:05 +02:00
|
|
|
"dhcp_options_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-12-23 07:13:33 +01:00
|
|
|
"default_security_group_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-03-09 10:09:06 +01:00
|
|
|
"tags": tagsSchema(),
|
2014-10-09 01:43:13 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-25 10:34:17 +01:00
|
|
|
instance_tenancy := "default"
|
|
|
|
if v, ok := d.GetOk("instance_tenancy"); ok {
|
|
|
|
instance_tenancy = v.(string)
|
|
|
|
}
|
2014-07-07 21:16:58 +02:00
|
|
|
// Create the VPC
|
2015-04-10 23:09:12 +02:00
|
|
|
createOpts := &ec2.CreateVPCInput{
|
2015-02-25 10:34:17 +01:00
|
|
|
CIDRBlock: aws.String(d.Get("cidr_block").(string)),
|
2015-03-12 21:13:45 +01:00
|
|
|
InstanceTenancy: aws.String(instance_tenancy),
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
2015-02-25 10:34:17 +01:00
|
|
|
log.Printf("[DEBUG] VPC create config: %#v", *createOpts)
|
2015-04-10 23:09:12 +02:00
|
|
|
vpcResp, err := conn.CreateVPC(createOpts)
|
2014-07-07 21:16:58 +02:00
|
|
|
if err != nil {
|
2015-03-12 21:13:45 +01:00
|
|
|
return fmt.Errorf("Error creating VPC: %s", err)
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
2015-02-25 10:34:17 +01:00
|
|
|
vpc := vpcResp.VPC
|
|
|
|
d.SetId(*vpc.VPCID)
|
|
|
|
log.Printf("[INFO] VPC ID: %s", d.Id())
|
2014-10-09 01:43:13 +02:00
|
|
|
|
|
|
|
// 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",
|
2015-04-10 23:09:12 +02:00
|
|
|
Refresh: VPCStateRefreshFunc(conn, 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-11-21 17:58:34 +01:00
|
|
|
func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
// Refresh the VPC state
|
2015-04-10 23:09:12 +02:00
|
|
|
vpcRaw, _, err := VPCStateRefreshFunc(conn, d.Id())()
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vpcRaw == nil {
|
2015-02-20 19:20:20 +01:00
|
|
|
d.SetId("")
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VPC stuff
|
|
|
|
vpc := vpcRaw.(*ec2.VPC)
|
2015-02-25 10:34:17 +01:00
|
|
|
vpcid := d.Id()
|
|
|
|
d.Set("cidr_block", vpc.CIDRBlock)
|
2015-04-28 21:57:05 +02:00
|
|
|
d.Set("dhcp_options_id", vpc.DHCPOptionsID)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2015-03-04 14:07:30 +01:00
|
|
|
// Tags
|
2015-05-12 21:58:10 +02:00
|
|
|
d.Set("tags", tagsToMap(vpc.Tags))
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
// Attributes
|
2015-02-25 10:34:17 +01:00
|
|
|
attribute := "enableDnsSupport"
|
2015-04-10 23:09:12 +02:00
|
|
|
DescribeAttrOpts := &ec2.DescribeVPCAttributeInput{
|
2015-03-12 21:13:45 +01:00
|
|
|
Attribute: aws.String(attribute),
|
|
|
|
VPCID: aws.String(vpcid),
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
2015-04-10 23:09:12 +02:00
|
|
|
resp, err := conn.DescribeVPCAttribute(DescribeAttrOpts)
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-25 10:34:17 +01:00
|
|
|
d.Set("enable_dns_support", *resp.EnableDNSSupport)
|
|
|
|
attribute = "enableDnsHostnames"
|
2015-04-10 23:09:12 +02:00
|
|
|
DescribeAttrOpts = &ec2.DescribeVPCAttributeInput{
|
2015-02-25 10:34:17 +01:00
|
|
|
Attribute: &attribute,
|
|
|
|
VPCID: &vpcid,
|
|
|
|
}
|
2015-04-10 23:09:12 +02:00
|
|
|
resp, err = conn.DescribeVPCAttribute(DescribeAttrOpts)
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-25 10:34:17 +01:00
|
|
|
d.Set("enable_dns_hostnames", *resp.EnableDNSHostnames)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
// Get the main routing table for this VPC
|
2015-02-25 10:34:17 +01:00
|
|
|
// Really Ugly need to make this better - rmenn
|
|
|
|
filter1 := &ec2.Filter{
|
|
|
|
Name: aws.String("association.main"),
|
2015-04-10 23:09:12 +02:00
|
|
|
Values: []*string{aws.String("true")},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
|
|
|
filter2 := &ec2.Filter{
|
|
|
|
Name: aws.String("vpc-id"),
|
2015-04-10 23:09:12 +02:00
|
|
|
Values: []*string{aws.String(d.Id())},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
2015-04-10 23:09:12 +02:00
|
|
|
DescribeRouteOpts := &ec2.DescribeRouteTablesInput{
|
|
|
|
Filters: []*ec2.Filter{filter1, filter2},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
2015-04-10 23:09:12 +02:00
|
|
|
routeResp, err := conn.DescribeRouteTables(DescribeRouteOpts)
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v := routeResp.RouteTables; len(v) > 0 {
|
2015-02-25 10:34:17 +01:00
|
|
|
d.Set("main_route_table_id", *v[0].RouteTableID)
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 23:09:12 +02:00
|
|
|
resourceAwsVpcSetDefaultNetworkAcl(conn, d)
|
|
|
|
resourceAwsVpcSetDefaultSecurityGroup(conn, d)
|
2014-12-22 18:38:15 +01:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
// Turn on partial mode
|
|
|
|
d.Partial(true)
|
2015-02-25 10:34:17 +01:00
|
|
|
vpcid := d.Id()
|
2014-10-09 01:43:13 +02:00
|
|
|
if d.HasChange("enable_dns_hostnames") {
|
2015-02-25 10:34:17 +01:00
|
|
|
val := d.Get("enable_dns_hostnames").(bool)
|
2015-04-10 23:09:12 +02:00
|
|
|
modifyOpts := &ec2.ModifyVPCAttributeInput{
|
2015-03-25 21:15:32 +01:00
|
|
|
VPCID: &vpcid,
|
|
|
|
EnableDNSHostnames: &ec2.AttributeBooleanValue{
|
|
|
|
Value: &val,
|
|
|
|
},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
2014-08-09 14:43:00 +02:00
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
log.Printf(
|
2015-03-20 19:44:42 +01:00
|
|
|
"[INFO] Modifying enable_dns_support vpc attribute for %s: %#v",
|
2015-02-25 10:34:17 +01:00
|
|
|
d.Id(), modifyOpts)
|
2015-04-10 23:09:12 +02:00
|
|
|
if _, err := conn.ModifyVPCAttribute(modifyOpts); err != nil {
|
2014-10-09 01:43:13 +02:00
|
|
|
return err
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
|
2015-03-20 19:44:42 +01:00
|
|
|
d.SetPartial("enable_dns_support")
|
2014-08-09 14:43:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
if d.HasChange("enable_dns_support") {
|
2015-03-25 21:15:32 +01:00
|
|
|
val := d.Get("enable_dns_support").(bool)
|
2015-04-10 23:09:12 +02:00
|
|
|
modifyOpts := &ec2.ModifyVPCAttributeInput{
|
2015-03-25 21:15:32 +01:00
|
|
|
VPCID: &vpcid,
|
|
|
|
EnableDNSSupport: &ec2.AttributeBooleanValue{
|
|
|
|
Value: &val,
|
|
|
|
},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
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",
|
2015-02-25 10:34:17 +01:00
|
|
|
d.Id(), modifyOpts)
|
2015-04-10 23:09:12 +02:00
|
|
|
if _, err := conn.ModifyVPCAttribute(modifyOpts); err != nil {
|
2014-10-09 01:43:13 +02:00
|
|
|
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
|
|
|
}
|
2015-03-04 14:07:30 +01:00
|
|
|
|
2015-05-12 21:58:10 +02:00
|
|
|
if err := setTags(conn, d); err != nil {
|
2015-03-04 14:07:30 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
2014-10-09 02:54:00 +02:00
|
|
|
|
2014-10-11 01:57:32 +02:00
|
|
|
d.Partial(false)
|
|
|
|
return resourceAwsVpcRead(d, meta)
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-25 10:34:17 +01:00
|
|
|
vpcID := d.Id()
|
2015-04-10 23:09:12 +02:00
|
|
|
DeleteVpcOpts := &ec2.DeleteVPCInput{
|
2015-02-25 10:34:17 +01:00
|
|
|
VPCID: &vpcID,
|
|
|
|
}
|
2014-10-09 01:43:13 +02:00
|
|
|
log.Printf("[INFO] Deleting VPC: %s", d.Id())
|
2015-04-10 23:09:12 +02:00
|
|
|
if _, err := conn.DeleteVPC(DeleteVpcOpts); err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
|
|
|
if ok && ec2err.Code() == "InvalidVpcID.NotFound" {
|
2014-07-08 02:07:31 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2015-04-10 23:09:12 +02:00
|
|
|
DescribeVpcOpts := &ec2.DescribeVPCsInput{
|
|
|
|
VPCIDs: []*string{aws.String(id)},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeVPCs(DescribeVpcOpts)
|
2014-07-07 21:16:58 +02:00
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpcID.NotFound" {
|
2014-07-07 21:23:04 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-10 23:09:12 +02:00
|
|
|
vpc := resp.VPCs[0]
|
2015-02-25 10:34:17 +01:00
|
|
|
return vpc, *vpc.State, nil
|
2014-07-07 21:16:58 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 18:38:15 +01:00
|
|
|
|
2015-02-20 19:20:20 +01:00
|
|
|
func resourceAwsVpcSetDefaultNetworkAcl(conn *ec2.EC2, d *schema.ResourceData) error {
|
2015-02-25 10:34:17 +01:00
|
|
|
filter1 := &ec2.Filter{
|
|
|
|
Name: aws.String("default"),
|
2015-04-10 23:09:12 +02:00
|
|
|
Values: []*string{aws.String("true")},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
|
|
|
filter2 := &ec2.Filter{
|
|
|
|
Name: aws.String("vpc-id"),
|
2015-04-10 23:09:12 +02:00
|
|
|
Values: []*string{aws.String(d.Id())},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
2015-04-10 23:09:12 +02:00
|
|
|
DescribeNetworkACLOpts := &ec2.DescribeNetworkACLsInput{
|
|
|
|
Filters: []*ec2.Filter{filter1, filter2},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
|
|
|
networkAclResp, err := conn.DescribeNetworkACLs(DescribeNetworkACLOpts)
|
2014-12-22 18:38:15 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-25 10:34:17 +01:00
|
|
|
if v := networkAclResp.NetworkACLs; len(v) > 0 {
|
|
|
|
d.Set("default_network_acl_id", v[0].NetworkACLID)
|
2014-12-22 18:38:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-23 07:13:33 +01:00
|
|
|
|
2015-02-20 19:20:20 +01:00
|
|
|
func resourceAwsVpcSetDefaultSecurityGroup(conn *ec2.EC2, d *schema.ResourceData) error {
|
2015-02-25 10:34:17 +01:00
|
|
|
filter1 := &ec2.Filter{
|
|
|
|
Name: aws.String("group-name"),
|
2015-04-10 23:09:12 +02:00
|
|
|
Values: []*string{aws.String("default")},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
|
|
|
filter2 := &ec2.Filter{
|
|
|
|
Name: aws.String("vpc-id"),
|
2015-04-10 23:09:12 +02:00
|
|
|
Values: []*string{aws.String(d.Id())},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
2015-04-10 23:09:12 +02:00
|
|
|
DescribeSgOpts := &ec2.DescribeSecurityGroupsInput{
|
|
|
|
Filters: []*ec2.Filter{filter1, filter2},
|
2015-02-25 10:34:17 +01:00
|
|
|
}
|
|
|
|
securityGroupResp, err := conn.DescribeSecurityGroups(DescribeSgOpts)
|
2014-12-23 07:13:33 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-25 10:34:17 +01:00
|
|
|
if v := securityGroupResp.SecurityGroups; len(v) > 0 {
|
|
|
|
d.Set("default_security_group_id", v[0].GroupID)
|
2014-12-23 07:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|