http: adding alloc lookup endpoint

This commit is contained in:
Armon Dadgar 2015-09-06 15:49:44 -07:00
parent e54d5402e2
commit 009fc62cc8
2 changed files with 60 additions and 16 deletions

View File

@ -28,24 +28,25 @@ func (s *HTTPServer) AllocsRequest(resp http.ResponseWriter, req *http.Request)
func (s *HTTPServer) AllocSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
allocID := strings.TrimPrefix(req.URL.Path, "/v1/allocation/")
switch req.Method {
case "GET":
return s.allocQuery(resp, req, allocID)
case "DELETE":
return s.allocDelete(resp, req, allocID)
default:
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
}
func (s *HTTPServer) allocQuery(resp http.ResponseWriter, req *http.Request,
allocID string) (interface{}, error) {
// TODO
return nil, nil
}
args := structs.AllocSpecificRequest{
AllocID: allocID,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
func (s *HTTPServer) allocDelete(resp http.ResponseWriter, req *http.Request,
allocID string) (interface{}, error) {
// TODO
return nil, nil
var out structs.SingleAllocResponse
if err := s.agent.RPC("Alloc.GetAlloc", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Alloc == nil {
return nil, CodedError(404, "alloc not found")
}
return out.Alloc, nil
}

View File

@ -52,3 +52,46 @@ func TestHTTP_AllocsList(t *testing.T) {
}
})
}
func TestHTTP_AllocQuery(t *testing.T) {
httpTest(t, nil, func(s *TestServer) {
// Directly manipulate the state
state := s.Agent.server.State()
alloc := mock.Alloc()
err := state.UpdateAllocations(1000,
[]*structs.Allocation{alloc})
if err != nil {
t.Fatalf("err: %v", err)
}
// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
respW := httptest.NewRecorder()
// Make the request
obj, err := s.Server.AllocSpecificRequest(respW, req)
if err != nil {
t.Fatalf("err: %v", err)
}
// Check for the index
if respW.HeaderMap.Get("X-Nomad-Index") == "" {
t.Fatalf("missing index")
}
if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" {
t.Fatalf("missing known leader")
}
if respW.HeaderMap.Get("X-Nomad-LastContact") == "" {
t.Fatalf("missing last contact")
}
// Check the job
a := obj.(*structs.Allocation)
if a.ID != alloc.ID {
t.Fatalf("bad: %#v", a)
}
})
}