From 766b64eac2802a984025c0c85de540094a4307d6 Mon Sep 17 00:00:00 2001 From: Blake Covarrubias Date: Fri, 30 Jul 2021 10:07:13 -0700 Subject: [PATCH] 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 --- .changelog/10691.txt | 3 +++ api/agent.go | 20 ++++++++++++++++++++ api/agent_test.go | 17 +++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 .changelog/10691.txt diff --git a/.changelog/10691.txt b/.changelog/10691.txt new file mode 100644 index 000000000..f39cbee22 --- /dev/null +++ b/.changelog/10691.txt @@ -0,0 +1,3 @@ +```release-note:improvement +api: Enable setting query options on agent health and maintenance endpoints. +``` diff --git a/api/agent.go b/api/agent.go index f78f54d3b..d9f1723bb 100644 --- a/api/agent.go +++ b/api/agent.go @@ -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) // - In all other cases, will return an 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)) r := a.c.newRequest("GET", path) + r.setQueryOptions(q) r.params.Add("format", "json") r.header.Set("Accept", "application/json") _, 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) // - In all other cases, will return an 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)) r := a.c.newRequest("GET", path) + r.setQueryOptions(q) r.params.Add("format", "json") r.header.Set("Accept", "application/json") _, 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 // for the given service ID. 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.setQueryOptions(q) r.params.Set("enable", "true") r.params.Set("reason", reason) _, 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 // for the given service ID. 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.setQueryOptions(q) r.params.Set("enable", "false") _, resp, err := requireOK(a.c.doRequest(r)) if err != nil { diff --git a/api/agent_test.go b/api/agent_test.go index ef7cac43e..9f717732f 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -1322,7 +1322,7 @@ func TestAPI_AgentMonitorJSON(t *testing.T) { }) } -func TestAPI_ServiceMaintenance(t *testing.T) { +func TestAPI_ServiceMaintenanceOpts(t *testing.T) { t.Parallel() c, s := makeClient(t) defer s.Stop() @@ -1337,8 +1337,11 @@ func TestAPI_ServiceMaintenance(t *testing.T) { t.Fatalf("err: %v", err) } + // Specify namespace in query option + opts := &QueryOptions{Namespace: defaultNamespace} + // 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) } @@ -1361,7 +1364,7 @@ func TestAPI_ServiceMaintenance(t *testing.T) { } // Disable maintenance mode - if err := agent.DisableServiceMaintenance("redis"); err != nil { + if err := agent.DisableServiceMaintenanceOpts("redis", opts); err != nil { 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") } -func TestAPI_AgentHealthService(t *testing.T) { +func TestAPI_AgentHealthServiceOpts(t *testing.T) { t.Parallel() c, s := makeClient(t) defer s.Stop() @@ -1651,7 +1654,8 @@ func TestAPI_AgentHealthService(t *testing.T) { 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) - state, out, err := agent.AgentHealthServiceByID(serviceID) + opts := &QueryOptions{Namespace: defaultNamespace} + state, out, err := agent.AgentHealthServiceByIDOpts(serviceID, opts) require.Nil(t, err, msg, "err") require.Equal(t, expected, state, msg, "state") if !shouldExist { @@ -1664,7 +1668,8 @@ func TestAPI_AgentHealthService(t *testing.T) { 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) - state, outs, err := agent.AgentHealthServiceByName(serviceName) + opts := &QueryOptions{Namespace: defaultNamespace} + state, outs, err := agent.AgentHealthServiceByNameOpts(serviceName, opts) require.Nil(t, err, msg, "err") require.Equal(t, expected, state, msg, "state") if !shouldExist {