github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/command/agent/node_endpoint.go (about) 1 package agent 2 3 import ( 4 "net/http" 5 "strconv" 6 "strings" 7 8 "github.com/hashicorp/nomad/nomad/structs" 9 ) 10 11 func (s *HTTPServer) NodesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { 12 if req.Method != "GET" { 13 return nil, CodedError(405, ErrInvalidMethod) 14 } 15 16 args := structs.NodeListRequest{} 17 if s.parse(resp, req, &args.Region, &args.QueryOptions) { 18 return nil, nil 19 } 20 21 var out structs.NodeListResponse 22 if err := s.agent.RPC("Node.List", &args, &out); err != nil { 23 return nil, err 24 } 25 26 setMeta(resp, &out.QueryMeta) 27 if out.Nodes == nil { 28 out.Nodes = make([]*structs.NodeListStub, 0) 29 } 30 return out.Nodes, nil 31 } 32 33 func (s *HTTPServer) NodeSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { 34 path := strings.TrimPrefix(req.URL.Path, "/v1/node/") 35 switch { 36 case strings.HasSuffix(path, "/evaluate"): 37 nodeName := strings.TrimSuffix(path, "/evaluate") 38 return s.nodeForceEvaluate(resp, req, nodeName) 39 case strings.HasSuffix(path, "/allocations"): 40 nodeName := strings.TrimSuffix(path, "/allocations") 41 return s.nodeAllocations(resp, req, nodeName) 42 case strings.HasSuffix(path, "/drain"): 43 nodeName := strings.TrimSuffix(path, "/drain") 44 return s.nodeToggleDrain(resp, req, nodeName) 45 default: 46 return s.nodeQuery(resp, req, path) 47 } 48 } 49 50 func (s *HTTPServer) nodeForceEvaluate(resp http.ResponseWriter, req *http.Request, 51 nodeID string) (interface{}, error) { 52 if req.Method != "PUT" && req.Method != "POST" { 53 return nil, CodedError(405, ErrInvalidMethod) 54 } 55 args := structs.NodeEvaluateRequest{ 56 NodeID: nodeID, 57 } 58 s.parseRegion(req, &args.Region) 59 60 var out structs.NodeUpdateResponse 61 if err := s.agent.RPC("Node.Evaluate", &args, &out); err != nil { 62 return nil, err 63 } 64 setIndex(resp, out.Index) 65 return out, nil 66 } 67 68 func (s *HTTPServer) nodeAllocations(resp http.ResponseWriter, req *http.Request, 69 nodeID string) (interface{}, error) { 70 if req.Method != "GET" { 71 return nil, CodedError(405, ErrInvalidMethod) 72 } 73 args := structs.NodeSpecificRequest{ 74 NodeID: nodeID, 75 } 76 if s.parse(resp, req, &args.Region, &args.QueryOptions) { 77 return nil, nil 78 } 79 80 var out structs.NodeAllocsResponse 81 if err := s.agent.RPC("Node.GetAllocs", &args, &out); err != nil { 82 return nil, err 83 } 84 85 setMeta(resp, &out.QueryMeta) 86 if out.Allocs == nil { 87 out.Allocs = make([]*structs.Allocation, 0) 88 } 89 return out.Allocs, nil 90 } 91 92 func (s *HTTPServer) nodeToggleDrain(resp http.ResponseWriter, req *http.Request, 93 nodeID string) (interface{}, error) { 94 if req.Method != "PUT" && req.Method != "POST" { 95 return nil, CodedError(405, ErrInvalidMethod) 96 } 97 98 // Get the enable value 99 enableRaw := req.URL.Query().Get("enable") 100 if enableRaw == "" { 101 return nil, CodedError(400, "missing enable value") 102 } 103 enable, err := strconv.ParseBool(enableRaw) 104 if err != nil { 105 return nil, CodedError(400, "invalid enable value") 106 } 107 108 args := structs.NodeUpdateDrainRequest{ 109 NodeID: nodeID, 110 Drain: enable, 111 } 112 s.parseRegion(req, &args.Region) 113 114 var out structs.NodeDrainUpdateResponse 115 if err := s.agent.RPC("Node.UpdateDrain", &args, &out); err != nil { 116 return nil, err 117 } 118 setIndex(resp, out.Index) 119 return out, nil 120 } 121 122 func (s *HTTPServer) nodeQuery(resp http.ResponseWriter, req *http.Request, 123 nodeID string) (interface{}, error) { 124 if req.Method != "GET" { 125 return nil, CodedError(405, ErrInvalidMethod) 126 } 127 args := structs.NodeSpecificRequest{ 128 NodeID: nodeID, 129 } 130 if s.parse(resp, req, &args.Region, &args.QueryOptions) { 131 return nil, nil 132 } 133 134 var out structs.SingleNodeResponse 135 if err := s.agent.RPC("Node.GetNode", &args, &out); err != nil { 136 return nil, err 137 } 138 139 setMeta(resp, &out.QueryMeta) 140 if out.Node == nil { 141 return nil, CodedError(404, "node not found") 142 } 143 return out.Node, nil 144 }