github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_common_schema.go (about)

     1  package aws
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/hashcode"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceTagsHash(v interface{}) int {
    14  	var buf bytes.Buffer
    15  	m := v.(map[string]interface{})
    16  	buf.WriteString(fmt.Sprintf("%s-", m["key"].(string)))
    17  	buf.WriteString(fmt.Sprintf("%s-", m["value"].(string)))
    18  	return hashcode.String(buf.String())
    19  }
    20  
    21  func dataSourceTags(m []*ec2.Tag) *schema.Set {
    22  	s := &schema.Set{
    23  		F: dataSourceTagsHash,
    24  	}
    25  	for _, v := range m {
    26  		tag := map[string]interface{}{
    27  			"key":   *v.Key,
    28  			"value": *v.Value,
    29  		}
    30  		s.Add(tag)
    31  	}
    32  	return s
    33  }
    34  
    35  func buildAwsDataSourceFilters(set *schema.Set) []*ec2.Filter {
    36  	var filters []*ec2.Filter
    37  	for _, v := range set.List() {
    38  		m := v.(map[string]interface{})
    39  		var filterValues []*string
    40  		for _, e := range m["values"].([]interface{}) {
    41  			filterValues = append(filterValues, aws.String(e.(string)))
    42  		}
    43  		filters = append(filters, &ec2.Filter{
    44  			Name:   aws.String(m["name"].(string)),
    45  			Values: filterValues,
    46  		})
    47  	}
    48  	return filters
    49  }
    50  
    51  func dataSourceFiltersSchema() *schema.Schema {
    52  	return &schema.Schema{
    53  		Type:     schema.TypeSet,
    54  		Optional: true,
    55  		ForceNew: true,
    56  		Elem: &schema.Resource{
    57  			Schema: map[string]*schema.Schema{
    58  				"name": {
    59  					Type:     schema.TypeString,
    60  					Required: true,
    61  				},
    62  
    63  				"values": {
    64  					Type:     schema.TypeList,
    65  					Required: true,
    66  					Elem:     &schema.Schema{Type: schema.TypeString},
    67  				},
    68  			},
    69  		},
    70  	}
    71  }
    72  
    73  func dataSourceTagsSchema() *schema.Schema {
    74  	return &schema.Schema{
    75  		Type:     schema.TypeSet,
    76  		Computed: true,
    77  		Set:      dataSourceTagsHash,
    78  		Elem: &schema.Resource{
    79  			Schema: map[string]*schema.Schema{
    80  				"key": {
    81  					Type:     schema.TypeString,
    82  					Computed: true,
    83  				},
    84  				"value": {
    85  					Type:     schema.TypeString,
    86  					Computed: true,
    87  				},
    88  			},
    89  		},
    90  	}
    91  }