open-nomad/command/agent/deployment_endpoint.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

226 lines
7.6 KiB
Go
Raw Normal View History

2017-06-29 21:50:47 +00:00
package agent
import (
"net/http"
"strings"
"github.com/hashicorp/nomad/nomad/structs"
)
func (s *HTTPServer) DeploymentsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodGet {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
args := structs.DeploymentListRequest{}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.DeploymentListResponse
if err := s.agent.RPC("Deployment.List", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Deployments == nil {
out.Deployments = make([]*structs.Deployment, 0)
}
return out.Deployments, nil
}
func (s *HTTPServer) DeploymentSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
path := strings.TrimPrefix(req.URL.Path, "/v1/deployment/")
switch {
case strings.HasPrefix(path, "allocations/"):
deploymentID := strings.TrimPrefix(path, "allocations/")
return s.deploymentAllocations(resp, req, deploymentID)
case strings.HasPrefix(path, "fail/"):
deploymentID := strings.TrimPrefix(path, "fail/")
return s.deploymentFail(resp, req, deploymentID)
case strings.HasPrefix(path, "pause/"):
deploymentID := strings.TrimPrefix(path, "pause/")
return s.deploymentPause(resp, req, deploymentID)
case strings.HasPrefix(path, "promote/"):
deploymentID := strings.TrimPrefix(path, "promote/")
return s.deploymentPromote(resp, req, deploymentID)
case strings.HasPrefix(path, "allocation-health/"):
deploymentID := strings.TrimPrefix(path, "allocation-health/")
return s.deploymentSetAllocHealth(resp, req, deploymentID)
case strings.HasPrefix(path, "unblock/"):
deploymentID := strings.TrimPrefix(path, "unblock/")
return s.deploymentUnblock(resp, req, deploymentID)
2017-06-29 21:50:47 +00:00
default:
return s.deploymentQuery(resp, req, path)
}
}
// TODO test and api
func (s *HTTPServer) deploymentFail(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodPut && req.Method != http.MethodPost {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
args := structs.DeploymentFailRequest{
DeploymentID: deploymentID,
}
2017-09-07 23:56:15 +00:00
s.parseWriteRequest(req, &args.WriteRequest)
2017-06-29 21:50:47 +00:00
var out structs.DeploymentUpdateResponse
if err := s.agent.RPC("Deployment.Fail", &args, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}
func (s *HTTPServer) deploymentPause(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodPut && req.Method != http.MethodPost {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
var pauseRequest structs.DeploymentPauseRequest
if err := decodeBody(req, &pauseRequest); err != nil {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, err.Error())
2017-06-29 21:50:47 +00:00
}
if pauseRequest.DeploymentID == "" {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "DeploymentID must be specified")
2017-06-29 21:50:47 +00:00
}
if pauseRequest.DeploymentID != deploymentID {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "Deployment ID does not match")
2017-06-29 21:50:47 +00:00
}
2017-09-07 23:56:15 +00:00
s.parseWriteRequest(req, &pauseRequest.WriteRequest)
2017-06-29 21:50:47 +00:00
var out structs.DeploymentUpdateResponse
if err := s.agent.RPC("Deployment.Pause", &pauseRequest, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}
func (s *HTTPServer) deploymentPromote(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodPut && req.Method != http.MethodPost {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
var promoteRequest structs.DeploymentPromoteRequest
if err := decodeBody(req, &promoteRequest); err != nil {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, err.Error())
2017-06-29 21:50:47 +00:00
}
if promoteRequest.DeploymentID == "" {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "DeploymentID must be specified")
2017-06-29 21:50:47 +00:00
}
if promoteRequest.DeploymentID != deploymentID {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "Deployment ID does not match")
2017-06-29 21:50:47 +00:00
}
2017-09-07 23:56:15 +00:00
s.parseWriteRequest(req, &promoteRequest.WriteRequest)
2017-06-29 21:50:47 +00:00
var out structs.DeploymentUpdateResponse
if err := s.agent.RPC("Deployment.Promote", &promoteRequest, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}
func (s *HTTPServer) deploymentUnblock(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodPut && req.Method != http.MethodPost {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
}
var unblockRequest structs.DeploymentUnblockRequest
if err := decodeBody(req, &unblockRequest); err != nil {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, err.Error())
}
if unblockRequest.DeploymentID == "" {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "DeploymentID must be specified")
}
if unblockRequest.DeploymentID != deploymentID {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "Deployment ID does not match")
}
s.parseWriteRequest(req, &unblockRequest.WriteRequest)
var out structs.DeploymentUpdateResponse
if err := s.agent.RPC("Deployment.Unblock", &unblockRequest, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}
2017-06-29 21:50:47 +00:00
func (s *HTTPServer) deploymentSetAllocHealth(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodPut && req.Method != http.MethodPost {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
var healthRequest structs.DeploymentAllocHealthRequest
if err := decodeBody(req, &healthRequest); err != nil {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, err.Error())
2017-06-29 21:50:47 +00:00
}
if healthRequest.DeploymentID == "" {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "DeploymentID must be specified")
2017-06-29 21:50:47 +00:00
}
if healthRequest.DeploymentID != deploymentID {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusBadRequest, "Deployment ID does not match")
2017-06-29 21:50:47 +00:00
}
2017-09-07 23:56:15 +00:00
s.parseWriteRequest(req, &healthRequest.WriteRequest)
2017-06-29 21:50:47 +00:00
var out structs.DeploymentUpdateResponse
if err := s.agent.RPC("Deployment.SetAllocHealth", &healthRequest, &out); err != nil {
return nil, err
}
setIndex(resp, out.Index)
return out, nil
}
func (s *HTTPServer) deploymentAllocations(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodGet {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
args := structs.DeploymentSpecificRequest{
DeploymentID: deploymentID,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.AllocListResponse
if err := s.agent.RPC("Deployment.Allocations", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Allocations == nil {
out.Allocations = make([]*structs.AllocListStub, 0)
}
for _, alloc := range out.Allocations {
alloc.SetEventDisplayMessages()
}
2017-06-29 21:50:47 +00:00
return out.Allocations, nil
}
func (s *HTTPServer) deploymentQuery(resp http.ResponseWriter, req *http.Request, deploymentID string) (interface{}, error) {
2020-06-15 20:00:44 +00:00
if req.Method != http.MethodGet {
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
2017-06-29 21:50:47 +00:00
}
args := structs.DeploymentSpecificRequest{
DeploymentID: deploymentID,
}
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
return nil, nil
}
var out structs.SingleDeploymentResponse
if err := s.agent.RPC("Deployment.GetDeployment", &args, &out); err != nil {
return nil, err
}
setMeta(resp, &out.QueryMeta)
if out.Deployment == nil {
2020-06-15 20:00:44 +00:00
return nil, CodedError(http.StatusNotFound, "deployment not found")
2017-06-29 21:50:47 +00:00
}
return out.Deployment, nil
}