github.com/anuvu/nomad@v0.8.7-atom1/command/agent/deployment_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) DeploymentsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { 11 if req.Method != "GET" { 12 return nil, CodedError(405, ErrInvalidMethod) 13 } 14 15 args := structs.DeploymentListRequest{} 16 if s.parse(resp, req, &args.Region, &args.QueryOptions) { 17 return nil, nil 18 } 19 20 var out structs.DeploymentListResponse 21 if err := s.agent.RPC("Deployment.List", &args, &out); err != nil { 22 return nil, err 23 } 24 25 setMeta(resp, &out.QueryMeta) 26 if out.Deployments == nil { 27 out.Deployments = make([]*structs.Deployment, 0) 28 } 29 return out.Deployments, nil 30 } 31 32 func (s *HTTPServer) DeploymentSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { 33 path := strings.TrimPrefix(req.URL.Path, "/v1/deployment/") 34 switch { 35 case strings.HasPrefix(path, "allocations/"): 36 deploymentID := strings.TrimPrefix(path, "allocations/") 37 return s.deploymentAllocations(resp, req, deploymentID) 38 case strings.HasPrefix(path, "fail/"): 39 deploymentID := strings.TrimPrefix(path, "fail/") 40 return s.deploymentFail(resp, req, deploymentID) 41 case strings.HasPrefix(path, "pause/"): 42 deploymentID := strings.TrimPrefix(path, "pause/") 43 return s.deploymentPause(resp, req, deploymentID) 44 case strings.HasPrefix(path, "promote/"): 45 deploymentID := strings.TrimPrefix(path, "promote/") 46 return s.deploymentPromote(resp, req, deploymentID) 47 case strings.HasPrefix(path, "allocation-health/"): 48 deploymentID := strings.TrimPrefix(path, "allocation-health/") 49 return s.deploymentSetAllocHealth(resp, req, deploymentID) 50 default: 51 return s.deploymentQuery(resp, req, path) 52 } 53 } 54 55 // TODO test and api 56 func (s *HTTPServer) deploymentFail(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) { 57 if req.Method != "PUT" && req.Method != "POST" { 58 return nil, CodedError(405, ErrInvalidMethod) 59 } 60 args := structs.DeploymentFailRequest{ 61 DeploymentID: deploymentID, 62 } 63 s.parseWriteRequest(req, &args.WriteRequest) 64 65 var out structs.DeploymentUpdateResponse 66 if err := s.agent.RPC("Deployment.Fail", &args, &out); err != nil { 67 return nil, err 68 } 69 setIndex(resp, out.Index) 70 return out, nil 71 } 72 73 func (s *HTTPServer) deploymentPause(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) { 74 if req.Method != "PUT" && req.Method != "POST" { 75 return nil, CodedError(405, ErrInvalidMethod) 76 } 77 78 var pauseRequest structs.DeploymentPauseRequest 79 if err := decodeBody(req, &pauseRequest); err != nil { 80 return nil, CodedError(400, err.Error()) 81 } 82 if pauseRequest.DeploymentID == "" { 83 return nil, CodedError(400, "DeploymentID must be specified") 84 } 85 if pauseRequest.DeploymentID != deploymentID { 86 return nil, CodedError(400, "Deployment ID does not match") 87 } 88 s.parseWriteRequest(req, &pauseRequest.WriteRequest) 89 90 var out structs.DeploymentUpdateResponse 91 if err := s.agent.RPC("Deployment.Pause", &pauseRequest, &out); err != nil { 92 return nil, err 93 } 94 setIndex(resp, out.Index) 95 return out, nil 96 } 97 98 func (s *HTTPServer) deploymentPromote(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) { 99 if req.Method != "PUT" && req.Method != "POST" { 100 return nil, CodedError(405, ErrInvalidMethod) 101 } 102 103 var promoteRequest structs.DeploymentPromoteRequest 104 if err := decodeBody(req, &promoteRequest); err != nil { 105 return nil, CodedError(400, err.Error()) 106 } 107 if promoteRequest.DeploymentID == "" { 108 return nil, CodedError(400, "DeploymentID must be specified") 109 } 110 if promoteRequest.DeploymentID != deploymentID { 111 return nil, CodedError(400, "Deployment ID does not match") 112 } 113 s.parseWriteRequest(req, &promoteRequest.WriteRequest) 114 115 var out structs.DeploymentUpdateResponse 116 if err := s.agent.RPC("Deployment.Promote", &promoteRequest, &out); err != nil { 117 return nil, err 118 } 119 setIndex(resp, out.Index) 120 return out, nil 121 } 122 123 func (s *HTTPServer) deploymentSetAllocHealth(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) { 124 if req.Method != "PUT" && req.Method != "POST" { 125 return nil, CodedError(405, ErrInvalidMethod) 126 } 127 128 var healthRequest structs.DeploymentAllocHealthRequest 129 if err := decodeBody(req, &healthRequest); err != nil { 130 return nil, CodedError(400, err.Error()) 131 } 132 if healthRequest.DeploymentID == "" { 133 return nil, CodedError(400, "DeploymentID must be specified") 134 } 135 if healthRequest.DeploymentID != deploymentID { 136 return nil, CodedError(400, "Deployment ID does not match") 137 } 138 s.parseWriteRequest(req, &healthRequest.WriteRequest) 139 140 var out structs.DeploymentUpdateResponse 141 if err := s.agent.RPC("Deployment.SetAllocHealth", &healthRequest, &out); err != nil { 142 return nil, err 143 } 144 setIndex(resp, out.Index) 145 return out, nil 146 } 147 148 func (s *HTTPServer) deploymentAllocations(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) { 149 if req.Method != "GET" { 150 return nil, CodedError(405, ErrInvalidMethod) 151 } 152 153 args := structs.DeploymentSpecificRequest{ 154 DeploymentID: deploymentID, 155 } 156 if s.parse(resp, req, &args.Region, &args.QueryOptions) { 157 return nil, nil 158 } 159 160 var out structs.AllocListResponse 161 if err := s.agent.RPC("Deployment.Allocations", &args, &out); err != nil { 162 return nil, err 163 } 164 165 setMeta(resp, &out.QueryMeta) 166 if out.Allocations == nil { 167 out.Allocations = make([]*structs.AllocListStub, 0) 168 } 169 for _, alloc := range out.Allocations { 170 alloc.SetEventDisplayMessages() 171 } 172 return out.Allocations, nil 173 } 174 175 func (s *HTTPServer) deploymentQuery(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) { 176 if req.Method != "GET" { 177 return nil, CodedError(405, ErrInvalidMethod) 178 } 179 180 args := structs.DeploymentSpecificRequest{ 181 DeploymentID: deploymentID, 182 } 183 if s.parse(resp, req, &args.Region, &args.QueryOptions) { 184 return nil, nil 185 } 186 187 var out structs.SingleDeploymentResponse 188 if err := s.agent.RPC("Deployment.GetDeployment", &args, &out); err != nil { 189 return nil, err 190 } 191 192 setMeta(resp, &out.QueryMeta) 193 if out.Deployment == nil { 194 return nil, CodedError(404, "deployment not found") 195 } 196 return out.Deployment, nil 197 }