From 1c7a9242583a984aa525754846be41951c4f4a92 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Wed, 15 Feb 2017 13:30:10 -0800 Subject: [PATCH] Update and provide docs to the `consul_catalog_service` data source. --- .../data_source_consul_catalog_service.go | 26 ++-- builtin/providers/consul/query_options.go | 3 - .../providers/consul/d/service.html.markdown | 112 ++++++++++++++++++ website/source/layouts/consul.erb | 3 + 4 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 website/source/docs/providers/consul/d/service.html.markdown diff --git a/builtin/providers/consul/data_source_consul_catalog_service.go b/builtin/providers/consul/data_source_consul_catalog_service.go index 7f41b66a3..71dc00780 100644 --- a/builtin/providers/consul/data_source_consul_catalog_service.go +++ b/builtin/providers/consul/data_source_consul_catalog_service.go @@ -2,6 +2,7 @@ package consul import ( "fmt" + "sort" consulapi "github.com/hashicorp/consul/api" "github.com/hashicorp/errwrap" @@ -9,7 +10,7 @@ import ( ) const ( - catalogServiceElem = "services" + catalogServiceElem = "service" catalogServiceCreateIndex = "create_index" catalogServiceDatacenter = "datacenter" @@ -170,26 +171,20 @@ func dataSourceConsulCatalogServiceRead(d *schema.ResourceData, meta interface{} m[catalogServiceNodeID] = service.ID m[catalogServiceNodeMeta] = service.NodeMeta m[catalogServiceNodeName] = service.Node - m[catalogServiceServiceAddress] = service.ServiceAddress + switch service.ServiceAddress { + case "": + m[catalogServiceServiceAddress] = service.Address + default: + m[catalogServiceServiceAddress] = service.ServiceAddress + } m[catalogServiceServiceEnableTagOverride] = fmt.Sprintf("%t", service.ServiceEnableTagOverride) m[catalogServiceServiceID] = service.ServiceID m[catalogServiceServiceName] = service.ServiceName m[catalogServiceServicePort] = fmt.Sprintf("%d", service.ServicePort) + sort.Strings(service.ServiceTags) m[catalogServiceServiceTags] = service.ServiceTags m[catalogServiceTaggedAddresses] = service.TaggedAddresses - { - const initNumTaggedAddrs = 2 - taggedAddrs := make(map[string]interface{}, initNumTaggedAddrs) - if addr, found := service.TaggedAddresses[apiTaggedLAN]; found { - taggedAddrs[schemaTaggedLAN] = addr - } - if addr, found := service.TaggedAddresses[apiTaggedWAN]; found { - taggedAddrs[schemaTaggedWAN] = addr - } - m[catalogServiceTaggedAddresses] = taggedAddrs - } - l = append(l, m) } @@ -197,9 +192,10 @@ func dataSourceConsulCatalogServiceRead(d *schema.ResourceData, meta interface{} d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter, serviceName, serviceTag)) d.Set(catalogServiceDatacenter, queryOpts.Datacenter) + d.Set(catalogServiceName, serviceName) d.Set(catalogServiceTag, serviceTag) if err := d.Set(catalogServiceElem, l); err != nil { - return errwrap.Wrapf("Unable to store services: {{err}}", err) + return errwrap.Wrapf("Unable to store service: {{err}}", err) } return nil diff --git a/builtin/providers/consul/query_options.go b/builtin/providers/consul/query_options.go index 41348fba5..1cce0fabe 100644 --- a/builtin/providers/consul/query_options.go +++ b/builtin/providers/consul/query_options.go @@ -49,12 +49,10 @@ var schemaQueryOpts = &schema.Schema{ }, queryOptToken: &schema.Schema{ Optional: true, - Default: true, Type: schema.TypeString, }, queryOptWaitIndex: &schema.Schema{ Optional: true, - Default: true, Type: schema.TypeInt, ValidateFunc: makeValidationFunc(queryOptWaitIndex, []interface{}{ validateIntMin(0), @@ -62,7 +60,6 @@ var schemaQueryOpts = &schema.Schema{ }, queryOptWaitTime: &schema.Schema{ Optional: true, - Default: true, Type: schema.TypeString, ValidateFunc: makeValidationFunc(queryOptWaitTime, []interface{}{ validateDurationMin("0ns"), diff --git a/website/source/docs/providers/consul/d/service.html.markdown b/website/source/docs/providers/consul/d/service.html.markdown new file mode 100644 index 000000000..d0cf8a476 --- /dev/null +++ b/website/source/docs/providers/consul/d/service.html.markdown @@ -0,0 +1,112 @@ +--- +layout: "consul" +page_title: "Consul: consul_catalog_service" +sidebar_current: "docs-consul-data-source-catalog-service" +description: |- + Provides details about a specific Consul service +--- + +# consul\_catalog\_service + +`consul_catalog_service` provides details about a specific Consul service in a +given datacenter. The results include a list of nodes advertising the specified +service, the node's IP address, port number, node ID, etc. By specifying a +different datacenter in the `query_options` it is possible to retrieve a list of +services from a different WAN-attached Consul datacenter. + +This data source is different from the `consul_catalog_services` (plural) data +source, which provides a summary of the current Consul services. + +## Example Usage + +``` +data "consul_catalog_service" "read-consul-dc1" { + # query_options { + # # Optional parameter: implicitly uses the current datacenter of the agent + # datacenter = "dc1" + # } + + name = "consul" +} + +# Set the description to a whitespace delimited list of the node names +resource "example_resource" "app" { + description = "${join(" ", data.consul_catalog_service.nodes)}" + ... +} +``` + +## Argument Reference + +The following arguments are supported: + +* `datacenter` - (Optional) The Consul datacenter to query. Defaults to the + same value found in `query_options` parameter specified below, or if that is + empty, the `datacenter` value found in the Consul agent that this provider is + configured to talk to. + +* `name` - (Required) The service name to select. + +* `query_options` - (Optional) See below. + +* `tag` - (Optional) A single tag that can be used to filter the list of nodes + to return based on a single matching tag.. + +The `query_options` block supports the following: + +* `allow_stale` - (Optional) When `true`, the default, allow responses from + Consul servers that are followers. + +* `require_consistent` - (Optional) When `true` force the client to perform a + read on at least quorum servers and verify the result is the same. Defaults + to `false`. + +* `token` - (Optional) Specify the Consul ACL token to use when performing the + request. This defaults to the same API token configured by the `consul` + provider but may be overriden if necessary. + +* `wait_index` - (Optional) Index number used to enable blocking quereis. + +* `wait_time` - (Optional) Max time the client should wait for a blocking query + to return. + +## Attributes Reference + +The following attributes are exported: + +* `datacenter` - The datacenter the keys are being read from to. +* `name` - The name of the service +* `tag` - The name of the tag used to filter the list of nodes in `service`. +* `service` - A list of nodes and details about each endpoint advertising a + service. Each element in the list is a map of attributes that correspond to + each individual node. The list of per-node attributes is detailed below. + +The following is a list of the per-node `service` attributes: + +* [`create_index`](https://www.consul.io/docs/agent/http/catalog.html#CreateIndex) - + The index entry at which point this entry was added to the catalog. +* [`modify_index`](https://www.consul.io/docs/agent/http/catalog.html#ModifyIndex) - + The index entry at which point this entry was modified in the catalog. +* [`node_address`](https://www.consul.io/docs/agent/http/catalog.html#Address) - + The address of the Consul node advertising the service. +* `node_id` - The Node ID of the Consul agent advertising the service. +* [`node_meta`](https://www.consul.io/docs/agent/http/catalog.html#Meta) - Node + meta data tag information, if any. +* [`node_name`](https://www.consul.io/docs/agent/http/catalog.html#Node) - The + name of the Consul node. +* [`address`](https://www.consul.io/docs/agent/http/catalog.html#ServiceAddress) - + The IP address of the service. If the `ServiceAddress` in the Consul catalog + is empty, this value is automatically populated with the `node_address` (the + `Address` in the Consul Catalog). +* [`enable_tag_override`](https://www.consul.io/docs/agent/http/catalog.html#ServiceEnableTagOverride) - + Whether service tags can be overridden on this service. +* [`id`](https://www.consul.io/docs/agent/http/catalog.html#ServiceID) - A + unique service instance identifier. +* [`name`](https://www.consul.io/docs/agent/http/catalog.html#ServiceName) - The + name of the service. +* [`port`](https://www.consul.io/docs/agent/http/catalog.html#ServicePort) - + Port number of the service. +* [`tagged_addresses`](https://www.consul.io/docs/agent/http/catalog.html#TaggedAddresses) - + List of explicit LAN and WAN IP addresses for the agent. +* [`tags`](https://www.consul.io/docs/agent/http/catalog.html#ServiceTags) - + List of tags for the service. diff --git a/website/source/layouts/consul.erb b/website/source/layouts/consul.erb index 32e0efa1d..ce12fc25e 100644 --- a/website/source/layouts/consul.erb +++ b/website/source/layouts/consul.erb @@ -13,6 +13,9 @@ > Data Sources