api: Support QueryOptions on additional agent endpoints (#10691)

Add support for setting QueryOptions on the following agent API endpoints:

- /agent/health/service/name/:name
- /agent/health/service/id/:id
- /agent/service/maintenance/:id

This follows the same pattern used in #9903 to support query options
for other agent API endpoints.

Resolves #9710
This commit is contained in:
Blake Covarrubias 2021-07-30 10:07:13 -07:00 committed by GitHub
parent 58bc20cc88
commit 766b64eac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 6 deletions

3
.changelog/10691.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
api: Enable setting query options on agent health and maintenance endpoints.
```

View File

@ -591,8 +591,13 @@ func (a *Agent) ServicesWithFilterOpts(filter string, q *QueryOptions) (map[stri
// - If the service is found, will return (critical|passing|warning), AgentServiceChecksInfo, nil) // - If the service is found, will return (critical|passing|warning), AgentServiceChecksInfo, nil)
// - In all other cases, will return an error // - In all other cases, will return an error
func (a *Agent) AgentHealthServiceByID(serviceID string) (string, *AgentServiceChecksInfo, error) { func (a *Agent) AgentHealthServiceByID(serviceID string) (string, *AgentServiceChecksInfo, error) {
return a.AgentHealthServiceByIDOpts(serviceID, nil)
}
func (a *Agent) AgentHealthServiceByIDOpts(serviceID string, q *QueryOptions) (string, *AgentServiceChecksInfo, error) {
path := fmt.Sprintf("/v1/agent/health/service/id/%v", url.PathEscape(serviceID)) path := fmt.Sprintf("/v1/agent/health/service/id/%v", url.PathEscape(serviceID))
r := a.c.newRequest("GET", path) r := a.c.newRequest("GET", path)
r.setQueryOptions(q)
r.params.Add("format", "json") r.params.Add("format", "json")
r.header.Set("Accept", "application/json") r.header.Set("Accept", "application/json")
_, resp, err := a.c.doRequest(r) _, resp, err := a.c.doRequest(r)
@ -625,8 +630,13 @@ func (a *Agent) AgentHealthServiceByID(serviceID string) (string, *AgentServiceC
// - If the service is found, will return (critical|passing|warning), []api.AgentServiceChecksInfo, nil) // - If the service is found, will return (critical|passing|warning), []api.AgentServiceChecksInfo, nil)
// - In all other cases, will return an error // - In all other cases, will return an error
func (a *Agent) AgentHealthServiceByName(service string) (string, []AgentServiceChecksInfo, error) { func (a *Agent) AgentHealthServiceByName(service string) (string, []AgentServiceChecksInfo, error) {
return a.AgentHealthServiceByNameOpts(service, nil)
}
func (a *Agent) AgentHealthServiceByNameOpts(service string, q *QueryOptions) (string, []AgentServiceChecksInfo, error) {
path := fmt.Sprintf("/v1/agent/health/service/name/%v", url.PathEscape(service)) path := fmt.Sprintf("/v1/agent/health/service/name/%v", url.PathEscape(service))
r := a.c.newRequest("GET", path) r := a.c.newRequest("GET", path)
r.setQueryOptions(q)
r.params.Add("format", "json") r.params.Add("format", "json")
r.header.Set("Accept", "application/json") r.header.Set("Accept", "application/json")
_, resp, err := a.c.doRequest(r) _, resp, err := a.c.doRequest(r)
@ -1028,7 +1038,12 @@ func (a *Agent) ConnectCALeaf(serviceID string, q *QueryOptions) (*LeafCert, *Qu
// EnableServiceMaintenance toggles service maintenance mode on // EnableServiceMaintenance toggles service maintenance mode on
// for the given service ID. // for the given service ID.
func (a *Agent) EnableServiceMaintenance(serviceID, reason string) error { func (a *Agent) EnableServiceMaintenance(serviceID, reason string) error {
return a.EnableServiceMaintenanceOpts(serviceID, reason, nil)
}
func (a *Agent) EnableServiceMaintenanceOpts(serviceID, reason string, q *QueryOptions) error {
r := a.c.newRequest("PUT", "/v1/agent/service/maintenance/"+serviceID) r := a.c.newRequest("PUT", "/v1/agent/service/maintenance/"+serviceID)
r.setQueryOptions(q)
r.params.Set("enable", "true") r.params.Set("enable", "true")
r.params.Set("reason", reason) r.params.Set("reason", reason)
_, resp, err := requireOK(a.c.doRequest(r)) _, resp, err := requireOK(a.c.doRequest(r))
@ -1042,7 +1057,12 @@ func (a *Agent) EnableServiceMaintenance(serviceID, reason string) error {
// DisableServiceMaintenance toggles service maintenance mode off // DisableServiceMaintenance toggles service maintenance mode off
// for the given service ID. // for the given service ID.
func (a *Agent) DisableServiceMaintenance(serviceID string) error { func (a *Agent) DisableServiceMaintenance(serviceID string) error {
return a.DisableServiceMaintenanceOpts(serviceID, nil)
}
func (a *Agent) DisableServiceMaintenanceOpts(serviceID string, q *QueryOptions) error {
r := a.c.newRequest("PUT", "/v1/agent/service/maintenance/"+serviceID) r := a.c.newRequest("PUT", "/v1/agent/service/maintenance/"+serviceID)
r.setQueryOptions(q)
r.params.Set("enable", "false") r.params.Set("enable", "false")
_, resp, err := requireOK(a.c.doRequest(r)) _, resp, err := requireOK(a.c.doRequest(r))
if err != nil { if err != nil {

View File

@ -1322,7 +1322,7 @@ func TestAPI_AgentMonitorJSON(t *testing.T) {
}) })
} }
func TestAPI_ServiceMaintenance(t *testing.T) { func TestAPI_ServiceMaintenanceOpts(t *testing.T) {
t.Parallel() t.Parallel()
c, s := makeClient(t) c, s := makeClient(t)
defer s.Stop() defer s.Stop()
@ -1337,8 +1337,11 @@ func TestAPI_ServiceMaintenance(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Specify namespace in query option
opts := &QueryOptions{Namespace: defaultNamespace}
// Enable maintenance mode // Enable maintenance mode
if err := agent.EnableServiceMaintenance("redis", "broken"); err != nil { if err := agent.EnableServiceMaintenanceOpts("redis", "broken", opts); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -1361,7 +1364,7 @@ func TestAPI_ServiceMaintenance(t *testing.T) {
} }
// Disable maintenance mode // Disable maintenance mode
if err := agent.DisableServiceMaintenance("redis"); err != nil { if err := agent.DisableServiceMaintenanceOpts("redis", opts); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -1641,7 +1644,7 @@ func TestAPI_AgentConnectAuthorize(t *testing.T) {
require.Equal(auth.Reason, "ACLs disabled, access is allowed by default") require.Equal(auth.Reason, "ACLs disabled, access is allowed by default")
} }
func TestAPI_AgentHealthService(t *testing.T) { func TestAPI_AgentHealthServiceOpts(t *testing.T) {
t.Parallel() t.Parallel()
c, s := makeClient(t) c, s := makeClient(t)
defer s.Stop() defer s.Stop()
@ -1651,7 +1654,8 @@ func TestAPI_AgentHealthService(t *testing.T) {
requireServiceHealthID := func(t *testing.T, serviceID, expected string, shouldExist bool) { requireServiceHealthID := func(t *testing.T, serviceID, expected string, shouldExist bool) {
msg := fmt.Sprintf("service id:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceID, shouldExist, expected) msg := fmt.Sprintf("service id:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceID, shouldExist, expected)
state, out, err := agent.AgentHealthServiceByID(serviceID) opts := &QueryOptions{Namespace: defaultNamespace}
state, out, err := agent.AgentHealthServiceByIDOpts(serviceID, opts)
require.Nil(t, err, msg, "err") require.Nil(t, err, msg, "err")
require.Equal(t, expected, state, msg, "state") require.Equal(t, expected, state, msg, "state")
if !shouldExist { if !shouldExist {
@ -1664,7 +1668,8 @@ func TestAPI_AgentHealthService(t *testing.T) {
requireServiceHealthName := func(t *testing.T, serviceName, expected string, shouldExist bool) { requireServiceHealthName := func(t *testing.T, serviceName, expected string, shouldExist bool) {
msg := fmt.Sprintf("service name:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceName, shouldExist, expected) msg := fmt.Sprintf("service name:%s, shouldExist:%v, expectedStatus:%s : bad %%s", serviceName, shouldExist, expected)
state, outs, err := agent.AgentHealthServiceByName(serviceName) opts := &QueryOptions{Namespace: defaultNamespace}
state, outs, err := agent.AgentHealthServiceByNameOpts(serviceName, opts)
require.Nil(t, err, msg, "err") require.Nil(t, err, msg, "err")
require.Equal(t, expected, state, msg, "state") require.Equal(t, expected, state, msg, "state")
if !shouldExist { if !shouldExist {