open-nomad/command/agent/job_endpoint.go

190 lines
4.9 KiB
Go
Raw Normal View History

2015-09-06 00:06:05 +00:00
package agent
import (
"net/http"
2015-09-06 01:00:30 +00:00
"strings"
2015-09-06 01:20:47 +00:00
"github.com/hashicorp/nomad/nomad/structs"
2015-09-06 00:06:05 +00:00
)
2015-09-06 01:00:30 +00:00
func (s *HTTPServer) JobsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
switch req.Method {
case "GET":
return s.jobListRequest(resp, req)
case "PUT", "POST":
2015-09-06 18:47:52 +00:00
return s.jobUpdate(resp, req, "")
2015-09-06 01:00:30 +00:00
default:
return nil, CodedError(405, ErrInvalidMethod)
2015-09-06 00:06:05 +00:00
}
2015-09-06 01:00:30 +00:00
}
2015-09-06 00:06:05 +00:00
2015-09-06 01:00:30 +00:00
func (s *HTTPServer) jobListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2015-09-06 19:32:22 +00:00
args := structs.JobListRequest{}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.JobListResponse
if err := s.agent.RPC("Job.List", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
2015-09-07 17:03:10 +00:00
if out.Jobs == nil {
out.Jobs = make([]*structs.JobListStub, 0)
}
2015-09-06 19:32:22 +00:00
return out.Jobs, nil
2015-09-06 01:00:30 +00:00
}
func (s *HTTPServer) JobSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
path := strings.TrimPrefix(req.URL.Path, "/v1/job/")
switch {
case strings.HasSuffix(path, "/evaluate"):
jobName := strings.TrimSuffix(path, "/evaluate")
return s.jobForceEvaluate(resp, req, jobName)
case strings.HasSuffix(path, "/allocations"):
jobName := strings.TrimSuffix(path, "/allocations")
return s.jobAllocations(resp, req, jobName)
case strings.HasSuffix(path, "/evaluations"):
jobName := strings.TrimSuffix(path, "/evaluations")
return s.jobEvaluations(resp, req, jobName)
default:
return s.jobCRUD(resp, req, path)
2015-09-06 00:06:05 +00:00
}
2015-09-06 01:00:30 +00:00
}
2015-09-06 00:06:05 +00:00
2015-09-06 01:00:30 +00:00
func (s *HTTPServer) jobForceEvaluate(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
if req.Method != "PUT" && req.Method != "POST" {
return nil, CodedError(405, ErrInvalidMethod)
2015-09-06 00:06:05 +00:00
}
args := structs.JobEvaluateRequest{
JobID: jobName,
}
s.parseRegion(req, &args.Region)
var out structs.JobRegisterResponse
if err := s.agent.RPC("Job.Evaluate", &args, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
2015-09-06 00:06:05 +00:00
}
2015-09-06 01:00:30 +00:00
func (s *HTTPServer) jobAllocations(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
2015-09-06 00:06:05 +00:00
}
2015-09-06 19:32:22 +00:00
args := structs.JobSpecificRequest{
JobID: jobName,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.JobAllocationsResponse
if err := s.agent.RPC("Job.Allocations", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
2015-09-07 17:03:10 +00:00
if out.Allocations == nil {
out.Allocations = make([]*structs.AllocListStub, 0)
}
2015-09-06 19:32:22 +00:00
return out.Allocations, nil
2015-09-06 01:00:30 +00:00
}
2015-09-06 00:06:05 +00:00
2015-09-06 01:00:30 +00:00
func (s *HTTPServer) jobEvaluations(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
2015-09-06 00:06:05 +00:00
}
2015-09-06 19:32:22 +00:00
args := structs.JobSpecificRequest{
JobID: jobName,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.JobEvaluationsResponse
if err := s.agent.RPC("Job.Evaluations", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
2015-09-07 17:03:10 +00:00
if out.Evaluations == nil {
out.Evaluations = make([]*structs.Evaluation, 0)
}
2015-09-06 19:32:22 +00:00
return out.Evaluations, nil
2015-09-06 01:00:30 +00:00
}
2015-09-06 00:06:05 +00:00
2015-09-06 01:00:30 +00:00
func (s *HTTPServer) jobCRUD(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
switch req.Method {
case "GET":
return s.jobQuery(resp, req, jobName)
case "PUT", "POST":
return s.jobUpdate(resp, req, jobName)
case "DELETE":
return s.jobDelete(resp, req, jobName)
default:
return nil, CodedError(405, ErrInvalidMethod)
2015-09-06 00:06:05 +00:00
}
2015-09-06 01:00:30 +00:00
}
func (s *HTTPServer) jobQuery(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
2015-09-06 01:43:40 +00:00
args := structs.JobSpecificRequest{
JobID: jobName,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.SingleJobResponse
if err := s.agent.RPC("Job.GetJob", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Job == nil {
return nil, CodedError(404, "job not found")
}
return out.Job, nil
2015-09-06 01:00:30 +00:00
}
func (s *HTTPServer) jobUpdate(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
2015-09-06 02:08:47 +00:00
var args structs.JobRegisterRequest
if err := decodeBody(req, &args, nil); err != nil {
return nil, CodedError(400, err.Error())
}
2015-09-06 18:47:52 +00:00
if jobName != "" && args.Job.ID != jobName {
2015-09-06 02:08:47 +00:00
return nil, CodedError(400, "Job ID does not match")
}
s.parseRegion(req, &args.Region)
var out structs.JobRegisterResponse
if err := s.agent.RPC("Job.Register", &args, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
2015-09-06 01:00:30 +00:00
}
func (s *HTTPServer) jobDelete(resp http.ResponseWriter, req *http.Request,
jobName string) (interface{}, error) {
2015-09-06 01:20:47 +00:00
args := structs.JobDeregisterRequest{
JobID: jobName,
}
s.parseRegion(req, &args.Region)
var out structs.JobDeregisterResponse
if err := s.agent.RPC("Job.Deregister", &args, &out); err != nil {
return nil, err
}
2015-09-06 01:43:40 +00:00
setIndex(resp, out.Index)
2015-09-06 01:20:47 +00:00
return out, nil
2015-09-06 00:06:05 +00:00
}