VPC Refactor

This commit is contained in:
Rahul Menon 2015-02-25 17:34:27 +05:30
parent 1472f976d7
commit 06b2d0bbf6
1 changed files with 64 additions and 37 deletions

View File

@ -181,20 +181,24 @@ func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
} }
func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEc2conn
// Turn on partial mode // Turn on partial mode
d.Partial(true) d.Partial(true)
vpcid := d.Id()
modifyOpts := &ec2.ModifyVPCAttributeRequest{
VPCID: &vpcid,
}
if d.HasChange("enable_dns_hostnames") { if d.HasChange("enable_dns_hostnames") {
options := new(ec2.ModifyVpcAttribute) val := d.Get("enable_dns_hostnames").(bool)
options.EnableDnsHostnames = d.Get("enable_dns_hostnames").(bool) modifyOpts.EnableDNSHostnames = &ec2.AttributeBooleanValue{
options.SetEnableDnsHostnames = true Value: &val,
}
log.Printf( log.Printf(
"[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v", "[INFO] Modifying enable_dns_hostnames vpc attribute for %s: %#v",
d.Id(), options) d.Id(), modifyOpts)
if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil { if err := ec2conn.ModifyVPCAttribute(modifyOpts); err != nil {
return err return err
} }
@ -202,36 +206,40 @@ func resourceAwsVpcUpdate(d *schema.ResourceData, meta interface{}) error {
} }
if d.HasChange("enable_dns_support") { if d.HasChange("enable_dns_support") {
options := new(ec2.ModifyVpcAttribute) val := d.Get("enable_dns_hostnames").(bool)
options.EnableDnsSupport = d.Get("enable_dns_support").(bool) modifyOpts.EnableDNSSupport = &ec2.AttributeBooleanValue{
options.SetEnableDnsSupport = true Value: &val,
}
log.Printf( log.Printf(
"[INFO] Modifying enable_dns_support vpc attribute for %s: %#v", "[INFO] Modifying enable_dns_support vpc attribute for %s: %#v",
d.Id(), options) d.Id(), modifyOpts)
if _, err := ec2conn.ModifyVpcAttribute(d.Id(), options); err != nil { if err := ec2conn.ModifyVPCAttribute(modifyOpts); err != nil {
return err return err
} }
d.SetPartial("enable_dns_support") d.SetPartial("enable_dns_support")
} }
//Tagging Support need to be worked on - rmenn
if err := setTags(ec2conn, d); err != nil { // if err := setTags(ec2conn, d); err != nil {
return err // return err
} else { // } else {
d.SetPartial("tags") // d.SetPartial("tags")
} // }
d.Partial(false) d.Partial(false)
return resourceAwsVpcRead(d, meta) return resourceAwsVpcRead(d, meta)
} }
func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEc2conn
vpcID := d.Id()
DeleteVpcOpts := &ec2.DeleteVPCRequest{
VPCID: &vpcID,
}
log.Printf("[INFO] Deleting VPC: %s", d.Id()) log.Printf("[INFO] Deleting VPC: %s", d.Id())
if _, err := ec2conn.DeleteVpc(d.Id()); err != nil { if err := ec2conn.DeleteVPC(DeleteVpcOpts); err != nil {
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(*awsGo.APIError)
if ok && ec2err.Code == "InvalidVpcID.NotFound" { if ok && ec2err.Code == "InvalidVpcID.NotFound" {
return nil return nil
} }
@ -246,9 +254,12 @@ func resourceAwsVpcDelete(d *schema.ResourceData, meta interface{}) error {
// a VPC. // a VPC.
func VPCStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { func VPCStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
resp, err := conn.DescribeVpcs([]string{id}, ec2.NewFilter()) DescribeVpcOpts := &ec2.DescribeVPCsRequest{
VPCIDs: []string{id},
}
resp, err := conn.DescribeVPCs(DescribeVpcOpts)
if err != nil { if err != nil {
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidVpcID.NotFound" { if ec2err, ok := err.(*awsGo.APIError); ok && ec2err.Code == "InvalidVpcID.NotFound" {
resp = nil resp = nil
} else { } else {
log.Printf("Error on VPCStateRefresh: %s", err) log.Printf("Error on VPCStateRefresh: %s", err)
@ -263,37 +274,53 @@ func VPCStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
} }
vpc := &resp.VPCs[0] vpc := &resp.VPCs[0]
return vpc, vpc.State, nil return vpc, *vpc.State, nil
} }
} }
func resourceAwsVpcSetDefaultNetworkAcl(conn *ec2.EC2, d *schema.ResourceData) error { func resourceAwsVpcSetDefaultNetworkAcl(conn *ec2.EC2, d *schema.ResourceData) error {
filter := ec2.NewFilter() filter1 := &ec2.Filter{
filter.Add("default", "true") Name: awsGo.String("default"),
filter.Add("vpc-id", d.Id()) Values: []string{("true")},
networkAclResp, err := conn.NetworkAcls(nil, filter) }
filter2 := &ec2.Filter{
Name: awsGo.String("vpc-id"),
Values: []string{(d.Id())},
}
DescribeNetworkACLOpts := &ec2.DescribeNetworkACLsRequest{
Filters: []ec2.Filter{*filter1, *filter2},
}
networkAclResp, err := conn.DescribeNetworkACLs(DescribeNetworkACLOpts)
if err != nil { if err != nil {
return err return err
} }
if v := networkAclResp.NetworkAcls; len(v) > 0 { if v := networkAclResp.NetworkACLs; len(v) > 0 {
d.Set("default_network_acl_id", v[0].NetworkAclId) d.Set("default_network_acl_id", v[0].NetworkACLID)
} }
return nil return nil
} }
func resourceAwsVpcSetDefaultSecurityGroup(conn *ec2.EC2, d *schema.ResourceData) error { func resourceAwsVpcSetDefaultSecurityGroup(conn *ec2.EC2, d *schema.ResourceData) error {
filter := ec2.NewFilter() filter1 := &ec2.Filter{
filter.Add("group-name", "default") Name: awsGo.String("group-name"),
filter.Add("vpc-id", d.Id()) Values: []string{("default")},
securityGroupResp, err := conn.SecurityGroups(nil, filter) }
filter2 := &ec2.Filter{
Name: awsGo.String("vpc-id"),
Values: []string{(d.Id())},
}
DescribeSgOpts := &ec2.DescribeSecurityGroupsRequest{
Filters: []ec2.Filter{*filter1, *filter2},
}
securityGroupResp, err := conn.DescribeSecurityGroups(DescribeSgOpts)
if err != nil { if err != nil {
return err return err
} }
if v := securityGroupResp.Groups; len(v) > 0 { if v := securityGroupResp.SecurityGroups; len(v) > 0 {
d.Set("default_security_group_id", v[0].Id) d.Set("default_security_group_id", v[0].GroupID)
} }
return nil return nil