2014-07-07 23:46:02 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/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,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: 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,
|
2015-01-30 19:17:16 +01:00
|
|
|
Computed: true,
|
2014-11-02 21:30:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSubnetCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-07 23:46:02 +02:00
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
createOpts := &ec2.CreateSubnetRequest{
|
|
|
|
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
|
|
|
|
2014-07-07 23:46:02 +02:00
|
|
|
resp, err := ec2conn.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
|
2015-03-02 23:32:48 +01:00
|
|
|
subnet := resp.Subnet
|
|
|
|
d.SetId(*subnet.SubnetID)
|
2015-03-05 21:43:52 +01:00
|
|
|
log.Printf("[INFO] Subnet ID: %s", *subnet.SubnetID)
|
2014-07-07 23:46:02 +02:00
|
|
|
|
|
|
|
// Wait for the Subnet to become available
|
2015-03-05 21:43:52 +01:00
|
|
|
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: "available",
|
2015-03-02 23:32:48 +01:00
|
|
|
Refresh: SubnetStateRefreshFunc(ec2conn, *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-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-07 23:46:02 +02:00
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
|
|
|
SubnetIDs: []string{d.Id()},
|
|
|
|
})
|
2014-07-07 23:46:02 +02:00
|
|
|
|
2014-11-02 21:30:33 +01:00
|
|
|
if err != nil {
|
2015-03-02 23:32:48 +01:00
|
|
|
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidSubnetID.NotFound" {
|
2015-01-29 17:06:40 +01:00
|
|
|
// Update state to indicate the subnet no longer exists.
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-02 21:30:33 +01:00
|
|
|
return err
|
2014-07-08 00:44:08 +02:00
|
|
|
}
|
2014-11-02 21:30:33 +01:00
|
|
|
if resp == nil {
|
|
|
|
return nil
|
2014-07-08 00:44:08 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 21:30:33 +01:00
|
|
|
subnet := &resp.Subnets[0]
|
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
d.Set("vpc_id", subnet.VPCID)
|
2014-11-02 21:30:33 +01:00
|
|
|
d.Set("availability_zone", subnet.AvailabilityZone)
|
2015-03-02 23:32:48 +01:00
|
|
|
d.Set("cidr_block", subnet.CIDRBlock)
|
|
|
|
d.Set("map_public_ip_on_launch", subnet.MapPublicIPOnLaunch)
|
2015-03-12 22:43:08 +01:00
|
|
|
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-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-07 23:46:02 +02:00
|
|
|
|
2014-11-02 21:30:33 +01:00
|
|
|
d.Partial(true)
|
|
|
|
|
2015-03-12 22:43:08 +01:00
|
|
|
if err := setTags(ec2conn, 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") {
|
2015-03-02 23:32:48 +01:00
|
|
|
modifyOpts := &ec2.ModifySubnetAttributeRequest{
|
|
|
|
SubnetID: aws.String(d.Id()),
|
|
|
|
MapPublicIPOnLaunch: &ec2.AttributeBooleanValue{aws.Boolean(true)},
|
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)
|
2014-07-11 22:10:09 +02:00
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
err := ec2conn.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-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-11-02 21:30:33 +01:00
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting subnet: %s", d.Id())
|
2015-03-02 23:32:48 +01:00
|
|
|
|
|
|
|
err := ec2conn.DeleteSubnet(&ec2.DeleteSubnetRequest{
|
|
|
|
SubnetID: aws.String(d.Id()),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ec2err, ok := err.(aws.APIError)
|
2014-11-02 21:30:33 +01:00
|
|
|
if ok && ec2err.Code == "InvalidSubnetID.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-03-02 23:32:48 +01:00
|
|
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
|
|
|
SubnetIDs: []string{id},
|
|
|
|
})
|
2014-07-07 23:46:02 +02:00
|
|
|
if err != nil {
|
2015-03-02 23:32:48 +01:00
|
|
|
if ec2err, ok := err.(aws.APIError); 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]
|
2015-03-02 23:32:48 +01:00
|
|
|
return subnet, *subnet.State, nil
|
2014-07-07 23:46:02 +02:00
|
|
|
}
|
|
|
|
}
|