api: add agent self call
This commit is contained in:
parent
76c7584eaf
commit
f5c57cedfa
|
@ -0,0 +1,32 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Agent encapsulates an API client which talks to Nomad's
|
||||
// agent endpoints for a specific node.
|
||||
type Agent struct {
|
||||
client *Client
|
||||
node string
|
||||
}
|
||||
|
||||
// Agent returns a new agent which can be used to query
|
||||
// the agent-specific endpoints.
|
||||
func (c *Client) Agent() *Agent {
|
||||
return &Agent{client: c}
|
||||
}
|
||||
|
||||
// Self is used to query the /v1/agent/self endpoint and
|
||||
// returns information specific to the running agent.
|
||||
func (a *Agent) Self() (map[string]map[string]interface{}, error) {
|
||||
var out map[string]map[string]interface{}
|
||||
|
||||
// Query the self endpoint on the agent
|
||||
_, err := a.client.query("/v1/agent/self", &out, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed querying self endpoint: %s", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAgent_Self(t *testing.T) {
|
||||
c, s := makeClient(t, nil, nil)
|
||||
defer s.Stop()
|
||||
|
||||
// Get a handle on the Agent endpoints
|
||||
a := c.Agent()
|
||||
|
||||
// Query the endpoint
|
||||
res, err := a.Self()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Check that we got a valid response
|
||||
if name, ok := res["member"]["Name"]; !ok || name == "" {
|
||||
t.Fatalf("bad member name in response: %#v", res)
|
||||
}
|
||||
}
|
|
@ -22,7 +22,7 @@ func makeClient(t *testing.T, cb1 configCallback,
|
|||
|
||||
// Create server
|
||||
server := testutil.NewTestServer(t, cb2)
|
||||
conf.URL = server.HTTPAddr
|
||||
conf.URL = "http://" + server.HTTPAddr
|
||||
|
||||
// Create client
|
||||
client, err := NewClient(conf)
|
||||
|
|
Loading…
Reference in New Issue