github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/data_source_aws_prefix_list.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 dataSourceAwsPrefixList() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsPrefixListRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"prefix_list_id": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			// Computed values.
    22  			"id": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Computed: true,
    25  			},
    26  			"name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Computed: true,
    29  			},
    30  			"cidr_blocks": &schema.Schema{
    31  				Type:     schema.TypeList,
    32  				Computed: true,
    33  				Elem:     &schema.Schema{Type: schema.TypeString},
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error {
    40  	conn := meta.(*AWSClient).ec2conn
    41  
    42  	req := &ec2.DescribePrefixListsInput{}
    43  
    44  	if prefixListID := d.Get("prefix_list_id"); prefixListID != "" {
    45  		req.PrefixListIds = []*string{aws.String(prefixListID.(string))}
    46  	}
    47  
    48  	log.Printf("[DEBUG] DescribePrefixLists %s\n", req)
    49  	resp, err := conn.DescribePrefixLists(req)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	if resp == nil || len(resp.PrefixLists) == 0 {
    54  		return fmt.Errorf("no matching prefix list found; the prefix list ID may be invalid or not exist in the current region")
    55  	}
    56  
    57  	pl := resp.PrefixLists[0]
    58  
    59  	d.SetId(*pl.PrefixListId)
    60  	d.Set("id", pl.PrefixListId)
    61  	d.Set("name", pl.PrefixListName)
    62  
    63  	cidrs := make([]string, len(pl.Cidrs))
    64  	for i, v := range pl.Cidrs {
    65  		cidrs[i] = *v
    66  	}
    67  	d.Set("cidr_blocks", cidrs)
    68  
    69  	return nil
    70  }