github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/providers/aws/resource_aws_vpc.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/diff" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/mitchellh/goamz/ec2" 12 ) 13 14 func resource_aws_vpc_create( 15 s *terraform.ResourceState, 16 d *terraform.ResourceDiff, 17 meta interface{}) (*terraform.ResourceState, error) { 18 p := meta.(*ResourceProvider) 19 ec2conn := p.ec2conn 20 21 // Merge the diff so that we have all the proper attributes 22 s = s.MergeDiff(d) 23 24 // Create the VPC 25 createOpts := &ec2.CreateVpc{ 26 CidrBlock: s.Attributes["cidr_block"], 27 } 28 log.Printf("[DEBUG] VPC create config: %#v", createOpts) 29 vpcResp, err := ec2conn.CreateVpc(createOpts) 30 if err != nil { 31 return nil, fmt.Errorf("Error creating VPC: %s", err) 32 } 33 34 // Get the ID and store it 35 vpc := &vpcResp.VPC 36 log.Printf("[INFO] VPC ID: %s", vpc.VpcId) 37 s.ID = vpc.VpcId 38 39 // Wait for the VPC to become available 40 log.Printf( 41 "[DEBUG] Waiting for VPC (%s) to become available", 42 s.ID) 43 stateConf := &resource.StateChangeConf{ 44 Pending: []string{"pending"}, 45 Target: "available", 46 Refresh: VPCStateRefreshFunc(ec2conn, s.ID), 47 Timeout: 10 * time.Minute, 48 } 49 vpcRaw, err := stateConf.WaitForState() 50 if err != nil { 51 return s, fmt.Errorf( 52 "Error waiting for VPC (%s) to become available: %s", 53 s.ID, err) 54 } 55 56 // Update our attributes and return 57 return resource_aws_vpc_update_state(s, vpcRaw.(*ec2.VPC)) 58 } 59 60 func resource_aws_vpc_update( 61 s *terraform.ResourceState, 62 d *terraform.ResourceDiff, 63 meta interface{}) (*terraform.ResourceState, error) { 64 // This should never be called because we have no update-able 65 // attributes 66 panic("Update for VPC is not supported") 67 68 return nil, nil 69 } 70 71 func resource_aws_vpc_destroy( 72 s *terraform.ResourceState, 73 meta interface{}) error { 74 p := meta.(*ResourceProvider) 75 ec2conn := p.ec2conn 76 77 log.Printf("[INFO] Deleting VPC: %s", s.ID) 78 if _, err := ec2conn.DeleteVpc(s.ID); err != nil { 79 ec2err, ok := err.(*ec2.Error) 80 if ok && ec2err.Code == "InvalidVpcID.NotFound" { 81 return nil 82 } 83 84 return fmt.Errorf("Error deleting VPC: %s", err) 85 } 86 87 return nil 88 } 89 90 func resource_aws_vpc_refresh( 91 s *terraform.ResourceState, 92 meta interface{}) (*terraform.ResourceState, error) { 93 p := meta.(*ResourceProvider) 94 ec2conn := p.ec2conn 95 96 vpcRaw, _, err := VPCStateRefreshFunc(ec2conn, s.ID)() 97 if err != nil { 98 return s, err 99 } 100 if vpcRaw == nil { 101 return nil, nil 102 } 103 104 vpc := vpcRaw.(*ec2.VPC) 105 return resource_aws_vpc_update_state(s, vpc) 106 } 107 108 func resource_aws_vpc_diff( 109 s *terraform.ResourceState, 110 c *terraform.ResourceConfig, 111 meta interface{}) (*terraform.ResourceDiff, error) { 112 b := &diff.ResourceBuilder{ 113 Attrs: map[string]diff.AttrType{ 114 "cidr_block": diff.AttrTypeCreate, 115 }, 116 } 117 118 return b.Diff(s, c) 119 } 120 121 func resource_aws_vpc_update_state( 122 s *terraform.ResourceState, 123 vpc *ec2.VPC) (*terraform.ResourceState, error) { 124 s.Attributes["cidr_block"] = vpc.CidrBlock 125 return s, nil 126 } 127 128 // VPCStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 129 // a VPC. 130 func VPCStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { 131 return func() (interface{}, string, error) { 132 resp, err := conn.DescribeVpcs([]string{id}, ec2.NewFilter()) 133 if err != nil { 134 if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidVpcID.NotFound" { 135 resp = nil 136 } else { 137 log.Printf("Error on VPCStateRefresh: %s", err) 138 return nil, "", err 139 } 140 } 141 142 if resp == nil { 143 // Sometimes AWS just has consistency issues and doesn't see 144 // our instance yet. Return an empty state. 145 return nil, "", nil 146 } 147 148 vpc := &resp.VPCs[0] 149 return vpc, vpc.State, nil 150 } 151 }