mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 17:35:43 +03:00
Merge pull request #4138 from hashicorp/i-hcl-json-endpoint
HCL to JSON api endpoint
This commit is contained in:
@@ -143,6 +143,7 @@ func (s *HTTPServer) Shutdown() {
|
||||
// registerHandlers is used to attach our handlers to the mux
|
||||
func (s *HTTPServer) registerHandlers(enableDebug bool) {
|
||||
s.mux.HandleFunc("/v1/jobs", s.wrap(s.JobsRequest))
|
||||
s.mux.HandleFunc("/v1/jobs/parse", s.wrap(s.JobsParseRequest))
|
||||
s.mux.HandleFunc("/v1/job/", s.wrap(s.JobSpecificRequest))
|
||||
|
||||
s.mux.HandleFunc("/v1/nodes", s.wrap(s.NodesRequest))
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/golang/snappy"
|
||||
"github.com/hashicorp/nomad/api"
|
||||
"github.com/hashicorp/nomad/jobspec"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
@@ -544,6 +545,32 @@ func (s *HTTPServer) jobDispatchRequest(resp http.ResponseWriter, req *http.Requ
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// JobsParseRequest parses a hcl jobspec and returns a api.Job
|
||||
func (s *HTTPServer) JobsParseRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
if req.Method != http.MethodPut && req.Method != http.MethodPost {
|
||||
return nil, CodedError(405, ErrInvalidMethod)
|
||||
}
|
||||
|
||||
args := &api.JobsParseRequest{}
|
||||
if err := decodeBody(req, &args); err != nil {
|
||||
return nil, CodedError(400, err.Error())
|
||||
}
|
||||
if args.JobHCL == "" {
|
||||
return nil, CodedError(400, "Job spec is empty")
|
||||
}
|
||||
|
||||
jobfile := strings.NewReader(args.JobHCL)
|
||||
jobStruct, err := jobspec.Parse(jobfile)
|
||||
if err != nil {
|
||||
return nil, CodedError(400, err.Error())
|
||||
}
|
||||
|
||||
if args.Canonicalize {
|
||||
jobStruct.Canonicalize()
|
||||
}
|
||||
return jobStruct, nil
|
||||
}
|
||||
|
||||
func ApiJobToStructJob(job *api.Job) *structs.Job {
|
||||
job.Canonicalize()
|
||||
|
||||
|
||||
@@ -272,6 +272,38 @@ func TestHTTP_JobsRegister_Defaulting(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTP_JobsParse(t *testing.T) {
|
||||
t.Parallel()
|
||||
httpTest(t, nil, func(s *TestAgent) {
|
||||
buf := encodeReq(api.JobsParseRequest{JobHCL: mock.HCL()})
|
||||
req, err := http.NewRequest("POST", "/v1/jobs/parse", buf)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
respW := httptest.NewRecorder()
|
||||
|
||||
obj, err := s.Server.JobsParseRequest(respW, req)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if obj == nil {
|
||||
t.Fatal("response should not be nil")
|
||||
}
|
||||
|
||||
job := obj.(*api.Job)
|
||||
expected := mock.Job()
|
||||
if job.Name == nil || *job.Name != expected.Name {
|
||||
t.Fatalf("job name is '%s', expected '%s'", *job.Name, expected.Name)
|
||||
}
|
||||
|
||||
if job.Datacenters == nil ||
|
||||
job.Datacenters[0] != expected.Datacenters[0] {
|
||||
t.Fatalf("job datacenters is '%s', expected '%s'",
|
||||
job.Datacenters[0], expected.Datacenters[0])
|
||||
}
|
||||
})
|
||||
}
|
||||
func TestHTTP_JobQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
httpTest(t, nil, func(s *TestAgent) {
|
||||
|
||||
Reference in New Issue
Block a user