diff --git a/api/operator.go b/api/operator.go index d5bc5d061d56..a4d8c56c2d11 100644 --- a/api/operator.go +++ b/api/operator.go @@ -1,6 +1,8 @@ package api import ( + "encoding/json" + "errors" "io" "io/ioutil" "strconv" @@ -297,10 +299,26 @@ func (op *Operator) LicensePut(license string, q *WriteOptions) (*WriteMeta, err } func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, *QueryMeta, error) { + req, err := op.c.newRequest("GET", "/v1/operator/license") + if err != nil { + return nil, nil, err + } + var reply LicenseReply - qm, err := op.c.query("/v1/operator/license", &reply, q) + _, resp, err := op.c.doRequest(req) if err != nil { return nil, nil, err } - return &reply, qm, nil + defer resp.Body.Close() + + if resp.StatusCode == 204 { + return nil, nil, errors.New("Nomad Enterprise only endpoint") + } + + err = json.NewDecoder(resp.Body).Decode(&reply) + if err == nil { + return &reply, nil, nil + } + + return nil, nil, err } diff --git a/command/agent/http.go b/command/agent/http.go index bbf071cbe0e0..c7568fd6e2e1 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -314,6 +314,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/v1/search", s.wrap(s.SearchRequest)) + s.mux.HandleFunc("/v1/operator/license", s.wrap(s.LicenseRequest)) s.mux.HandleFunc("/v1/operator/raft/", s.wrap(s.OperatorRequest)) s.mux.HandleFunc("/v1/operator/autopilot/configuration", s.wrap(s.OperatorAutopilotConfiguration)) s.mux.HandleFunc("/v1/operator/autopilot/health", s.wrap(s.OperatorServerHealth)) diff --git a/command/agent/http_oss.go b/command/agent/http_oss.go index e90818141501..863ab4bc27ca 100644 --- a/command/agent/http_oss.go +++ b/command/agent/http_oss.go @@ -16,8 +16,6 @@ func (s *HTTPServer) registerEnterpriseHandlers() { s.mux.HandleFunc("/v1/quota/", s.wrap(s.entOnly)) s.mux.HandleFunc("/v1/quota", s.wrap(s.entOnly)) - s.mux.HandleFunc("/v1/operator/license", s.wrap(s.entOnly)) - s.mux.HandleFunc("/v1/recommendation", s.wrap(s.entOnly)) s.mux.HandleFunc("/v1/recommendations", s.wrap(s.entOnly)) s.mux.HandleFunc("/v1/recommendations/apply", s.wrap(s.entOnly)) diff --git a/command/agent/operator_endpoint_oss.go b/command/agent/operator_endpoint_oss.go new file mode 100644 index 000000000000..d589afebebcd --- /dev/null +++ b/command/agent/operator_endpoint_oss.go @@ -0,0 +1,20 @@ +// +build !ent + +package agent + +import ( + "net/http" +) + +func (s *HTTPServer) LicenseRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + switch req.Method { + case "GET": + resp.WriteHeader(http.StatusNoContent) + return nil, nil + case "PUT": + return nil, CodedError(501, ErrEntOnly) + default: + return nil, CodedError(405, ErrInvalidMethod) + } + +} diff --git a/vendor/github.com/hashicorp/nomad/api/operator.go b/vendor/github.com/hashicorp/nomad/api/operator.go index d5bc5d061d56..a4d8c56c2d11 100644 --- a/vendor/github.com/hashicorp/nomad/api/operator.go +++ b/vendor/github.com/hashicorp/nomad/api/operator.go @@ -1,6 +1,8 @@ package api import ( + "encoding/json" + "errors" "io" "io/ioutil" "strconv" @@ -297,10 +299,26 @@ func (op *Operator) LicensePut(license string, q *WriteOptions) (*WriteMeta, err } func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, *QueryMeta, error) { + req, err := op.c.newRequest("GET", "/v1/operator/license") + if err != nil { + return nil, nil, err + } + var reply LicenseReply - qm, err := op.c.query("/v1/operator/license", &reply, q) + _, resp, err := op.c.doRequest(req) if err != nil { return nil, nil, err } - return &reply, qm, nil + defer resp.Body.Close() + + if resp.StatusCode == 204 { + return nil, nil, errors.New("Nomad Enterprise only endpoint") + } + + err = json.NewDecoder(resp.Body).Decode(&reply) + if err == nil { + return &reply, nil, nil + } + + return nil, nil, err }