github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_ip_ranges.go (about)

     1  package aws
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"sort"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"github.com/hashicorp/go-cleanhttp"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  type dataSourceAwsIPRangesResult struct {
    17  	CreateDate string
    18  	Prefixes   []dataSourceAwsIPRangesPrefix
    19  	SyncToken  string
    20  }
    21  
    22  type dataSourceAwsIPRangesPrefix struct {
    23  	IpPrefix string `json:"ip_prefix"`
    24  	Region   string
    25  	Service  string
    26  }
    27  
    28  func dataSourceAwsIPRanges() *schema.Resource {
    29  	return &schema.Resource{
    30  		Read: dataSourceAwsIPRangesRead,
    31  
    32  		Schema: map[string]*schema.Schema{
    33  			"cidr_blocks": &schema.Schema{
    34  				Type:     schema.TypeList,
    35  				Computed: true,
    36  				Elem:     &schema.Schema{Type: schema.TypeString},
    37  			},
    38  			"create_date": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  			"regions": &schema.Schema{
    43  				Type:     schema.TypeSet,
    44  				Elem:     &schema.Schema{Type: schema.TypeString},
    45  				Optional: true,
    46  			},
    47  			"services": &schema.Schema{
    48  				Type:     schema.TypeSet,
    49  				Required: true,
    50  				Elem:     &schema.Schema{Type: schema.TypeString},
    51  			},
    52  			"sync_token": &schema.Schema{
    53  				Type:     schema.TypeInt,
    54  				Computed: true,
    55  			},
    56  		},
    57  	}
    58  }
    59  
    60  func dataSourceAwsIPRangesRead(d *schema.ResourceData, meta interface{}) error {
    61  
    62  	conn := cleanhttp.DefaultClient()
    63  
    64  	log.Printf("[DEBUG] Reading IP ranges")
    65  
    66  	res, err := conn.Get("https://ip-ranges.amazonaws.com/ip-ranges.json")
    67  
    68  	if err != nil {
    69  		return fmt.Errorf("Error listing IP ranges: %s", err)
    70  	}
    71  
    72  	defer res.Body.Close()
    73  
    74  	data, err := ioutil.ReadAll(res.Body)
    75  
    76  	if err != nil {
    77  		return fmt.Errorf("Error reading response body: %s", err)
    78  	}
    79  
    80  	result := new(dataSourceAwsIPRangesResult)
    81  
    82  	if err := json.Unmarshal(data, result); err != nil {
    83  		return fmt.Errorf("Error parsing result: %s", err)
    84  	}
    85  
    86  	if err := d.Set("create_date", result.CreateDate); err != nil {
    87  		return fmt.Errorf("Error setting create date: %s", err)
    88  	}
    89  
    90  	syncToken, err := strconv.Atoi(result.SyncToken)
    91  
    92  	if err != nil {
    93  		return fmt.Errorf("Error while converting sync token: %s", err)
    94  	}
    95  
    96  	d.SetId(result.SyncToken)
    97  
    98  	if err := d.Set("sync_token", syncToken); err != nil {
    99  		return fmt.Errorf("Error setting sync token: %s", err)
   100  	}
   101  
   102  	get := func(key string) *schema.Set {
   103  
   104  		set := d.Get(key).(*schema.Set)
   105  
   106  		for _, e := range set.List() {
   107  
   108  			s := e.(string)
   109  
   110  			set.Remove(s)
   111  			set.Add(strings.ToLower(s))
   112  
   113  		}
   114  
   115  		return set
   116  
   117  	}
   118  
   119  	var (
   120  		regions        = get("regions")
   121  		services       = get("services")
   122  		noRegionFilter = regions.Len() == 0
   123  		prefixes       []string
   124  	)
   125  
   126  	for _, e := range result.Prefixes {
   127  
   128  		var (
   129  			matchRegion  = noRegionFilter || regions.Contains(strings.ToLower(e.Region))
   130  			matchService = services.Contains(strings.ToLower(e.Service))
   131  		)
   132  
   133  		if matchRegion && matchService {
   134  			prefixes = append(prefixes, e.IpPrefix)
   135  		}
   136  
   137  	}
   138  
   139  	if len(prefixes) == 0 {
   140  		return fmt.Errorf(" No IP ranges result from filters")
   141  	}
   142  
   143  	sort.Strings(prefixes)
   144  
   145  	if err := d.Set("cidr_blocks", prefixes); err != nil {
   146  		return fmt.Errorf("Error setting ip ranges: %s", err)
   147  	}
   148  
   149  	return nil
   150  
   151  }