github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/aws/resource_aws_default_subnet.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsDefaultSubnet() *schema.Resource {
    13  	// reuse aws_subnet schema, and methods for READ, UPDATE
    14  	dsubnet := resourceAwsSubnet()
    15  	dsubnet.Create = resourceAwsDefaultSubnetCreate
    16  	dsubnet.Delete = resourceAwsDefaultSubnetDelete
    17  
    18  	// vpc_id is a required value for Default Subnets
    19  	dsubnet.Schema["availability_zone"] = &schema.Schema{
    20  		Type:     schema.TypeString,
    21  		Required: true,
    22  	}
    23  	// vpc_id is a computed value for Default Subnets
    24  	dsubnet.Schema["vpc_id"] = &schema.Schema{
    25  		Type:     schema.TypeString,
    26  		Computed: true,
    27  	}
    28  	// cidr_block is a computed value for Default Subnets
    29  	dsubnet.Schema["cidr_block"] = &schema.Schema{
    30  		Type:     schema.TypeString,
    31  		Computed: true,
    32  	}
    33  	// ipv6_cidr_block is a computed value for Default Subnets
    34  	dsubnet.Schema["ipv6_cidr_block"] = &schema.Schema{
    35  		Type:     schema.TypeString,
    36  		Computed: true,
    37  	}
    38  	// map_public_ip_on_launch is a computed value for Default Subnets
    39  	dsubnet.Schema["map_public_ip_on_launch"] = &schema.Schema{
    40  		Type:     schema.TypeBool,
    41  		Computed: true,
    42  	}
    43  	// assign_ipv6_address_on_creation is a computed value for Default Subnets
    44  	dsubnet.Schema["assign_ipv6_address_on_creation"] = &schema.Schema{
    45  		Type:     schema.TypeBool,
    46  		Computed: true,
    47  	}
    48  
    49  	return dsubnet
    50  }
    51  
    52  func resourceAwsDefaultSubnetCreate(d *schema.ResourceData, meta interface{}) error {
    53  	conn := meta.(*AWSClient).ec2conn
    54  	req := &ec2.DescribeSubnetsInput{
    55  		Filters: []*ec2.Filter{
    56  			&ec2.Filter{
    57  				Name:   aws.String("availabilityZone"),
    58  				Values: aws.StringSlice([]string{d.Get("availability_zone").(string)}),
    59  			},
    60  			&ec2.Filter{
    61  				Name:   aws.String("defaultForAz"),
    62  				Values: aws.StringSlice([]string{"true"}),
    63  			},
    64  		},
    65  	}
    66  
    67  	resp, err := conn.DescribeSubnets(req)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	if len(resp.Subnets) != 1 || resp.Subnets[0] == nil {
    73  		return fmt.Errorf("Default subnet not found")
    74  	}
    75  
    76  	d.SetId(aws.StringValue(resp.Subnets[0].SubnetId))
    77  
    78  	return resourceAwsSubnetUpdate(d, meta)
    79  }
    80  
    81  func resourceAwsDefaultSubnetDelete(d *schema.ResourceData, meta interface{}) error {
    82  	log.Printf("[WARN] Cannot destroy Default Subnet. Terraform will remove this resource from the state file, however resources may remain.")
    83  	d.SetId("")
    84  	return nil
    85  }