Merge pull request #8609 from hashicorp/dnephin/add-query-options-to-ServiceRegister

api: Add a context to ServiceRegisterOpts
This commit is contained in:
Daniel Nephin 2021-01-06 18:52:49 -05:00 committed by GitHub
commit 5a7f4c0dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package api
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -272,6 +273,17 @@ type ServiceRegisterOpts struct {
//Using this parameter allows to idempotently register a service and its checks without //Using this parameter allows to idempotently register a service and its checks without
//having to manually deregister checks. //having to manually deregister checks.
ReplaceExistingChecks bool ReplaceExistingChecks bool
// ctx is an optional context pass through to the underlying HTTP
// request layer. Use WithContext() to set the context.
ctx context.Context
}
// WithContext sets the context to be used for the request on a new ServiceRegisterOpts,
// and returns the opts.
func (o ServiceRegisterOpts) WithContext(ctx context.Context) ServiceRegisterOpts {
o.ctx = ctx
return o
} }
// AgentCheckRegistration is used to register a new check // AgentCheckRegistration is used to register a new check
@ -688,6 +700,7 @@ func (a *Agent) ServiceRegisterOpts(service *AgentServiceRegistration, opts Serv
func (a *Agent) serviceRegister(service *AgentServiceRegistration, opts ServiceRegisterOpts) error { func (a *Agent) serviceRegister(service *AgentServiceRegistration, opts ServiceRegisterOpts) error {
r := a.c.newRequest("PUT", "/v1/agent/service/register") r := a.c.newRequest("PUT", "/v1/agent/service/register")
r.obj = service r.obj = service
r.ctx = opts.ctx
if opts.ReplaceExistingChecks { if opts.ReplaceExistingChecks {
r.params.Set("replace-existing-checks", "true") r.params.Set("replace-existing-checks", "true")
} }

View File

@ -1,7 +1,9 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -208,7 +210,9 @@ func TestAPI_AgentServiceAndReplaceChecks(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if err := agent.ServiceRegisterOpts(regupdate, ServiceRegisterOpts{ReplaceExistingChecks: true}); err != nil { ctx := context.Background()
opts := ServiceRegisterOpts{ReplaceExistingChecks: true}.WithContext(ctx)
if err := agent.ServiceRegisterOpts(regupdate, opts); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -247,6 +251,18 @@ func TestAPI_AgentServiceAndReplaceChecks(t *testing.T) {
} }
} }
func TestAgent_ServiceRegisterOpts_WithContextTimeout(t *testing.T) {
c, err := NewClient(DefaultConfig())
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
t.Cleanup(cancel)
opts := ServiceRegisterOpts{}.WithContext(ctx)
err = c.Agent().ServiceRegisterOpts(&AgentServiceRegistration{}, opts)
require.True(t, errors.Is(err, context.DeadlineExceeded), "expected timeout")
}
func TestAPI_AgentServices(t *testing.T) { func TestAPI_AgentServices(t *testing.T) {
t.Parallel() t.Parallel()
c, s := makeClient(t) c, s := makeClient(t)