-
Notifications
You must be signed in to change notification settings - Fork 113
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" {} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 fromconsul_agent_self
(and add something reflective intoconsul_agent_config
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, added that!