github.com/hooklift/nomad@v0.5.7-0.20170407200202-db11e7dd7b55/command/agent/operator_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/hashicorp/raft"
     9  )
    10  
    11  func (s *HTTPServer) OperatorRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    12  	path := strings.TrimPrefix(req.URL.Path, "/v1/operator/raft/")
    13  	switch {
    14  	case strings.HasPrefix(path, "configuration"):
    15  		return s.OperatorRaftConfiguration(resp, req)
    16  	case strings.HasPrefix(path, "peer"):
    17  		return s.OperatorRaftPeer(resp, req)
    18  	default:
    19  		return nil, CodedError(404, ErrInvalidMethod)
    20  	}
    21  }
    22  
    23  // OperatorRaftConfiguration is used to inspect the current Raft configuration.
    24  // This supports the stale query mode in case the cluster doesn't have a leader.
    25  func (s *HTTPServer) OperatorRaftConfiguration(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    26  	if req.Method != "GET" {
    27  		resp.WriteHeader(http.StatusMethodNotAllowed)
    28  		return nil, nil
    29  	}
    30  
    31  	var args structs.GenericRequest
    32  	if done := s.parse(resp, req, &args.Region, &args.QueryOptions); done {
    33  		return nil, nil
    34  	}
    35  
    36  	var reply structs.RaftConfigurationResponse
    37  	if err := s.agent.RPC("Operator.RaftGetConfiguration", &args, &reply); err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return reply, nil
    42  }
    43  
    44  // OperatorRaftPeer supports actions on Raft peers. Currently we only support
    45  // removing peers by address.
    46  func (s *HTTPServer) OperatorRaftPeer(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    47  	if req.Method != "DELETE" {
    48  		resp.WriteHeader(http.StatusMethodNotAllowed)
    49  		return nil, nil
    50  	}
    51  
    52  	var args structs.RaftPeerByAddressRequest
    53  	s.parseRegion(req, &args.Region)
    54  
    55  	params := req.URL.Query()
    56  	if _, ok := params["address"]; ok {
    57  		args.Address = raft.ServerAddress(params.Get("address"))
    58  	} else {
    59  		resp.WriteHeader(http.StatusBadRequest)
    60  		resp.Write([]byte("Must specify ?address with IP:port of peer to remove"))
    61  		return nil, nil
    62  	}
    63  
    64  	var reply struct{}
    65  	if err := s.agent.RPC("Operator.RaftRemovePeerByAddress", &args, &reply); err != nil {
    66  		return nil, err
    67  	}
    68  	return nil, nil
    69  }