mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
api: add status endpoint
This commit is contained in:
32
api/status.go
Normal file
32
api/status.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package api
|
||||
|
||||
// Status is used to query the status-related endpoints.
|
||||
type Status struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Status returns a handle on the status endpoints.
|
||||
func (c *Client) Status() *Status {
|
||||
return &Status{client: c}
|
||||
}
|
||||
|
||||
// Leader is used to query for the current cluster leader.
|
||||
func (s *Status) Leader() (string, error) {
|
||||
var resp string
|
||||
_, err := s.client.query("/v1/status/leader", &resp, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Peers is used to query the addresses of the server peers
|
||||
// in the cluster.
|
||||
func (s *Status) Peers() ([]string, error) {
|
||||
var resp []string
|
||||
_, err := s.client.query("/v1/status/peers", &resp, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
41
api/status_test.go
Normal file
41
api/status_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/nomad/testutil"
|
||||
)
|
||||
|
||||
func TestStatus_Leader(t *testing.T) {
|
||||
c, s := makeClient(t, nil, nil)
|
||||
defer s.Stop()
|
||||
status := c.Status()
|
||||
|
||||
// Query for leader status should return a result
|
||||
out, err := status.Leader()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if out == "" {
|
||||
t.Fatalf("expected leader, got: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatus_Leader_NoLeader(t *testing.T) {
|
||||
// Start a server without bootstrap mode. This prevents
|
||||
// the leadership from being acquired.
|
||||
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
||||
c.Server.Bootstrap = false
|
||||
})
|
||||
defer s.Stop()
|
||||
status := c.Status()
|
||||
|
||||
// Query for leader status should return nothing.
|
||||
out, err := status.Leader()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if out != "" {
|
||||
t.Fatalf("expected no leader, got: %q", out)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user