/
data_source_consul_agent_config.go
78 lines (65 loc) · 2.03 KB
/
data_source_consul_agent_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
}