Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New data source: consul_agent_config #42

Merged
merged 3 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions consul/data_source_consul_agent_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package consul

import (
"fmt"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceConsulAgentConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceConsulAgentConfigRead,

Schema: map[string]*schema.Schema{
"datacenter": &schema.Schema{
Type: schema.TypeString,
Description: "The datacenter the agent is running in",
Computed: true,
},

"node_id": &schema.Schema{
Type: schema.TypeString,
Description: "The ID of the node the agent is running on",
Computed: true,
},

"node_name": &schema.Schema{
Type: schema.TypeString,
Description: "The name of the node the agent is running on",
Computed: true,
},

"server": &schema.Schema{
Type: schema.TypeBool,
Description: "If the agent is a server or not",
Computed: true,
},

"revision": &schema.Schema{
Type: schema.TypeString,
Description: "The VCS revision of the build of Consul that is running",
Computed: true,
},

"version": &schema.Schema{
Type: schema.TypeString,
Description: "The version of the build of Consul that is running",
Computed: true,
},
},
}
}

func dataSourceConsulAgentConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*consulapi.Client)
agentSelf, err := client.Agent().Self()
if err != nil {
return err
}

config, ok := agentSelf["Config"]
if !ok {
return fmt.Errorf("Config key not present on agent self endpoint")
}

// We use the ID of the node as the datasource ID, as the datasource
// queries config from the agent running on that registered node, so it
// is the best we can do to get a consistent identifier
d.SetId(fmt.Sprintf("agent-%s", config["NodeId"]))

d.Set("datacenter", config["Datacenter"])
d.Set("node_id", config["NodeID"])
d.Set("node_name", config["NodeName"])
d.Set("server", config["Server"])
d.Set("revision", config["Revision"])
d.Set("version", config["Version"])

return nil
}
31 changes: 31 additions & 0 deletions consul/data_source_consul_agent_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package consul

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataConsulAgentConfig_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataConsulAgentConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.consul_agent_config.example", "datacenter", "dc1"),
resource.TestCheckResourceAttr("data.consul_agent_config.example", "server", "true"),
resource.TestCheckResourceAttrSet("data.consul_agent_config.example", "node_name"),
resource.TestCheckResourceAttrSet("data.consul_agent_config.example", "node_id"),
resource.TestCheckResourceAttrSet("data.consul_agent_config.example", "revision"),
resource.TestCheckResourceAttrSet("data.consul_agent_config.example", "version"),
),
},
},
})
}

