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