terraform/builtin/providers/aws/resource_aws_subnet.go

226 lines
5.3 KiB
Go
Raw Normal View History

2014-07-07 23:46:02 +02:00
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
2014-07-07 23:46:02 +02:00
"github.com/hashicorp/terraform/helper/resource"
2014-11-02 21:30:33 +01:00
"github.com/hashicorp/terraform/helper/schema"
2014-07-07 23:46:02 +02:00
)
2014-11-02 21:30:33 +01:00
func resourceAwsSubnet() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSubnetCreate,
Read: resourceAwsSubnetRead,
Update: resourceAwsSubnetUpdate,
Delete: resourceAwsSubnetDelete,
Schema: map[string]*schema.Schema{
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
2014-11-02 21:30:33 +01:00
ForceNew: true,
},
"cidr_block": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"availability_zone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"map_public_ip_on_launch": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
2014-11-02 21:30:33 +01:00
},
"tags": tagsSchema(),
},
}
}
func resourceAwsSubnetCreate(d *schema.ResourceData, meta interface{}) error {
2015-04-16 22:05:55 +02:00
conn := meta.(*AWSClient).ec2conn
2014-07-07 23:46:02 +02:00
createOpts := &ec2.CreateSubnetInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
CidrBlock: aws.String(d.Get("cidr_block").(string)),
VpcId: aws.String(d.Get("vpc_id").(string)),
2014-07-07 23:46:02 +02:00
}
2014-11-02 21:30:33 +01:00
var err error
resp, err := conn.CreateSubnet(createOpts)
2014-11-02 21:30:33 +01:00
2014-07-07 23:46:02 +02:00
if err != nil {
2014-11-02 21:30:33 +01:00
return fmt.Errorf("Error creating subnet: %s", err)
2014-07-07 23:46:02 +02:00
}
// Get the ID and store it
subnet := resp.Subnet
d.SetId(*subnet.SubnetId)
log.Printf("[INFO] Subnet ID: %s", *subnet.SubnetId)
2014-07-07 23:46:02 +02:00
// Wait for the Subnet to become available
log.Printf("[DEBUG] Waiting for subnet (%s) to become available", *subnet.SubnetId)
2014-07-07 23:46:02 +02:00
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"available"},
Refresh: SubnetStateRefreshFunc(conn, *subnet.SubnetId),
2014-07-07 23:46:02 +02:00
Timeout: 10 * time.Minute,
}
2014-11-02 21:30:33 +01:00
_, err = stateConf.WaitForState()
2014-09-10 07:13:16 +02:00
2014-11-02 21:30:33 +01:00
if err != nil {
return fmt.Errorf(
"Error waiting for subnet (%s) to become ready: %s",
d.Id(), err)
}
2014-07-07 23:46:02 +02:00
2014-11-02 21:30:33 +01:00
return resourceAwsSubnetUpdate(d, meta)
2014-07-07 23:46:02 +02:00
}
2014-11-02 21:30:33 +01:00
func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
2015-04-16 22:05:55 +02:00
conn := meta.(*AWSClient).ec2conn
2014-07-07 23:46:02 +02:00
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
SubnetIds: []*string{aws.String(d.Id())},
})
2014-07-07 23:46:02 +02:00
2014-11-02 21:30:33 +01:00
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSubnetID.NotFound" {
// Update state to indicate the subnet no longer exists.
d.SetId("")
return nil
}
2014-11-02 21:30:33 +01:00
return err
}
2014-11-02 21:30:33 +01:00
if resp == nil {
return nil
}
subnet := resp.Subnets[0]
2014-11-02 21:30:33 +01:00
d.Set("vpc_id", subnet.VpcId)
2014-11-02 21:30:33 +01:00
d.Set("availability_zone", subnet.AvailabilityZone)
d.Set("cidr_block", subnet.CidrBlock)
d.Set("map_public_ip_on_launch", subnet.MapPublicIpOnLaunch)
d.Set("tags", tagsToMap(subnet.Tags))
2014-11-02 21:30:33 +01:00
2014-07-07 23:46:02 +02:00
return nil
}
2014-11-02 21:30:33 +01:00
func resourceAwsSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
2015-04-16 22:05:55 +02:00
conn := meta.(*AWSClient).ec2conn
2014-07-07 23:46:02 +02:00
2014-11-02 21:30:33 +01:00
d.Partial(true)
if err := setTags(conn, d); err != nil {
2014-11-02 21:30:33 +01:00
return err
} else {
d.SetPartial("tags")
2014-07-07 23:46:02 +02:00
}
2014-11-02 21:30:33 +01:00
if d.HasChange("map_public_ip_on_launch") {
modifyOpts := &ec2.ModifySubnetAttributeInput{
SubnetId: aws.String(d.Id()),
MapPublicIpOnLaunch: &ec2.AttributeBooleanValue{
Value: aws.Bool(d.Get("map_public_ip_on_launch").(bool)),
},
2014-11-02 21:30:33 +01:00
}
2014-07-07 23:46:02 +02:00
2014-11-02 21:30:33 +01:00
log.Printf("[DEBUG] Subnet modify attributes: %#v", modifyOpts)
_, err := conn.ModifySubnetAttribute(modifyOpts)
2014-11-02 21:30:33 +01:00
if err != nil {
return err
} else {
d.SetPartial("map_public_ip_on_launch")
}
2014-07-07 23:46:02 +02:00
}
2014-11-02 21:30:33 +01:00
d.Partial(false)
return resourceAwsSubnetRead(d, meta)
2014-07-07 23:46:02 +02:00
}
2014-11-02 21:30:33 +01:00
func resourceAwsSubnetDelete(d *schema.ResourceData, meta interface{}) error {
2015-04-16 22:05:55 +02:00
conn := meta.(*AWSClient).ec2conn
2014-11-02 21:30:33 +01:00
log.Printf("[INFO] Deleting subnet: %s", d.Id())
req := &ec2.DeleteSubnetInput{
SubnetId: aws.String(d.Id()),
}
wait := resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"destroyed"},
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
Refresh: func() (interface{}, string, error) {
_, err := conn.DeleteSubnet(req)
if err != nil {
if apiErr, ok := err.(awserr.Error); ok {
if apiErr.Code() == "DependencyViolation" {
// There is some pending operation, so just retry
// in a bit.
return 42, "pending", nil
}
if apiErr.Code() == "InvalidSubnetID.NotFound" {
return 42, "destroyed", nil
}
}
return 42, "failure", err
}
return 42, "destroyed", nil
},
}
2014-11-02 21:30:33 +01:00
if _, err := wait.WaitForState(); err != nil {
2014-11-02 21:30:33 +01:00
return fmt.Errorf("Error deleting subnet: %s", err)
2014-09-10 07:13:16 +02:00
}
2014-11-02 21:30:33 +01:00
return nil
2014-07-07 23:46:02 +02:00
}
2014-11-02 21:30:33 +01:00
// SubnetStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a Subnet.
2014-07-07 23:46:02 +02:00
func SubnetStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
SubnetIds: []*string{aws.String(id)},
})
2014-07-07 23:46:02 +02:00
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSubnetID.NotFound" {
2014-07-07 23:46:02 +02:00
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
2014-07-07 23:46:02 +02:00
}
}