providers/aws: convert aws_vpc to helper/schema

This commit is contained in:
Mitchell Hashimoto 2014-10-08 16:43:13 -07:00
parent 0db9d98fff
commit 699b2efa1c
5 changed files with 99 additions and 137 deletions

View File

@ -46,6 +46,7 @@ func Provider() *schema.Provider {
"aws_instance": resourceAwsInstance(), "aws_instance": resourceAwsInstance(),
"aws_security_group": resourceAwsSecurityGroup(), "aws_security_group": resourceAwsSecurityGroup(),
"aws_db_subnet_group": resourceAwsDbSubnetGroup(), "aws_db_subnet_group": resourceAwsDbSubnetGroup(),
"aws_vpc": resourceAwsVpc(),
}, },
} }
} }

View File

@ -117,6 +117,7 @@ func resourceAwsInstance() *schema.Resource {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
}, },
"iam_instance_profile": &schema.Schema{ "iam_instance_profile": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
ForceNew: true, ForceNew: true,

View File

@ -3,142 +3,133 @@ package aws
import ( import (
"fmt" "fmt"
"log" "log"
"strconv"
"time" "time"
"github.com/hashicorp/terraform/helper/diff" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
) )
func resource_aws_vpc_create( func resourceAwsVpc() *schema.Resource {
s *terraform.InstanceState, return &schema.Resource{
d *terraform.InstanceDiff, Create: resourceAwsVpcCreate,
meta interface{}) (*terraform.InstanceState, error) { 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,
},
},
}
}
func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
// Merge the diff so that we have all the proper attributes
s = s.MergeDiff(d)
// Create the VPC // Create the VPC
createOpts := &ec2.CreateVpc{ createOpts := &ec2.CreateVpc{
CidrBlock: s.Attributes["cidr_block"], CidrBlock: d.Get("cidr_block").(string),
} }
log.Printf("[DEBUG] VPC create config: %#v", createOpts) log.Printf("[DEBUG] VPC create config: %#v", createOpts)
vpcResp, err := ec2conn.CreateVpc(createOpts) vpcResp, err := ec2conn.CreateVpc(createOpts)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error creating VPC: %s", err) return fmt.Errorf("Error creating VPC: %s", err)
} }
// Get the ID and store it // Get the ID and store it
vpc := &vpcResp.VPC vpc := &vpcResp.VPC
log.Printf("[INFO] VPC ID: %s", vpc.VpcId) log.Printf("[INFO] VPC ID: %s", vpc.VpcId)
s.ID = vpc.VpcId d.SetId(vpc.VpcId)
// Set partial mode and say that we setup the cidr block
d.Partial(true)
d.SetPartial("cidr_block")
// Wait for the VPC to become available // Wait for the VPC to become available
log.Printf( log.Printf(
"[DEBUG] Waiting for VPC (%s) to become available", "[DEBUG] Waiting for VPC (%s) to become available",
s.ID) d.Id())
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"pending"}, Pending: []string{"pending"},
Target: "available", Target: "available",
Refresh: VPCStateRefreshFunc(ec2conn, s.ID), Refresh: VPCStateRefreshFunc(ec2conn, d.Id()),
Timeout: 10 * time.Minute, Timeout: 10 * time.Minute,
} }
vpcRaw, err := stateConf.WaitForState() if _, err := stateConf.WaitForState(); err != nil {
if err != nil { return fmt.Errorf(
return s, fmt.Errorf(
"Error waiting for VPC (%s) to become available: %s", "Error waiting for VPC (%s) to become available: %s",
s.ID, err) d.Id(), err)
}
if attr, ok := d.Attributes["enable_dns_support"]; ok {
options := new(ec2.ModifyVpcAttribute)
options.EnableDnsSupport = attr.New != "" && attr.New != "false"
options.SetEnableDnsSupport = true
s.Attributes["enable_dns_support"] = strconv.FormatBool(options.EnableDnsSupport)
log.Printf("[INFO] Modifying vpc attributes for %s: %#v", s.ID, options)
if _, err := ec2conn.ModifyVpcAttribute(s.ID, options); err != nil {
return s, err
}
}
if attr, ok := d.Attributes["enable_dns_hostnames"]; ok {
options := new(ec2.ModifyVpcAttribute)
options.EnableDnsHostnames = attr.New != "" && attr.New != "false"
options.SetEnableDnsHostnames = true
s.Attributes["enable_dns_hostnames"] = strconv.FormatBool(options.EnableDnsHostnames)
log.Printf("[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v", s.ID, options)
if _, err := ec2conn.ModifyVpcAttribute(s.ID, options); err != nil {
return s, err
}
} }
// Update our attributes and return // Update our attributes and return
return resource_aws_vpc_update_state(s, vpcRaw.(*ec2.VPC)) return resourceAwsVpcUpdate(d, meta)
} }
func resource_aws_vpc_update( func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error {
s *terraform.InstanceState,
d *terraform.InstanceDiff,
meta interface{}) (*terraform.InstanceState, error) {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
rs := s.MergeDiff(d)
log.Printf("[DEBUG] attributes: %#v", d.Attributes) // Turn on partial mode
d.Partial(true)
defer d.Partial(false)
if attr, ok := d.Attributes["enable_dns_support"]; ok { if d.HasChange("enable_dns_hostnames") {
options := new(ec2.ModifyVpcAttribute) options := new(ec2.ModifyVpcAttribute)
options.EnableDnsHostnames = d.Get("enable_dns_hostnames").(bool)
options.EnableDnsSupport = attr.New != "" && attr.New != "false"
options.SetEnableDnsSupport = true
rs.Attributes["enable_dns_support"] = strconv.FormatBool(options.EnableDnsSupport)
log.Printf("[INFO] Modifying enable_dns_support vpc attribute for %s: %#v", s.ID, options)
if _, err := ec2conn.ModifyVpcAttribute(s.ID, options); err != nil {
return s, err
}
}
if attr, ok := d.Attributes["enable_dns_hostnames"]; ok {
options := new(ec2.ModifyVpcAttribute)
options.EnableDnsHostnames = attr.New != "" && attr.New != "false"
options.SetEnableDnsHostnames = true options.SetEnableDnsHostnames = true
rs.Attributes["enable_dns_hostnames"] = strconv.FormatBool(options.EnableDnsHostnames) log.Printf(
"[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v",
log.Printf("[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v", s.ID, options) d.Id(), options)
if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil {
if _, err := ec2conn.ModifyVpcAttribute(s.ID, options); err != nil { return err
return s, err
} }
d.SetPartial("enable_dns_hostnames")
} }
return rs, nil if d.HasChange("enable_dns_support") {
options := new(ec2.ModifyVpcAttribute)
options.EnableDnsSupport = d.Get("enable_dns_support").(bool)
options.SetEnableDnsSupport = true
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
}
d.SetPartial("enable_dns_support")
}
return nil
} }
func resource_aws_vpc_destroy(
s *terraform.InstanceState, func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error {
meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
log.Printf("[INFO] Deleting VPC: %s", s.ID) log.Printf("[INFO] Deleting VPC: %s", d.Id())
if _, err := ec2conn.DeleteVpc(s.ID); err != nil { if _, err := ec2conn.DeleteVpc(d.Id()); err != nil {
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(*ec2.Error)
if ok && ec2err.Code == "InvalidVpcID.NotFound" { if ok && ec2err.Code == "InvalidVpcID.NotFound" {
return nil return nil
@ -150,60 +141,37 @@ func resource_aws_vpc_destroy(
return nil return nil
} }
func resource_aws_vpc_refresh( func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
s *terraform.InstanceState,
meta interface{}) (*terraform.InstanceState, error) {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, s.ID)() // Refresh the VPC state
vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, d.Id())()
if err != nil { if err != nil {
return s, err return err
} }
if vpcRaw == nil { if vpcRaw == nil {
return nil, nil return nil
} }
if dnsSupportResp, err := ec2conn.VpcAttribute(s.ID, "enableDnsSupport"); err != nil { // VPC stuff
return s, err vpc := vpcRaw.(*ec2.VPC)
} else { d.Set("cidr_block", vpc.CidrBlock)
s.Attributes["enable_dns_support"] = strconv.FormatBool(dnsSupportResp.EnableDnsSupport)
// Attributes
resp, err := ec2conn.VpcAttribute(d.Id(), "enableDnsSupport")
if err != nil {
return err
} }
d.Set("enable_dns_support", resp.EnableDnsSupport)
if dnsHostnamesResp, err := ec2conn.VpcAttribute(s.ID, "enableDnsHostnames"); err != nil { resp, err = ec2conn.VpcAttribute(d.Id(), "enableDnsHostnames")
return s, err if err != nil {
} else { return err
s.Attributes["enable_dns_hostnames"] = strconv.FormatBool(dnsHostnamesResp.EnableDnsHostnames)
} }
d.Set("enable_dns_hostnames", resp.EnableDnsHostnames)
return resource_aws_vpc_update_state(s, vpcRaw.(*ec2.VPC)) return nil
}
func resource_aws_vpc_diff(
s *terraform.InstanceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.InstanceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"cidr_block": diff.AttrTypeCreate,
"enable_dns_support": diff.AttrTypeUpdate,
"enable_dns_hostnames": diff.AttrTypeUpdate,
},
ComputedAttrs: []string{
"enable_dns_support",
"enable_dns_hostnames",
},
}
return b.Diff(s, c)
}
func resource_aws_vpc_update_state(
s *terraform.InstanceState,
vpc *ec2.VPC) (*terraform.InstanceState, error) {
s.Attributes["cidr_block"] = vpc.CidrBlock
return s, nil
} }
// VPCStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // VPCStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch

View File

@ -9,7 +9,7 @@ import (
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
) )
func TestAccVpc(t *testing.T) { func TestAccVpc_basic(t *testing.T) {
var vpc ec2.VPC var vpc ec2.VPC
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{

View File

@ -126,14 +126,6 @@ func init() {
Diff: resource_aws_subnet_diff, Diff: resource_aws_subnet_diff,
Refresh: resource_aws_subnet_refresh, Refresh: resource_aws_subnet_refresh,
}, },
"aws_vpc": resource.Resource{
Create: resource_aws_vpc_create,
Destroy: resource_aws_vpc_destroy,
Diff: resource_aws_vpc_diff,
Refresh: resource_aws_vpc_refresh,
Update: resource_aws_vpc_update,
},
}, },
} }
} }