Skip to content

Commit

Permalink
Set/parse idempotency_token query param
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmunda committed Jul 7, 2021
1 parent 3a8febe commit c9de4d5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ func (r *request) setWriteOptions(q *WriteOptions) {
if q.AuthToken != "" {
r.token = q.AuthToken
}
if q.IdempotencyToken != "" {
r.params.Set("idempotency_token", q.IdempotencyToken)
}
r.ctx = q.Context()
}

Expand Down
10 changes: 7 additions & 3 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ func TestSetWriteOptions(t *testing.T) {

r, _ := c.newRequest("GET", "/v1/jobs")
q := &WriteOptions{
Region: "foo",
Namespace: "bar",
AuthToken: "foobar",
Region: "foo",
Namespace: "bar",
AuthToken: "foobar",
IdempotencyToken: "idempotent",
}
r.setWriteOptions(q)

Expand All @@ -267,6 +268,9 @@ func TestSetWriteOptions(t *testing.T) {
if r.params.Get("namespace") != "bar" {
t.Fatalf("bad: %v", r.params)
}
if r.params.Get("idempotency_token") != "idempotent" {
t.Fatalf("bad: %v", r.params)
}
if r.token != "foobar" {
t.Fatalf("bad: %v", r.token)
}
Expand Down
8 changes: 8 additions & 0 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,13 @@ func parseNamespace(req *http.Request, n *string) {
}
}

// parseIdempotencyToken is used to parse the ?idempotency_token parameter
func parseIdempotencyToken(req *http.Request, n *string) {
if other := req.URL.Query().Get("idempotency_token"); other != "" {
*n = other
}
}

// parseBool parses a query parameter to a boolean or returns (nil, nil) if the
// parameter is not present.
func parseBool(req *http.Request, field string) (*bool, error) {
Expand Down Expand Up @@ -721,6 +728,7 @@ func (s *HTTPServer) parseWriteRequest(req *http.Request, w *structs.WriteReques
parseNamespace(req, &w.Namespace)
s.parseToken(req, &w.AuthToken)
s.parseRegion(req, &w.Region)
parseIdempotencyToken(req, &w.IdempotencyToken)
}

// wrapUntrustedContent wraps handlers in a http.ResponseWriter that prevents
Expand Down
12 changes: 7 additions & 5 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"time"

"github.com/golang/snappy"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

api "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/kr/pretty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHTTP_JobsList(t *testing.T) {
Expand Down Expand Up @@ -1454,8 +1455,9 @@ func TestHTTP_JobDispatch(t *testing.T) {
respW := httptest.NewRecorder()
args2 := structs.JobDispatchRequest{
WriteRequest: structs.WriteRequest{
Region: "global",
Namespace: structs.DefaultNamespace,
Region: "global",
Namespace: structs.DefaultNamespace,
IdempotencyToken: "foo",
},
}
buf := encodeReq(args2)
Expand Down

0 comments on commit c9de4d5

Please sign in to comment.