github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/command/agent/alloc_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) AllocsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    11  	if req.Method != "GET" {
    12  		return nil, CodedError(405, ErrInvalidMethod)
    13  	}
    14  
    15  	args := structs.AllocListRequest{}
    16  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    17  		return nil, nil
    18  	}
    19  
    20  	var out structs.AllocListResponse
    21  	if err := s.agent.RPC("Alloc.List", &args, &out); err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	setMeta(resp, &out.QueryMeta)
    26  	if out.Allocations == nil {
    27  		out.Allocations = make([]*structs.AllocListStub, 0)
    28  	}
    29  	return out.Allocations, nil
    30  }
    31  
    32  func (s *HTTPServer) AllocSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    33  	allocID := strings.TrimPrefix(req.URL.Path, "/v1/allocation/")
    34  	if req.Method != "GET" {
    35  		return nil, CodedError(405, ErrInvalidMethod)
    36  	}
    37  
    38  	args := structs.AllocSpecificRequest{
    39  		AllocID: allocID,
    40  	}
    41  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    42  		return nil, nil
    43  	}
    44  
    45  	var out structs.SingleAllocResponse
    46  	if err := s.agent.RPC("Alloc.GetAlloc", &args, &out); err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	setMeta(resp, &out.QueryMeta)
    51  	if out.Alloc == nil {
    52  		return nil, CodedError(404, "alloc not found")
    53  	}
    54  	return out.Alloc, nil
    55  }