github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_default_vpc.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 resourceAwsDefaultVpc() *schema.Resource {
    13  	// reuse aws_vpc schema, and methods for READ, UPDATE
    14  	dvpc := resourceAwsVpc()
    15  	dvpc.Create = resourceAwsDefaultVpcCreate
    16  	dvpc.Delete = resourceAwsDefaultVpcDelete
    17  
    18  	// cidr_block is a computed value for Default VPCs
    19  	dvpc.Schema["cidr_block"] = &schema.Schema{
    20  		Type:     schema.TypeString,
    21  		Computed: true,
    22  	}
    23  	// instance_tenancy is a computed value for Default VPCs
    24  	dvpc.Schema["instance_tenancy"] = &schema.Schema{
    25  		Type:     schema.TypeString,
    26  		Computed: true,
    27  	}
    28  	// assign_generated_ipv6_cidr_block is a computed value for Default VPCs
    29  	dvpc.Schema["assign_generated_ipv6_cidr_block"] = &schema.Schema{
    30  		Type:     schema.TypeBool,
    31  		Computed: true,
    32  	}
    33  
    34  	return dvpc
    35  }
    36  
    37  func resourceAwsDefaultVpcCreate(d *schema.ResourceData, meta interface{}) error {
    38  	conn := meta.(*AWSClient).ec2conn
    39  	req := &ec2.DescribeVpcsInput{
    40  		Filters: []*ec2.Filter{
    41  			{
    42  				Name:   aws.String("isDefault"),
    43  				Values: aws.StringSlice([]string{"true"}),
    44  			},
    45  		},
    46  	}
    47  
    48  	resp, err := conn.DescribeVpcs(req)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	if resp.Vpcs == nil || len(resp.Vpcs) == 0 {
    54  		return fmt.Errorf("No default VPC found in this region.")
    55  	}
    56  
    57  	d.SetId(aws.StringValue(resp.Vpcs[0].VpcId))
    58  
    59  	return resourceAwsVpcUpdate(d, meta)
    60  }
    61  
    62  func resourceAwsDefaultVpcDelete(d *schema.ResourceData, meta interface{}) error {
    63  	log.Printf("[WARN] Cannot destroy Default VPC. Terraform will remove this resource from the state file, however resources may remain.")
    64  	d.SetId("")
    65  	return nil
    66  }