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

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	consulapi "github.com/hashicorp/consul/api"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  const (
    13  	catalogServiceElem = "service"
    14  
    15  	catalogServiceCreateIndex              = "create_index"
    16  	catalogServiceDatacenter               = "datacenter"
    17  	catalogServiceModifyIndex              = "modify_index"
    18  	catalogServiceNodeAddress              = "node_address"
    19  	catalogServiceNodeID                   = "node_id"
    20  	catalogServiceNodeMeta                 = "node_meta"
    21  	catalogServiceNodeName                 = "node_name"
    22  	catalogServiceServiceAddress           = "address"
    23  	catalogServiceServiceEnableTagOverride = "enable_tag_override"
    24  	catalogServiceServiceID                = "id"
    25  	catalogServiceServiceName              = "name"
    26  	catalogServiceServicePort              = "port"
    27  	catalogServiceServiceTags              = "tags"
    28  	catalogServiceTaggedAddresses          = "tagged_addresses"
    29  
    30  	// Filters
    31  	catalogServiceName = "name"
    32  	catalogServiceTag  = "tag"
    33  )
    34  
    35  func dataSourceConsulCatalogService() *schema.Resource {
    36  	return &schema.Resource{
    37  		Read: dataSourceConsulCatalogServiceRead,
    38  		Schema: map[string]*schema.Schema{
    39  			// Data Source Predicate(s)
    40  			catalogServiceDatacenter: &schema.Schema{
    41  				// Used in the query, must be stored and force a refresh if the value
    42  				// changes.
    43  				Computed: true,
    44  				Type:     schema.TypeString,
    45  				ForceNew: true,
    46  			},
    47  			catalogServiceTag: &schema.Schema{
    48  				// Used in the query, must be stored and force a refresh if the value
    49  				// changes.
    50  				Computed: true,
    51  				Type:     schema.TypeString,
    52  				ForceNew: true,
    53  			},
    54  			catalogServiceName: &schema.Schema{
    55  				Required: true,
    56  				Type:     schema.TypeString,
    57  			},
    58  			catalogNodesQueryOpts: schemaQueryOpts,
    59  
    60  			// Out parameters
    61  			catalogServiceElem: &schema.Schema{
    62  				Computed: true,
    63  				Type:     schema.TypeList,
    64  				Elem: &schema.Resource{
    65  					Schema: map[string]*schema.Schema{
    66  						catalogServiceCreateIndex: &schema.Schema{
    67  							Type:     schema.TypeString,
    68  							Computed: true,
    69  						},
    70  						catalogServiceNodeAddress: &schema.Schema{
    71  							Type:     schema.TypeString,
    72  							Computed: true,
    73  						},
    74  						catalogServiceNodeID: &schema.Schema{
    75  							Type:     schema.TypeString,
    76  							Computed: true,
    77  						},
    78  						catalogServiceModifyIndex: &schema.Schema{
    79  							Type:     schema.TypeString,
    80  							Computed: true,
    81  						},
    82  						catalogServiceNodeName: &schema.Schema{
    83  							Type:     schema.TypeString,
    84  							Computed: true,
    85  						},
    86  						catalogServiceNodeMeta: &schema.Schema{
    87  							Type:     schema.TypeMap,
    88  							Computed: true,
    89  						},
    90  						catalogServiceServiceAddress: &schema.Schema{
    91  							Type:     schema.TypeString,
    92  							Computed: true,
    93  						},
    94  						catalogServiceServiceEnableTagOverride: &schema.Schema{
    95  							Type:     schema.TypeString,
    96  							Computed: true,
    97  						},
    98  						catalogServiceServiceID: &schema.Schema{
    99  							Type:     schema.TypeString,
   100  							Computed: true,
   101  						},
   102  						catalogServiceServiceName: &schema.Schema{
   103  							Type:     schema.TypeString,
   104  							Computed: true,
   105  						},
   106  						catalogServiceServicePort: &schema.Schema{
   107  							Type:     schema.TypeString,
   108  							Computed: true,
   109  						},
   110  						catalogServiceServiceTags: &schema.Schema{
   111  							Type:     schema.TypeList,
   112  							Computed: true,
   113  							Elem:     &schema.Schema{Type: schema.TypeString},
   114  						},
   115  						catalogServiceTaggedAddresses: &schema.Schema{
   116  							Type:     schema.TypeMap,
   117  							Computed: true,
   118  							Elem: &schema.Resource{
   119  								Schema: map[string]*schema.Schema{
   120  									catalogNodesSchemaTaggedLAN: &schema.Schema{
   121  										Type:     schema.TypeString,
   122  										Computed: true,
   123  									},
   124  									catalogNodesSchemaTaggedWAN: &schema.Schema{
   125  										Type:     schema.TypeString,
   126  										Computed: true,
   127  									},
   128  								},
   129  							},
   130  						},
   131  					},
   132  				},
   133  			},
   134  		},
   135  	}
   136  }
   137  
   138  func dataSourceConsulCatalogServiceRead(d *schema.ResourceData, meta interface{}) error {
   139  	client := meta.(*consulapi.Client)
   140  
   141  	// Parse out data source filters to populate Consul's query options
   142  	queryOpts, err := getQueryOpts(d, client)
   143  	if err != nil {
   144  		return errwrap.Wrapf("unable to get query options for fetching catalog services: {{err}}", err)
   145  	}
   146  
   147  	var serviceName string
   148  	if v, ok := d.GetOk(catalogServiceName); ok {
   149  		serviceName = v.(string)
   150  	}
   151  
   152  	var serviceTag string
   153  	if v, ok := d.GetOk(catalogServiceTag); ok {
   154  		serviceTag = v.(string)
   155  	}
   156  
   157  	// services, meta, err := client.Catalog().Services(queryOpts)
   158  	services, meta, err := client.Catalog().Service(serviceName, serviceTag, queryOpts)
   159  	if err != nil {
   160  		return err
   161  	}
   162  
   163  	l := make([]interface{}, 0, len(services))
   164  	for _, service := range services {
   165  		const defaultServiceAttrs = 13
   166  		m := make(map[string]interface{}, defaultServiceAttrs)
   167  
   168  		m[catalogServiceCreateIndex] = fmt.Sprintf("%d", service.CreateIndex)
   169  		m[catalogServiceModifyIndex] = fmt.Sprintf("%d", service.ModifyIndex)
   170  		m[catalogServiceNodeAddress] = service.Address
   171  		m[catalogServiceNodeID] = service.ID
   172  		m[catalogServiceNodeMeta] = service.NodeMeta
   173  		m[catalogServiceNodeName] = service.Node
   174  		switch service.ServiceAddress {
   175  		case "":
   176  			m[catalogServiceServiceAddress] = service.Address
   177  		default:
   178  			m[catalogServiceServiceAddress] = service.ServiceAddress
   179  		}
   180  		m[catalogServiceServiceEnableTagOverride] = fmt.Sprintf("%t", service.ServiceEnableTagOverride)
   181  		m[catalogServiceServiceID] = service.ServiceID
   182  		m[catalogServiceServiceName] = service.ServiceName
   183  		m[catalogServiceServicePort] = fmt.Sprintf("%d", service.ServicePort)
   184  		sort.Strings(service.ServiceTags)
   185  		m[catalogServiceServiceTags] = service.ServiceTags
   186  		m[catalogServiceTaggedAddresses] = service.TaggedAddresses
   187  
   188  		l = append(l, m)
   189  	}
   190  
   191  	const idKeyFmt = "catalog-service-%s-%q-%q"
   192  	d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter, serviceName, serviceTag))
   193  
   194  	d.Set(catalogServiceDatacenter, queryOpts.Datacenter)
   195  	d.Set(catalogServiceName, serviceName)
   196  	d.Set(catalogServiceTag, serviceTag)
   197  	if err := d.Set(catalogServiceElem, l); err != nil {
   198  		return errwrap.Wrapf("Unable to store service: {{err}}", err)
   199  	}
   200  
   201  	return nil
   202  }