api: add agent self call

This commit is contained in:
Ryan Uber 2015-09-08 11:41:03 -07:00
parent 76c7584eaf
commit f5c57cedfa
3 changed files with 57 additions and 1 deletions

32
api/agent.go Normal file
View File

@ -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
}

24
api/agent_test.go Normal file
View File

@ -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)
}
}

View File

@ -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)