github.com/ilhicas/nomad@v0.11.8/command/agent/scaling_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  func (s *HTTPServer) ScalingPoliciesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    12  	switch req.Method {
    13  	case "GET":
    14  		return s.scalingPoliciesListRequest(resp, req)
    15  	default:
    16  		return nil, CodedError(405, ErrInvalidMethod)
    17  	}
    18  }
    19  
    20  func (s *HTTPServer) scalingPoliciesListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    21  	args := structs.ScalingPolicyListRequest{}
    22  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    23  		return nil, nil
    24  	}
    25  
    26  	var out structs.ScalingPolicyListResponse
    27  	if err := s.agent.RPC("Scaling.ListPolicies", &args, &out); err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	setMeta(resp, &out.QueryMeta)
    32  	if out.Policies == nil {
    33  		out.Policies = make([]*structs.ScalingPolicyListStub, 0)
    34  	}
    35  	return out.Policies, nil
    36  }
    37  
    38  func (s *HTTPServer) ScalingPolicySpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    39  	path := strings.TrimPrefix(req.URL.Path, "/v1/scaling/policy/")
    40  	switch {
    41  	default:
    42  		return s.scalingPolicyCRUD(resp, req, path)
    43  	}
    44  }
    45  
    46  func (s *HTTPServer) scalingPolicyCRUD(resp http.ResponseWriter, req *http.Request,
    47  	policyID string) (interface{}, error) {
    48  	switch req.Method {
    49  	case "GET":
    50  		return s.scalingPolicyQuery(resp, req, policyID)
    51  	default:
    52  		return nil, CodedError(405, ErrInvalidMethod)
    53  	}
    54  }
    55  
    56  func (s *HTTPServer) scalingPolicyQuery(resp http.ResponseWriter, req *http.Request,
    57  	policyID string) (interface{}, error) {
    58  	args := structs.ScalingPolicySpecificRequest{
    59  		ID: policyID,
    60  	}
    61  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    62  		return nil, nil
    63  	}
    64  
    65  	var out structs.SingleScalingPolicyResponse
    66  	if err := s.agent.RPC("Scaling.GetPolicy", &args, &out); err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	setMeta(resp, &out.QueryMeta)
    71  	if out.Policy == nil {
    72  		return nil, CodedError(404, "policy not found")
    73  	}
    74  
    75  	return out.Policy, nil
    76  }
    77  
    78  func ApiScalingPolicyToStructs(count int, ap *api.ScalingPolicy) *structs.ScalingPolicy {
    79  	p := structs.ScalingPolicy{
    80  		Enabled: *ap.Enabled,
    81  		Max:     ap.Max,
    82  		Policy:  ap.Policy,
    83  		Target:  map[string]string{},
    84  	}
    85  	if ap.Min != nil {
    86  		p.Min = *ap.Min
    87  	} else {
    88  		p.Min = int64(count)
    89  	}
    90  	return &p
    91  }