capability.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v2http
  15. import (
  16. "fmt"
  17. "net/http"
  18. "github.com/coreos/etcd/etcdserver/api"
  19. "github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
  20. )
  21. func capabilityHandler(c api.Capability, fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
  22. return func(w http.ResponseWriter, r *http.Request) {
  23. if !api.IsCapabilityEnabled(c) {
  24. notCapable(w, r, c)
  25. return
  26. }
  27. fn(w, r)
  28. }
  29. }
  30. func notCapable(w http.ResponseWriter, r *http.Request, c api.Capability) {
  31. herr := httptypes.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Not capable of accessing %s feature during rolling upgrades.", c))
  32. if err := herr.WriteTo(w); err != nil {
  33. plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr)
  34. }
  35. }