Skip to content

Commit

Permalink
Push out the last of the docs and fixes for the `consul_catalog_nodes…
Browse files Browse the repository at this point in the history
…` data source.
  • Loading branch information
sean- committed Feb 16, 2017
1 parent b4c76c0 commit 400962f
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 61 deletions.
115 changes: 65 additions & 50 deletions builtin/providers/consul/data_source_consul_catalog_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,80 @@ import (
)

const (
queryOptNodesAttr = "nodes"

nodeID = "id"
nodeAddress = "address"
nodeMetaAttr = "meta"
nodeName = "name"
nodeTaggedAddresses = "tagged_addresses"

queryOpts = "query_options"

apiTaggedLAN = "lan"
apiTaggedWAN = "wan"
schemaTaggedLAN = "lan"
schemaTaggedWAN = "wan"
catalogNodesElem = "nodes"
catalogNodesDatacenter = "datacenter"
catalogNodesQueryOpts = "query_options"

catalogNodesNodeID = "id"
catalogNodesNodeAddress = "address"
catalogNodesNodeMeta = "meta"
catalogNodesNodeName = "name"
catalogNodesNodeTaggedAddresses = "tagged_addresses"

catalogNodesNodeIDs = "node_ids"
catalogNodesNodeNames = "node_names"

catalogNodesAPITaggedLAN = "lan"
catalogNodesAPITaggedWAN = "wan"
catalogNodesSchemaTaggedLAN = "lan"
catalogNodesSchemaTaggedWAN = "wan"
)

