github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_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 dataSourceAwsVpc() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsVpcRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"cidr_block": {
    18  				Type:     schema.TypeString,
    19  				Optional: true,
    20  				Computed: true,
    21  			},
    22  
    23  			"dhcp_options_id": {
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				Computed: true,
    27  			},
    28  
    29  			"default": {
    30  				Type:     schema.TypeBool,
    31  				Optional: true,
    32  				Computed: true,
    33  			},
    34  
    35  			"filter": ec2CustomFiltersSchema(),
    36  
    37  			"id": {
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  				Computed: true,
    41  			},
    42  
    43  			"instance_tenancy": {
    44  				Type:     schema.TypeString,
    45  				Computed: true,
    46  			},
    47  
    48  			"ipv6_cidr_block": {
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  
    53  			"ipv6_association_id": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"state": {
    59  				Type:     schema.TypeString,
    60  				Optional: true,
    61  				Computed: true,
    62  			},
    63  
    64  			"tags": tagsSchemaComputed(),
    65  		},
    66  	}
    67  }
    68  
    69  func dataSourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
    70  	conn := meta.(*AWSClient).ec2conn
    71  
    72  	req := &ec2.DescribeVpcsInput{}
    73  
    74  	if id := d.Get("id"); id != "" {
    75  		req.VpcIds = []*string{aws.String(id.(string))}
    76  	}
    77  
    78  	// We specify "default" as boolean, but EC2 filters want
    79  	// it to be serialized as a string. Note that setting it to
    80  	// "false" here does not actually filter by it *not* being
    81  	// the default, because Terraform can't distinguish between
    82  	// "false" and "not set".
    83  	isDefaultStr := ""
    84  	if d.Get("default").(bool) {
    85  		isDefaultStr = "true"
    86  	}
    87  
    88  	req.Filters = buildEC2AttributeFilterList(
    89  		map[string]string{
    90  			"cidr":            d.Get("cidr_block").(string),
    91  			"dhcp-options-id": d.Get("dhcp_options_id").(string),
    92  			"isDefault":       isDefaultStr,
    93  			"state":           d.Get("state").(string),
    94  		},
    95  	)
    96  	req.Filters = append(req.Filters, buildEC2TagFilterList(
    97  		tagsFromMap(d.Get("tags").(map[string]interface{})),
    98  	)...)
    99  	req.Filters = append(req.Filters, buildEC2CustomFilterList(
   100  		d.Get("filter").(*schema.Set),
   101  	)...)
   102  	if len(req.Filters) == 0 {
   103  		// Don't send an empty filters list; the EC2 API won't accept it.
   104  		req.Filters = nil
   105  	}
   106  
   107  	log.Printf("[DEBUG] DescribeVpcs %s\n", req)
   108  	resp, err := conn.DescribeVpcs(req)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	if resp == nil || len(resp.Vpcs) == 0 {
   113  		return fmt.Errorf("no matching VPC found")
   114  	}
   115  	if len(resp.Vpcs) > 1 {
   116  		return fmt.Errorf("multiple VPCs matched; use additional constraints to reduce matches to a single VPC")
   117  	}
   118  
   119  	vpc := resp.Vpcs[0]
   120  
   121  	d.SetId(*vpc.VpcId)
   122  	d.Set("id", vpc.VpcId)
   123  	d.Set("cidr_block", vpc.CidrBlock)
   124  	d.Set("dhcp_options_id", vpc.DhcpOptionsId)
   125  	d.Set("instance_tenancy", vpc.InstanceTenancy)
   126  	d.Set("default", vpc.IsDefault)
   127  	d.Set("state", vpc.State)
   128  	d.Set("tags", tagsToMap(vpc.Tags))
   129  
   130  	if vpc.Ipv6CidrBlockAssociationSet != nil {
   131  		d.Set("ipv6_association_id", vpc.Ipv6CidrBlockAssociationSet[0].AssociationId)
   132  		d.Set("ipv6_cidr_block", vpc.Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock)
   133  	}
   134  
   135  	return nil
   136  }