mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
Set/parse idempotency_token query param
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 idempotencyToken := req.URL.Query().Get("idempotency_token"); idempotencyToken != "" {
|
||||
*n = idempotencyToken
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user