First refactor

This commit is contained in:
Rahul Menon 2015-02-25 16:17:55 +05:30
parent 38a8232e9a
commit 1472f976d7
1 changed files with 49 additions and 25 deletions

View File

@ -5,9 +5,10 @@ import (
"log" "log"
"time" "time"
awsGo "github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/ec2"
) )
func resourceAwsVpc() *schema.Resource { func resourceAwsVpc() *schema.Resource {
@ -57,29 +58,33 @@ func resourceAwsVpc() *schema.Resource {
Computed: true, Computed: true,
}, },
"tags": tagsSchema(), // "tags": tagsSchema(),
}, },
} }
} }
func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEc2conn
cidr := d.Get("cidr_block").(string)
instance_tenancy := "default"
if v := d.Get("instance_tenancy"); v != nil {
instance_tenancy = v.(string)
}
// Create the VPC // Create the VPC
createOpts := &ec2.CreateVpc{ createOpts := &ec2.CreateVPCRequest{
CidrBlock: d.Get("cidr_block").(string), CIDRBlock: &cidr,
InstanceTenancy: d.Get("instance_tenancy").(string), InstanceTenancy: &instance_tenancy,
} }
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 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) d.SetId(*vpc.VPCID)
d.SetId(vpc.VpcId) log.Printf("[INFO] VPC ID: %s", d.Id())
// Set partial mode and say that we setup the cidr block // Set partial mode and say that we setup the cidr block
d.Partial(true) d.Partial(true)
@ -106,7 +111,7 @@ func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
} }
func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEc2conn
// Refresh the VPC state // Refresh the VPC state
vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, d.Id())() vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, d.Id())()
@ -120,34 +125,53 @@ func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
// VPC stuff // VPC stuff
vpc := vpcRaw.(*ec2.VPC) vpc := vpcRaw.(*ec2.VPC)
d.Set("cidr_block", vpc.CidrBlock) vpcid := d.Id()
d.Set("cidr_block", vpc.CIDRBlock)
// Tags // Tags - TBD rmenn
d.Set("tags", tagsToMap(vpc.Tags)) //d.Set("tags", tagsToMap(vpc.Tags))
// Attributes // Attributes
resp, err := ec2conn.VpcAttribute(d.Id(), "enableDnsSupport") attribute := "enableDnsSupport"
DescribeAttrOpts := &ec2.DescribeVPCAttributeRequest{
Attribute: &attribute,
VPCID: &vpcid,
}
resp, err := ec2conn.DescribeVPCAttribute(DescribeAttrOpts)
if err != nil { if err != nil {
return err return err
} }
d.Set("enable_dns_support", resp.EnableDnsSupport) d.Set("enable_dns_support", *resp.EnableDNSSupport)
attribute = "enableDnsHostnames"
resp, err = ec2conn.VpcAttribute(d.Id(), "enableDnsHostnames") DescribeAttrOpts = &ec2.DescribeVPCAttributeRequest{
Attribute: &attribute,
VPCID: &vpcid,
}
resp, err = ec2conn.DescribeVPCAttribute(DescribeAttrOpts)
if err != nil { if err != nil {
return err return err
} }
d.Set("enable_dns_hostnames", resp.EnableDnsHostnames) d.Set("enable_dns_hostnames", *resp.EnableDNSHostnames)
// Get the main routing table for this VPC // Get the main routing table for this VPC
filter := ec2.NewFilter() // Really Ugly need to make this better - rmenn
filter.Add("association.main", "true") filter1 := &ec2.Filter{
filter.Add("vpc-id", d.Id()) Name: awsGo.String("association.main"),
routeResp, err := ec2conn.DescribeRouteTables(nil, filter) Values: []string{("true")},
}
filter2 := &ec2.Filter{
Name: awsGo.String("VPCID"),
Values: []string{(d.Id())},
}
DescribeRouteOpts := &ec2.DescribeRouteTablesRequest{
Filters: []ec2.Filter{*filter1, *filter2},
}
routeResp, err := ec2conn.DescribeRouteTables(DescribeRouteOpts)
if err != nil { if err != nil {
return err return err
} }
if v := routeResp.RouteTables; len(v) > 0 { if v := routeResp.RouteTables; len(v) > 0 {
d.Set("main_route_table_id", v[0].RouteTableId) d.Set("main_route_table_id", *v[0].RouteTableID)
} }
resourceAwsVpcSetDefaultNetworkAcl(ec2conn, d) resourceAwsVpcSetDefaultNetworkAcl(ec2conn, d)