func dataSourceConsulCatalogNodes() *schema.Resource {
return &schema.Resource{
Read: dataSourceConsulCatalogNodesRead,
Schema: map[string]*schema.Schema{
queryOpts: schemaQueryOpts,
queryOptNodesAttr: &schema.Schema{
// Filters
catalogNodesQueryOpts: schemaQueryOpts,

// Out parameters
catalogNodesDatacenter: &schema.Schema{
Computed: true,
Type: schema.TypeString,
},
catalogNodesNodeIDs: &schema.Schema{
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
},
catalogNodesNodeNames: &schema.Schema{
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
},
catalogNodesElem: &schema.Schema{
Computed: true,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
nodeID: &schema.Schema{
catalogNodesNodeID: &schema.Schema{
Type: schema.TypeString,
Computed: true,
ValidateFunc: makeValidationFunc(nodeID, []interface{}{validateRegexp(`^[\S]+$`)}),
ValidateFunc: makeValidationFunc(catalogNodesNodeID, []interface{}{validateRegexp(`^[\S]+$`)}),
},
nodeName: &schema.Schema{
catalogNodesNodeName: &schema.Schema{
Type: schema.TypeString,
Computed: true,
ValidateFunc: makeValidationFunc(nodeName, []interface{}{validateRegexp(`^[\S]+$`)}),
ValidateFunc: makeValidationFunc(catalogNodesNodeName, []interface{}{validateRegexp(`^[\S]+$`)}),
},
nodeAddress: &schema.Schema{
catalogNodesNodeAddress: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
nodeMetaAttr: &schema.Schema{
catalogNodesNodeMeta: &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
nodeTaggedAddresses: &schema.Schema{
catalogNodesNodeTaggedAddresses: &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
schemaTaggedLAN: &schema.Schema{
catalogNodesSchemaTaggedLAN: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
schemaTaggedWAN: &schema.Schema{
catalogNodesSchemaTaggedWAN: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
Expand Down Expand Up @@ -92,6 +112,9 @@ func dataSourceConsulCatalogNodesRead(d *schema.ResourceData, meta interface{})

l := make([]interface{}, 0, len(nodes))

nodeNames := make([]interface{}, 0, len(nodes))
nodeIDs := make([]interface{}, 0, len(nodes))

for _, node := range nodes {
const defaultNodeAttrs = 4
m := make(map[string]interface{}, defaultNodeAttrs)
Expand All @@ -100,39 +123,31 @@ func dataSourceConsulCatalogNodesRead(d *schema.ResourceData, meta interface{})
id = node.Node
}

m[nodeID] = id
m[nodeName] = node.Node
m[nodeAddress] = node.Address

{
const initNumTaggedAddrs = 2
taggedAddrs := make(map[string]interface{}, initNumTaggedAddrs)
if addr, found := node.TaggedAddresses[apiTaggedLAN]; found {
taggedAddrs[schemaTaggedLAN] = addr
}
if addr, found := node.TaggedAddresses[apiTaggedWAN]; found {
taggedAddrs[schemaTaggedWAN] = addr
}
m[nodeTaggedAddresses] = taggedAddrs
}
nodeIDs = append(nodeIDs, id)
nodeNames = append(nodeNames, node.Node)

{
const initNumMetaAddrs = 4
metaVals := make(map[string]interface{}, initNumMetaAddrs)
for s, t := range node.Meta {
metaVals[s] = t
}
m[nodeMetaAttr] = metaVals
}
m[catalogNodesNodeAddress] = node.Address
m[catalogNodesNodeID] = id
m[catalogNodesNodeName] = node.Node
m[catalogNodesNodeMeta] = node.Meta
m[catalogNodesNodeTaggedAddresses] = node.TaggedAddresses

l = append(l, m)
}

const idKeyFmt = "catalog-nodes-%s"
d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))

d.Set("datacenter", queryOpts.Datacenter)
if err := d.Set(queryOptNodesAttr, l); err != nil {
d.Set(catalogNodesDatacenter, queryOpts.Datacenter)
if err := d.Set(catalogNodesNodeIDs, nodeIDs); err != nil {
return errwrap.Wrapf("Unable to store node IDs: {{err}}", err)
}

if err := d.Set(catalogNodesNodeNames, nodeNames); err != nil {
return errwrap.Wrapf("Unable to store node names: {{err}}", err)
}

if err := d.Set(catalogNodesElem, l); err != nil {
return errwrap.Wrapf("Unable to store nodes: {{err}}", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func dataSourceConsulCatalogService() *schema.Resource {
Required: true,
Type: schema.TypeString,
},
queryOpts: schemaQueryOpts,
catalogNodesQueryOpts: schemaQueryOpts,

// Out parameters
catalogServiceElem: &schema.Schema{
Expand Down Expand Up @@ -117,11 +117,11 @@ func dataSourceConsulCatalogService() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
schemaTaggedLAN: &schema.Schema{
catalogNodesSchemaTaggedLAN: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
schemaTaggedWAN: &schema.Schema{
catalogNodesSchemaTaggedWAN: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func dataSourceConsulCatalogServices() *schema.Resource {
Type: schema.TypeString,
ForceNew: true,
},
queryOpts: schemaQueryOpts,
catalogNodesQueryOpts: schemaQueryOpts,

// Out parameters
catalogServicesNames: &schema.Schema{
Expand Down
83 changes: 83 additions & 0 deletions website/source/docs/providers/consul/d/nodes.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
layout: "consul"
page_title: "Consul: consul_catalog_nodes"
sidebar_current: "docs-consul-data-source-catalog-nodes"
description: |-
Provides a list of nodes in a given Consul datacenter.
---

# consul\_catalog\_nodes

The `consul_catalog_nodes` data source returns a list of Consul nodes that have
been registered with the Consul cluster in a given datacenter. By specifying a
different datacenter in the `query_options` it is possible to retrieve a list of
nodes from a different WAN-attached Consul datacenter.

## Example Usage

```
data "consul_catalog_nodes" "read-dc1-nodes" {
# query_options {
# # Optional parameter: implicitly uses the current datacenter of the agent
# datacenter = "dc1"
# }
}
# Set the description to a whitespace delimited list of the node names
resource "example_resource" "app" {
description = "${join(" ", formatlist("%s", data.consul_catalog_nodes.node_names))}"
...
}
```

## 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.

* `query_options` - (Optional) See below.

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.
* `node_ids` - A list of the Consul node IDs.
* `node_names` - A list of the Consul node names.
* `nodes` - A list of nodes and details about each Consul agent. The list of
per-node attributes is detailed below.

The following is a list of the per-node attributes contained within the `nodes`
map:

* `id` - The Node ID of the Consul agent.
* [`meta`](https://www.consul.io/docs/agent/http/catalog.html#Meta) - Node meta
data tag information, if any.
* [`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#Address) - The
IP address the node is advertising to the Consul cluster.
* [`tagged_addresses`](https://www.consul.io/docs/agent/http/catalog.html#TaggedAddresses) -
List of explicit LAN and WAN IP addresses for the agent.
12 changes: 5 additions & 7 deletions website/source/docs/providers/consul/d/services.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ source, which provides a detailed response about a specific Consul service.

```
data "consul_catalog_services" "read-dc1" {
query_options {
# Optional parameter
datacenter = "dc1"
}
# query_options {
# # Optional parameter: implicitly uses the current datacenter of the agent
# datacenter = "dc1"
# }
}
# Set the description to a whitespace delimited list of the services
Expand Down Expand Up @@ -71,9 +71,7 @@ The following attributes are exported:
list of services found.
* `services.<service>` - For each name given, the corresponding attribute is a
Terraform map of services and their tags. The value is an alphanumerically
sorted, whitespace delimited set of tags associated with the service. As
shown in the example above, to create a list of the available servies, wrap
the `names` attribute in a call to `keys()`.
sorted, whitespace delimited set of tags associated with the service.
* `tags` - A map of the tags found for each service. If more than one service
shares the same tag, unique service names will be joined by whitespace (this
is the inverse of `services` and can be used to lookup the services that match
Expand Down

0 comments on commit 400962f

Please sign in to comment.