github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/command/agent/job_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  )
     9  
    10  func (s *HTTPServer) JobsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    11  	switch req.Method {
    12  	case "GET":
    13  		return s.jobListRequest(resp, req)
    14  	case "PUT", "POST":
    15  		return s.jobUpdate(resp, req, "")
    16  	default:
    17  		return nil, CodedError(405, ErrInvalidMethod)
    18  	}
    19  }
    20  
    21  func (s *HTTPServer) jobListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    22  	args := structs.JobListRequest{}
    23  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    24  		return nil, nil
    25  	}
    26  
    27  	var out structs.JobListResponse
    28  	if err := s.agent.RPC("Job.List", &args, &out); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	setMeta(resp, &out.QueryMeta)
    33  	if out.Jobs == nil {
    34  		out.Jobs = make([]*structs.JobListStub, 0)
    35  	}
    36  	return out.Jobs, nil
    37  }
    38  
    39  func (s *HTTPServer) JobSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    40  	path := strings.TrimPrefix(req.URL.Path, "/v1/job/")
    41  	switch {
    42  	case strings.HasSuffix(path, "/evaluate"):
    43  		jobName := strings.TrimSuffix(path, "/evaluate")
    44  		return s.jobForceEvaluate(resp, req, jobName)
    45  	case strings.HasSuffix(path, "/allocations"):
    46  		jobName := strings.TrimSuffix(path, "/allocations")
    47  		return s.jobAllocations(resp, req, jobName)
    48  	case strings.HasSuffix(path, "/evaluations"):
    49  		jobName := strings.TrimSuffix(path, "/evaluations")
    50  		return s.jobEvaluations(resp, req, jobName)
    51  	default:
    52  		return s.jobCRUD(resp, req, path)
    53  	}
    54  }
    55  
    56  func (s *HTTPServer) jobForceEvaluate(resp http.ResponseWriter, req *http.Request,
    57  	jobName string) (interface{}, error) {
    58  	if req.Method != "PUT" && req.Method != "POST" {
    59  		return nil, CodedError(405, ErrInvalidMethod)
    60  	}
    61  	args := structs.JobEvaluateRequest{
    62  		JobID: jobName,
    63  	}
    64  	s.parseRegion(req, &args.Region)
    65  
    66  	var out structs.JobRegisterResponse
    67  	if err := s.agent.RPC("Job.Evaluate", &args, &out); err != nil {
    68  		return nil, err
    69  	}
    70  	setIndex(resp, out.Index)
    71  	return out, nil
    72  }
    73  
    74  func (s *HTTPServer) jobAllocations(resp http.ResponseWriter, req *http.Request,
    75  	jobName string) (interface{}, error) {
    76  	if req.Method != "GET" {
    77  		return nil, CodedError(405, ErrInvalidMethod)
    78  	}
    79  	args := structs.JobSpecificRequest{
    80  		JobID: jobName,
    81  	}
    82  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    83  		return nil, nil
    84  	}
    85  
    86  	var out structs.JobAllocationsResponse
    87  	if err := s.agent.RPC("Job.Allocations", &args, &out); err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	setMeta(resp, &out.QueryMeta)
    92  	if out.Allocations == nil {
    93  		out.Allocations = make([]*structs.AllocListStub, 0)
    94  	}
    95  	return out.Allocations, nil
    96  }
    97  
    98  func (s *HTTPServer) jobEvaluations(resp http.ResponseWriter, req *http.Request,
    99  	jobName string) (interface{}, error) {
   100  	if req.Method != "GET" {
   101  		return nil, CodedError(405, ErrInvalidMethod)
   102  	}
   103  	args := structs.JobSpecificRequest{
   104  		JobID: jobName,
   105  	}
   106  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
   107  		return nil, nil
   108  	}
   109  
   110  	var out structs.JobEvaluationsResponse
   111  	if err := s.agent.RPC("Job.Evaluations", &args, &out); err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	setMeta(resp, &out.QueryMeta)
   116  	if out.Evaluations == nil {
   117  		out.Evaluations = make([]*structs.Evaluation, 0)
   118  	}
   119  	return out.Evaluations, nil
   120  }
   121  
   122  func (s *HTTPServer) jobCRUD(resp http.ResponseWriter, req *http.Request,
   123  	jobName string) (interface{}, error) {
   124  	switch req.Method {
   125  	case "GET":
   126  		return s.jobQuery(resp, req, jobName)
   127  	case "PUT", "POST":
   128  		return s.jobUpdate(resp, req, jobName)
   129  	case "DELETE":
   130  		return s.jobDelete(resp, req, jobName)
   131  	default:
   132  		return nil, CodedError(405, ErrInvalidMethod)
   133  	}
   134  }
   135  
   136  func (s *HTTPServer) jobQuery(resp http.ResponseWriter, req *http.Request,
   137  	jobName string) (interface{}, error) {
   138  	args := structs.JobSpecificRequest{
   139  		JobID: jobName,
   140  	}
   141  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
   142  		return nil, nil
   143  	}
   144  
   145  	var out structs.SingleJobResponse
   146  	if err := s.agent.RPC("Job.GetJob", &args, &out); err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	setMeta(resp, &out.QueryMeta)
   151  	if out.Job == nil {
   152  		return nil, CodedError(404, "job not found")
   153  	}
   154  	return out.Job, nil
   155  }
   156  
   157  func (s *HTTPServer) jobUpdate(resp http.ResponseWriter, req *http.Request,
   158  	jobName string) (interface{}, error) {
   159  	var args structs.JobRegisterRequest
   160  	if err := decodeBody(req, &args); err != nil {
   161  		return nil, CodedError(400, err.Error())
   162  	}
   163  	if args.Job == nil {
   164  		return nil, CodedError(400, "Job must be specified")
   165  	}
   166  	if jobName != "" && args.Job.ID != jobName {
   167  		return nil, CodedError(400, "Job ID does not match")
   168  	}
   169  	s.parseRegion(req, &args.Region)
   170  
   171  	var out structs.JobRegisterResponse
   172  	if err := s.agent.RPC("Job.Register", &args, &out); err != nil {
   173  		return nil, err
   174  	}
   175  	setIndex(resp, out.Index)
   176  	return out, nil
   177  }
   178  
   179  func (s *HTTPServer) jobDelete(resp http.ResponseWriter, req *http.Request,
   180  	jobName string) (interface{}, error) {
   181  	args := structs.JobDeregisterRequest{
   182  		JobID: jobName,
   183  	}
   184  	s.parseRegion(req, &args.Region)
   185  
   186  	var out structs.JobDeregisterResponse
   187  	if err := s.agent.RPC("Job.Deregister", &args, &out); err != nil {
   188  		return nil, err
   189  	}
   190  	setIndex(resp, out.Index)
   191  	return out, nil
   192  }