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

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	consulapi "github.com/hashicorp/consul/api"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  const (
    14  	// Datasource predicates
    15  	catalogServicesServiceName = "name"
    16  
    17  	// Out parameters
    18  	catalogServicesDatacenter  = "datacenter"
    19  	catalogServicesNames       = "names"
    20  	catalogServicesServices    = "services"
    21  	catalogServicesServiceTags = "tags"
    22  )
    23  
    24  func dataSourceConsulCatalogServices() *schema.Resource {
    25  	return &schema.Resource{
    26  		Read: dataSourceConsulCatalogServicesRead,
    27  		Schema: map[string]*schema.Schema{
    28  			// Data Source Predicate(s)
    29  			catalogServicesDatacenter: &schema.Schema{
    30  				// Used in the query, must be stored and force a refresh if the value
    31  				// changes.
    32  				Computed: true,
    33  				Type:     schema.TypeString,
    34  				ForceNew: true,
    35  			},
    36  			catalogNodesQueryOpts: schemaQueryOpts,
    37  
    38  			// Out parameters
    39  			catalogServicesNames: &schema.Schema{
    40  				Type:     schema.TypeList,
    41  				Computed: true,
    42  				Elem:     &schema.Schema{Type: schema.TypeString},
    43  			},
    44  			catalogServicesServices: &schema.Schema{
    45  				Computed: true,
    46  				Type:     schema.TypeMap,
    47  				Elem: &schema.Resource{
    48  					Schema: map[string]*schema.Schema{
    49  						catalogServiceServiceTags: &schema.Schema{
    50  							Type:     schema.TypeList,
    51  							Computed: true,
    52  							Elem:     &schema.Schema{Type: schema.TypeString},
    53  						},
    54  					},
    55  				},
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func dataSourceConsulCatalogServicesRead(d *schema.ResourceData, meta interface{}) error {
    62  	client := meta.(*consulapi.Client)
    63  
    64  	// Parse out data source filters to populate Consul's query options
    65  	queryOpts, err := getQueryOpts(d, client)
    66  	if err != nil {
    67  		return errwrap.Wrapf("unable to get query options for fetching catalog services: {{err}}", err)
    68  	}
    69  
    70  	services, meta, err := client.Catalog().Services(queryOpts)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	catalogServices := make(map[string]interface{}, len(services))
    76  	for name, tags := range services {
    77  		tagList := make([]string, 0, len(tags))
    78  		for _, tag := range tags {
    79  			tagList = append(tagList, tag)
    80  		}
    81  
    82  		sort.Strings(tagList)
    83  		catalogServices[name] = strings.Join(tagList, " ")
    84  	}
    85  
    86  	serviceNames := make([]interface{}, 0, len(services))
    87  	for k := range catalogServices {
    88  		serviceNames = append(serviceNames, k)
    89  	}
    90  
    91  	const idKeyFmt = "catalog-services-%s"
    92  	d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))
    93  
    94  	d.Set(catalogServicesDatacenter, queryOpts.Datacenter)
    95  	if err := d.Set(catalogServicesServices, catalogServices); err != nil {
    96  		return errwrap.Wrapf("Unable to store services: {{err}}", err)
    97  	}
    98  
    99  	if err := d.Set(catalogServicesNames, serviceNames); err != nil {
   100  		return errwrap.Wrapf("Unable to store service names: {{err}}", err)
   101  	}
   102  
   103  	return nil
   104  }