terraform/builtin/providers/aws/resource_aws_subnet.go

196 lines
5.2 KiB
Go
Raw Normal View History

2014-07-07 23:46:02 +02:00
package aws
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
)
func resource_aws_subnet_create(
2014-09-17 02:44:42 +02:00
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
d *terraform.InstanceDiff,
2014-09-17 02:44:42 +02:00
meta interface{}) (*terraform.InstanceState, error) {
2014-07-07 23:46:02 +02:00
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
// Merge the diff so that we have all the proper attributes
s = s.MergeDiff(d)
// Create the Subnet
createOpts := &ec2.CreateSubnet{
AvailabilityZone: s.Attributes["availability_zone"],
CidrBlock: s.Attributes["cidr_block"],
VpcId: s.Attributes["vpc_id"],
}
log.Printf("[DEBUG] Subnet create config: %#v", createOpts)
resp, err := ec2conn.CreateSubnet(createOpts)
if err != nil {
return nil, fmt.Errorf("Error creating subnet: %s", err)
2014-07-07 23:46:02 +02:00
}
// Get the ID and store it
subnet := &resp.Subnet
s.ID = subnet.SubnetId
log.Printf("[INFO] Subnet ID: %s", s.ID)
// Wait for the Subnet to become available
log.Printf(
"[DEBUG] Waiting for subnet (%s) to become available",
s.ID)
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: "available",
Refresh: SubnetStateRefreshFunc(ec2conn, s.ID),
Timeout: 10 * time.Minute,
}
subnetRaw, err := stateConf.WaitForState()
if err != nil {
return s, fmt.Errorf(
"Error waiting for subnet (%s) to become available: %s",
s.ID, err)
}
2014-09-10 07:13:16 +02:00
// Map public ip on launch must be set in another API call
if attr := s.Attributes["map_public_ip_on_launch"]; attr == "true" {
2014-09-10 07:13:16 +02:00
modifyOpts := &ec2.ModifySubnetAttribute{
SubnetId: s.ID,
2014-09-10 07:13:16 +02:00
MapPublicIpOnLaunch: true,
}
log.Printf("[DEBUG] Subnet modify attributes: %#v", modifyOpts)
_, err := ec2conn.ModifySubnetAttribute(modifyOpts)
if err != nil {
return nil, fmt.Errorf("Error modify subnet attributes: %s", err)
}
}
2014-07-07 23:46:02 +02:00
// Update our attributes and return
return resource_aws_subnet_update_state(s, subnetRaw.(*ec2.Subnet))
}
func resource_aws_subnet_update(
2014-09-17 02:44:42 +02:00
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
d *terraform.InstanceDiff,
2014-09-17 02:44:42 +02:00
meta interface{}) (*terraform.InstanceState, error) {
2014-07-07 23:46:02 +02:00
// This should never be called because we have no update-able
// attributes
panic("Update for subnet is not supported")
}
func resource_aws_subnet_destroy(
2014-09-17 02:44:42 +02:00
s *terraform.InstanceState,
2014-07-07 23:46:02 +02:00
meta interface{}) error {
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
log.Printf("[INFO] Deleting Subnet: %s", s.ID)
f := func() error {
_, err := ec2conn.DeleteSubnet(s.ID)
return err
}
if err := resource.Retry(10 * time.Second, f); err != nil {
ec2err, ok := err.(*ec2.Error)
if ok && ec2err.Code == "InvalidSubnetID.NotFound" {
return nil
}
2014-07-07 23:46:02 +02:00
return fmt.Errorf("Error deleting subnet: %s", err)
}
// Wait for the Subnet to actually delete
log.Printf("[DEBUG] Waiting for subnet (%s) to delete", s.ID)
stateConf := &resource.StateChangeConf{
Pending: []string{"available", "pending"},
Target: "",
Refresh: SubnetStateRefreshFunc(ec2conn, s.ID),
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
2014-08-25 06:50:35 +02:00
"Error waiting for subnet (%s) to destroy: %s",
s.ID, err)
}
2014-07-07 23:46:02 +02:00
return nil
}
func resource_aws_subnet_refresh(
2014-09-17 02:44:42 +02:00
s *terraform.InstanceState,
meta interface{}) (*terraform.InstanceState, error) {
2014-07-07 23:46:02 +02:00
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
subnetRaw, _, err := SubnetStateRefreshFunc(ec2conn, s.ID)()
if err != nil {
return s, err
}
if subnetRaw == nil {
return nil, nil
}
subnet := subnetRaw.(*ec2.Subnet)
return resource_aws_subnet_update_state(s, subnet)
}
func resource_aws_subnet_diff(
2014-09-17 02:44:42 +02:00
s *terraform.InstanceState,
2014-07-07 23:46:02 +02:00
c *terraform.ResourceConfig,
2014-09-18 01:33:24 +02:00
meta interface{}) (*terraform.InstanceDiff, error) {
2014-07-07 23:46:02 +02:00
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
2014-09-10 07:13:16 +02:00
"availability_zone": diff.AttrTypeCreate,
"cidr_block": diff.AttrTypeCreate,
"vpc_id": diff.AttrTypeCreate,
"map_public_ip_on_launch": diff.AttrTypeCreate,
2014-07-07 23:46:02 +02:00
},
ComputedAttrs: []string{
"availability_zone",
},
2014-07-07 23:46:02 +02:00
}
return b.Diff(s, c)
}
func resource_aws_subnet_update_state(
2014-09-17 02:44:42 +02:00
s *terraform.InstanceState,
subnet *ec2.Subnet) (*terraform.InstanceState, error) {
2014-07-07 23:46:02 +02:00
s.Attributes["availability_zone"] = subnet.AvailabilityZone
s.Attributes["cidr_block"] = subnet.CidrBlock
s.Attributes["vpc_id"] = subnet.VpcId
2014-09-10 07:13:16 +02:00
if subnet.MapPublicIpOnLaunch {
s.Attributes["map_public_ip_on_launch"] = "true"
}
2014-07-07 23:46:02 +02:00
return s, nil
}
// SubnetStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a Subnet.
func SubnetStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeSubnets([]string{id}, ec2.NewFilter())
if err != nil {
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidSubnetID.NotFound" {
resp = nil
} else {
log.Printf("Error on SubnetStateRefresh: %s", err)
return nil, "", err
}
}
if resp == nil {
// Sometimes AWS just has consistency issues and doesn't see
// our instance yet. Return an empty state.
return nil, "", nil
}
subnet := &resp.Subnets[0]
return subnet, subnet.State, nil
}
}