-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
autocomplete api #2964
Merged
+827
−0
Merged
autocomplete api #2964
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1b24ae5
Retrieve job information for resources endpoint
chelseakomlo cb1e898
adding test validation that received resources matches requested
chelseakomlo c3e0bd8
test resources endpoint will return matching prefixes
chelseakomlo 0bd9742
remove unnecessary validations; these are tested elsewhere
chelseakomlo 5435773
refactor test helper
chelseakomlo 4945b98
limit resources results to 20
chelseakomlo 1ec5010
remove resourceliststub, no need for another layer of abstraction
chelseakomlo 9e06777
change resources endpoint from http get to post
chelseakomlo dfd8c1d
adds evaluations
chelseakomlo e14b279
add truncation boolean to response
chelseakomlo a9ed750
use upsert as a test helper
chelseakomlo ed8e707
refactor to remove duplication for types of resources
chelseakomlo a9728c3
adding allocations to resouces list endpoint
chelseakomlo a3234b2
refactor and add error handling for invalid context type
chelseakomlo 760f429
resources list endpoint accepts http POST and PUT
chelseakomlo c9c7b19
refactor rpc endpoint and tests
chelseakomlo 9947939
add documentation
chelseakomlo 01640d8
set response index and meta information
chelseakomlo ca4ed64
resources are expected by state store to be plural
chelseakomlo 96a1723
further refactoring
chelseakomlo 971183d
fix up tests to intantiate assertion test helper
chelseakomlo 74f35dd
if no context is specified, set maximum index for available contexts
chelseakomlo b2df34c
further refactoring
chelseakomlo 753b157
syntax fixups and logging
chelseakomlo e5e4fc4
max index for any resource, if context is unspecified
chelseakomlo a1b5925
code simplifications and logging
chelseakomlo 440fea7
update changelog
chelseakomlo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package agent | ||
|
||
import ( | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"net/http" | ||
) | ||
|
||
// ResourceListRequest accepts a prefix and context and returns a list of matching | ||
// IDs for that context. | ||
func (s *HTTPServer) ResourceListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
if req.Method == "POST" || req.Method == "PUT" { | ||
return s.resourcesRequest(resp, req) | ||
} | ||
return nil, CodedError(405, ErrInvalidMethod) | ||
} | ||
|
||
func (s *HTTPServer) resourcesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
args := structs.ResourceListRequest{} | ||
|
||
if err := decodeBody(req, &args); err != nil { | ||
return nil, CodedError(400, err.Error()) | ||
} | ||
|
||
var out structs.ResourceListResponse | ||
if err := s.agent.RPC("Resources.List", &args, &out); err != nil { | ||
return nil, err | ||
} | ||
|
||
setMeta(resp, &out.QueryMeta) | ||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,304 @@ | ||
package agent | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
a "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHTTP_ResourcesWithIllegalMethod(t *testing.T) { | ||
assert := a.New(t) | ||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
req, err := http.NewRequest("DELETE", "/v1/resources", nil) | ||
assert.Nil(err) | ||
respW := httptest.NewRecorder() | ||
|
||
_, err = s.Server.ResourceListRequest(respW, req) | ||
assert.NotNil(err, "HTTP DELETE should not be accepted for this endpoint") | ||
}) | ||
} | ||
|
||
func createJobForTest(jobID string, s *TestAgent, t *testing.T) { | ||
assert := a.New(t) | ||
|
||
job := mock.Job() | ||
job.ID = jobID | ||
job.TaskGroups[0].Count = 1 | ||
|
||
state := s.Agent.server.State() | ||
err := state.UpsertJob(1000, job) | ||
assert.Nil(err) | ||
} | ||
|
||
func TestHTTP_Resources_POST(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
testJob := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706" | ||
testJobPrefix := "aaaaaaaa-e8f7-fd38" | ||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
createJobForTest(testJob, s, t) | ||
|
||
data := structs.ResourceListRequest{Prefix: testJobPrefix, Context: "jobs"} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
|
||
j := res.Matches["jobs"] | ||
|
||
assert.Equal(1, len(j)) | ||
assert.Equal(j[0], testJob) | ||
|
||
assert.Equal(res.Truncations["job"], false) | ||
assert.NotEqual("0", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_Resources_PUT(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
testJob := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706" | ||
testJobPrefix := "aaaaaaaa-e8f7-fd38" | ||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
createJobForTest(testJob, s, t) | ||
|
||
data := structs.ResourceListRequest{Prefix: testJobPrefix, Context: "jobs"} | ||
req, err := http.NewRequest("PUT", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
|
||
j := res.Matches["jobs"] | ||
|
||
assert.Equal(1, len(j)) | ||
assert.Equal(j[0], testJob) | ||
|
||
assert.Equal(res.Truncations["job"], false) | ||
assert.NotEqual("0", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_Resources_MultipleJobs(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
testJobA := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706" | ||
testJobB := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89707" | ||
testJobC := "bbbbbbbb-e8f7-fd38-c855-ab94ceb89707" | ||
|
||
testJobPrefix := "aaaaaaaa-e8f7-fd38" | ||
|
||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
createJobForTest(testJobA, s, t) | ||
createJobForTest(testJobB, s, t) | ||
createJobForTest(testJobC, s, t) | ||
|
||
data := structs.ResourceListRequest{Prefix: testJobPrefix, Context: "jobs"} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
|
||
j := res.Matches["jobs"] | ||
|
||
assert.Equal(2, len(j)) | ||
assert.Contains(j, testJobA) | ||
assert.Contains(j, testJobB) | ||
assert.NotContains(j, testJobC) | ||
|
||
assert.Equal(res.Truncations["job"], false) | ||
assert.NotEqual("0", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_ResoucesList_Evaluation(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
state := s.Agent.server.State() | ||
eval1 := mock.Eval() | ||
eval2 := mock.Eval() | ||
err := state.UpsertEvals(9000, | ||
[]*structs.Evaluation{eval1, eval2}) | ||
assert.Nil(err) | ||
|
||
prefix := eval1.ID[:len(eval1.ID)-2] | ||
data := structs.ResourceListRequest{Prefix: prefix, Context: "evals"} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
|
||
j := res.Matches["evals"] | ||
assert.Equal(1, len(j)) | ||
assert.Contains(j, eval1.ID) | ||
assert.NotContains(j, eval2.ID) | ||
|
||
assert.Equal(res.Truncations["evals"], false) | ||
assert.Equal("9000", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_ResoucesList_Allocations(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
state := s.Agent.server.State() | ||
alloc := mock.Alloc() | ||
err := state.UpsertAllocs(7000, []*structs.Allocation{alloc}) | ||
assert.Nil(err) | ||
|
||
prefix := alloc.ID[:len(alloc.ID)-2] | ||
data := structs.ResourceListRequest{Prefix: prefix, Context: "allocs"} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
|
||
a := res.Matches["allocs"] | ||
assert.Equal(1, len(a)) | ||
assert.Contains(a, alloc.ID) | ||
|
||
assert.Equal(res.Truncations["allocs"], false) | ||
assert.Equal("7000", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_ResoucesList_Nodes(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
state := s.Agent.server.State() | ||
node := mock.Node() | ||
err := state.UpsertNode(6000, node) | ||
assert.Nil(err) | ||
|
||
prefix := node.ID[:len(node.ID)-2] | ||
data := structs.ResourceListRequest{Prefix: prefix, Context: "nodes"} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
|
||
n := res.Matches["nodes"] | ||
assert.Equal(1, len(n)) | ||
assert.Contains(n, node.ID) | ||
|
||
assert.Equal(res.Truncations["nodes"], false) | ||
assert.Equal("6000", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_Resources_NoJob(t *testing.T) { | ||
assert := a.New(t) | ||
|
||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
data := structs.ResourceListRequest{Prefix: "12345", Context: "jobs"} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
assert.Equal(1, len(res.Matches)) | ||
assert.Equal(0, len(res.Matches["jobs"])) | ||
|
||
assert.Equal("0", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} | ||
|
||
func TestHTTP_Resources_NoContext(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably test more than just the job is returned |
||
assert := a.New(t) | ||
|
||
testJobID := "aaaaaaaa-e8f7-fd38-c855-ab94ceb89706" | ||
testJobPrefix := "aaaaaaaa-e8f7-fd38" | ||
t.Parallel() | ||
httpTest(t, nil, func(s *TestAgent) { | ||
createJobForTest(testJobID, s, t) | ||
|
||
state := s.Agent.server.State() | ||
eval1 := mock.Eval() | ||
eval1.ID = testJobID | ||
err := state.UpsertEvals(8000, []*structs.Evaluation{eval1}) | ||
assert.Nil(err) | ||
|
||
data := structs.ResourceListRequest{Prefix: testJobPrefix} | ||
req, err := http.NewRequest("POST", "/v1/resources", encodeReq(data)) | ||
assert.Nil(err) | ||
|
||
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourceListRequest(respW, req) | ||
assert.Nil(err) | ||
|
||
res := resp.(structs.ResourceListResponse) | ||
|
||
matchedJobs := res.Matches["jobs"] | ||
matchedEvals := res.Matches["evals"] | ||
|
||
assert.Equal(1, len(matchedJobs)) | ||
assert.Equal(1, len(matchedEvals)) | ||
|
||
assert.Equal(matchedJobs[0], testJobID) | ||
assert.Equal(matchedEvals[0], eval1.ID) | ||
|
||
assert.Equal("8000", respW.HeaderMap.Get("X-Nomad-Index")) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allocs and nodes?