const testAccDataConsulAgentConfig = `
data "consul_agent_config" "example" {}
`
1 change: 1 addition & 0 deletions consul/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Provider() terraform.ResourceProvider {

DataSourcesMap: map[string]*schema.Resource{
"consul_agent_self": dataSourceConsulAgentSelf(),
"consul_agent_config": dataSourceConsulAgentConfig(),
"consul_catalog_nodes": dataSourceConsulCatalogNodes(),
"consul_catalog_service": dataSourceConsulCatalogService(),
"consul_catalog_services": dataSourceConsulCatalogServices(),
Expand Down
114 changes: 64 additions & 50 deletions website/consul.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,77 @@
<div class="docs-sidebar hidden-print affix-top" role="complementary">
<ul class="nav docs-sidenav">
<li<%= sidebar_current("docs-home") %>>
<a href="/docs/providers/index.html">All Providers</a>
</li>
<a href="/docs/providers/index.html">All Providers</a>
</li>

<li<%= sidebar_current("docs-consul-index") %>>
<a href="/docs/providers/consul/index.html">Consul Provider</a>
</li>
<a href="/docs/providers/consul/index.html">Consul Provider</a>
</li>

<li<%= sidebar_current("docs-consul-data-source") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-consul-data-source-agent-self") %>>
<a href="/docs/providers/consul/d/agent_self.html">consul_agent_self</a>
</li>
<li<%= sidebar_current("docs-consul-data-source-catalog-nodes") %>>
<a href="/docs/providers/consul/d/nodes.html">consul_catalog_nodes</a>
</li>
<li<%= sidebar_current("docs-consul-data-source-catalog-service") %>>
<a href="/docs/providers/consul/d/service.html">consul_catalog_service</a>
</li>
<li<%= sidebar_current("docs-consul-data-source-catalog-services") %>>
<a href="/docs/providers/consul/d/services.html">consul_catalog_services</a>
</li>
<li<%= sidebar_current("docs-consul-data-source-keys") %>>
<a href="/docs/providers/consul/d/keys.html">consul_keys</a>
</li>
<li<%= sidebar_current("docs-consul-data-source-key-prefix") %>>
<a href="/docs/providers/consul/d/key_prefix.html">consul_key_prefix</a>
</li>
</ul>
</li>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-consul-data-source-agent-self") %>>
<a href="/docs/providers/consul/d/agent_self.html">consul_agent_self</a>
</li>

<li<%= sidebar_current("docs-consul-resource") %>>
<a href="#">Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-consul-resource-agent-service") %>>
<a href="/docs/providers/consul/r/agent_service.html">consul_agent_service</a>
</li>
<li<%= sidebar_current("docs-consul-resource-catalog-entry") %>>
<a href="/docs/providers/consul/r/catalog_entry.html">consul_catalog_entry</a>
</li>
<li<%= sidebar_current("docs-consul-resource-keys") %>>
<a href="/docs/providers/consul/r/keys.html">consul_keys</a>
</li>
<li<%= sidebar_current("docs-consul-resource-key-prefix") %>>
<a href="/docs/providers/consul/r/key_prefix.html">consul_key_prefix</a>
</li>
<li<%= sidebar_current("docs-consul-resource-node") %>>
<a href="/docs/providers/consul/r/node.html">consul_node</a>
</li>
<li<%= sidebar_current("docs-consul-resource-prepared-query") %>>
<a href="/docs/providers/consul/r/prepared_query.html">consul_prepared_query</a>
</li>
<li<%= sidebar_current("docs-consul-resource-service") %>>
<a href="/docs/providers/consul/r/service.html">consul_service</a>
</li>
<li<%= sidebar_current("docs-consul-data-source-agent-config") %>>
<a href="/docs/providers/consul/d/agent_config.html">consul_agent_config</a>
</li>

<li<%= sidebar_current("docs-consul-data-source-catalog-nodes") %>>
<a href="/docs/providers/consul/d/nodes.html">consul_catalog_nodes</a>
</li>

<li<%= sidebar_current("docs-consul-data-source-catalog-service") %>>
<a href="/docs/providers/consul/d/service.html">consul_catalog_service</a>
</li>

<li<%= sidebar_current("docs-consul-data-source-catalog-services") %>>
<a href="/docs/providers/consul/d/services.html">consul_catalog_services</a>
</li>

<li<%= sidebar_current("docs-consul-data-source-keys") %>>
<a href="/docs/providers/consul/d/keys.html">consul_keys</a>
</li>

<li<%= sidebar_current("docs-consul-data-source-key-prefix") %>>
<a href="/docs/providers/consul/d/key_prefix.html">consul_key_prefix</a>
</li>
</ul>
</li>

<li<%= sidebar_current("docs-consul-resource") %>>
<a href="#">Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-consul-resource-agent-service") %>>
<a href="/docs/providers/consul/r/agent_service.html">consul_agent_service</a>
</li>

<li<%= sidebar_current("docs-consul-resource-catalog-entry") %>>
<a href="/docs/providers/consul/r/catalog_entry.html">consul_catalog_entry</a>
</li>

<li<%= sidebar_current("docs-consul-resource-keys") %>>
<a href="/docs/providers/consul/r/keys.html">consul_keys</a>
</li>

<li<%= sidebar_current("docs-consul-resource-key-prefix") %>>
<a href="/docs/providers/consul/r/key_prefix.html">consul_key_prefix</a>
</li>

<li<%= sidebar_current("docs-consul-resource-node") %>>
<a href="/docs/providers/consul/r/node.html">consul_node</a>
</li>

<li<%= sidebar_current("docs-consul-resource-prepared-query") %>>
<a href="/docs/providers/consul/r/prepared_query.html">consul_prepared_query</a>
</li>

<li<%= sidebar_current("docs-consul-resource-service") %>>
<a href="/docs/providers/consul/r/service.html">consul_service</a>
</li>
</ul>
</li>
</ul>
</div>
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/agent_config.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
layout: "consul"
page_title: "Consul: consul_agent_config"
sidebar_current: "docs-consul-data-source-agent-config"
description: |-
Provides the configuration information of the local Consul agent.
---

# consul_agent_config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to include a ~> **NOTE:** regarding how this data source differs from consul_agent_self (and add something reflective into consul_agent_config)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, added that!


The `consul_agent_config` data source returns
[configuration and status data](https://www.consul.io/docs/agent/http/agent.html#agent_self)
from the agent specified in the `provider`.

## Example Usage

```hcl
data "consul_agent_config" "remote_agent" {}

output "info" {
consul_version = "${data.consul_agent_config.version}"
}
```

## Attributes Reference

The following attributes are exported:

* `datacenter` - The datacenter the agent is running in
* `node_id` - The ID of the node the agent is running on
* `node_name` - The name of the node the agent is running on
* `server` - Boolean if the agent is a server or not
* `revision` - The first 9 characters of the VCS revision of the build of Consul that is running
* `version` - The version of the build of Consul that is running