2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
// Status can be used to query the Status endpoints
|
|
|
|
type Status struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns a handle to the status endpoints
|
|
|
|
func (c *Client) Status() *Status {
|
|
|
|
return &Status{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leader is used to query for a known leader
|
2020-07-29 16:09:15 +00:00
|
|
|
func (s *Status) LeaderWithQueryOptions(q *QueryOptions) (string, error) {
|
2015-01-06 18:40:00 +00:00
|
|
|
r := s.c.newRequest("GET", "/v1/status/leader")
|
2020-07-29 16:09:15 +00:00
|
|
|
|
|
|
|
if q != nil {
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:24:23 +00:00
|
|
|
_, resp, err := s.c.doRequest(r)
|
2015-01-06 18:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-06-14 22:49:32 +00:00
|
|
|
defer closeResponseBody(resp)
|
2021-10-28 16:24:23 +00:00
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
var leader string
|
|
|
|
if err := decodeBody(resp, &leader); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return leader, nil
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:09:15 +00:00
|
|
|
func (s *Status) Leader() (string, error) {
|
|
|
|
return s.LeaderWithQueryOptions(nil)
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// Peers is used to query for a known raft peers
|
2020-07-29 16:09:15 +00:00
|
|
|
func (s *Status) PeersWithQueryOptions(q *QueryOptions) ([]string, error) {
|
2015-01-06 18:40:00 +00:00
|
|
|
r := s.c.newRequest("GET", "/v1/status/peers")
|
2020-07-29 16:09:15 +00:00
|
|
|
|
|
|
|
if q != nil {
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:24:23 +00:00
|
|
|
_, resp, err := s.c.doRequest(r)
|
2015-01-06 18:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-14 22:49:32 +00:00
|
|
|
defer closeResponseBody(resp)
|
2021-10-28 16:24:23 +00:00
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
|
|
|
|
var peers []string
|
|
|
|
if err := decodeBody(resp, &peers); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return peers, nil
|
|
|
|
}
|
2020-07-29 16:09:15 +00:00
|
|
|
|
|
|
|
func (s *Status) Peers() ([]string, error) {
|
|
|
|
return s.PeersWithQueryOptions(nil)
|
|
|
|
}
|