mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
Merge branch 'master' of https://github.com/hashicorp/nomad
This commit is contained in:
77
command/agent/fs_endpoint.go
Normal file
77
command/agent/fs_endpoint.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
allocIDNotPresentErr = fmt.Errorf("must provide a valid alloc id")
|
||||
fileNameNotPresentErr = fmt.Errorf("must provide a file name")
|
||||
)
|
||||
|
||||
func (s *HTTPServer) DirectoryListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
var allocID, path string
|
||||
|
||||
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/ls/"); allocID == "" {
|
||||
return nil, allocIDNotPresentErr
|
||||
}
|
||||
if path = req.URL.Query().Get("path"); path == "" {
|
||||
path = "/"
|
||||
}
|
||||
fs, err := s.agent.client.GetAllocFS(allocID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fs.List(path)
|
||||
}
|
||||
|
||||
func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
var allocID, path string
|
||||
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/stat/"); allocID == "" {
|
||||
return nil, allocIDNotPresentErr
|
||||
}
|
||||
if path = req.URL.Query().Get("path"); path == "" {
|
||||
return nil, fileNameNotPresentErr
|
||||
}
|
||||
fs, err := s.agent.client.GetAllocFS(allocID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fs.Stat(path)
|
||||
}
|
||||
|
||||
func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
var allocID, path string
|
||||
var offset, limit int64
|
||||
var err error
|
||||
|
||||
q := req.URL.Query()
|
||||
|
||||
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/readat/"); allocID == "" {
|
||||
return nil, allocIDNotPresentErr
|
||||
}
|
||||
if path = q.Get("path"); path == "" {
|
||||
return nil, fileNameNotPresentErr
|
||||
}
|
||||
|
||||
if offset, err = strconv.ParseInt(q.Get("offset"), 10, 64); err != nil {
|
||||
return nil, fmt.Errorf("error parsing offset: %v", err)
|
||||
}
|
||||
if limit, err = strconv.ParseInt(q.Get("limit"), 10, 64); err != nil {
|
||||
return nil, fmt.Errorf("error parsing limit: %v", err)
|
||||
}
|
||||
fs, err := s.agent.client.GetAllocFS(allocID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := fs.ReadAt(path, offset, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
io.Copy(resp, r)
|
||||
return nil, nil
|
||||
}
|
||||
86
command/agent/fs_endpoint_test.go
Normal file
86
command/agent/fs_endpoint_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAllocDirFS_List(t *testing.T) {
|
||||
httpTest(t, nil, func(s *TestServer) {
|
||||
req, err := http.NewRequest("GET", "/v1/client/fs/ls/", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
respW := httptest.NewRecorder()
|
||||
|
||||
_, err = s.Server.DirectoryListRequest(respW, req)
|
||||
if err != allocIDNotPresentErr {
|
||||
t.Fatalf("expected err: %v, actual: %v", allocIDNotPresentErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAllocDirFS_Stat(t *testing.T) {
|
||||
httpTest(t, nil, func(s *TestServer) {
|
||||
req, err := http.NewRequest("GET", "/v1/client/fs/stat/", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
respW := httptest.NewRecorder()
|
||||
|
||||
_, err = s.Server.FileStatRequest(respW, req)
|
||||
if err != allocIDNotPresentErr {
|
||||
t.Fatalf("expected err: %v, actual: %v", allocIDNotPresentErr, err)
|
||||
}
|
||||
|
||||
req, err = http.NewRequest("GET", "/v1/client/fs/stat/foo", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
respW = httptest.NewRecorder()
|
||||
|
||||
_, err = s.Server.FileStatRequest(respW, req)
|
||||
if err != fileNameNotPresentErr {
|
||||
t.Fatalf("expected err: %v, actual: %v", allocIDNotPresentErr, err)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestAllocDirFS_ReadAt(t *testing.T) {
|
||||
httpTest(t, nil, func(s *TestServer) {
|
||||
req, err := http.NewRequest("GET", "/v1/client/fs/readat/", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
respW := httptest.NewRecorder()
|
||||
|
||||
_, err = s.Server.FileReadAtRequest(respW, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
|
||||
req, err = http.NewRequest("GET", "/v1/client/fs/readat/foo", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
respW = httptest.NewRecorder()
|
||||
|
||||
_, err = s.Server.FileReadAtRequest(respW, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
|
||||
req, err = http.NewRequest("GET", "/v1/client/fs/readat/foo?path=/path/to/file", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
respW = httptest.NewRecorder()
|
||||
|
||||
_, err = s.Server.FileReadAtRequest(respW, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -103,6 +103,10 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
|
||||
s.mux.HandleFunc("/v1/evaluations", s.wrap(s.EvalsRequest))
|
||||
s.mux.HandleFunc("/v1/evaluation/", s.wrap(s.EvalSpecificRequest))
|
||||
|
||||
s.mux.HandleFunc("/v1/client/fs/ls/", s.wrap(s.DirectoryListRequest))
|
||||
s.mux.HandleFunc("/v1/client/fs/stat/", s.wrap(s.FileStatRequest))
|
||||
s.mux.HandleFunc("/v1/client/fs/readat/", s.wrap(s.FileReadAtRequest))
|
||||
|
||||
s.mux.HandleFunc("/v1/agent/self", s.wrap(s.AgentSelfRequest))
|
||||
s.mux.HandleFunc("/v1/agent/join", s.wrap(s.AgentJoinRequest))
|
||||
s.mux.HandleFunc("/v1/agent/members", s.wrap(s.AgentMembersRequest))
|
||||
|
||||
Reference in New Issue
Block a user