api: allow querying node name from agent
This commit is contained in:
parent
f5c57cedfa
commit
c1f4faaab3
22
api/agent.go
22
api/agent.go
|
@ -8,7 +8,9 @@ import (
|
|||
// agent endpoints for a specific node.
|
||||
type Agent struct {
|
||||
client *Client
|
||||
node string
|
||||
|
||||
// Cache static agent info
|
||||
nodeName string
|
||||
}
|
||||
|
||||
// Agent returns a new agent which can be used to query
|
||||
|
@ -30,3 +32,21 @@ func (a *Agent) Self() (map[string]map[string]interface{}, error) {
|
|||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// NodeName is used to query the Nomad agent for its node name.
|
||||
func (a *Agent) NodeName() (string, error) {
|
||||
// Return from cache if we have it
|
||||
if a.nodeName != "" {
|
||||
return a.nodeName, nil
|
||||
}
|
||||
|
||||
// Query the node name
|
||||
info, err := a.Self()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if name, ok := info["member"]["Name"]; ok {
|
||||
a.nodeName = name.(string)
|
||||
}
|
||||
return a.nodeName, nil
|
||||
}
|
||||
|
|
|
@ -22,3 +22,25 @@ func TestAgent_Self(t *testing.T) {
|
|||
t.Fatalf("bad member name in response: %#v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgent_NodeName(t *testing.T) {
|
||||
c, s := makeClient(t, nil, nil)
|
||||
defer s.Stop()
|
||||
a := c.Agent()
|
||||
|
||||
// Query the agent for the node name
|
||||
res, err := a.NodeName()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Ensure we got a node name back
|
||||
if res == "" {
|
||||
t.Fatalf("expected node name, got nothing")
|
||||
}
|
||||
|
||||
// Check that we cached the node name
|
||||
if a.nodeName == "" {
|
||||
t.Fatalf("should have cached node name")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue