get_index_handler.go 971 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package v2
  2. import (
  3. "net/http"
  4. "path"
  5. etcdErr "github.com/coreos/etcd/error"
  6. "github.com/coreos/etcd/third_party/github.com/gorilla/mux"
  7. )
  8. // getIndexHandler retrieves the current lock index.
  9. // The "field" parameter specifies to read either the lock "index" or lock "value".
  10. func (h *handler) getIndexHandler(w http.ResponseWriter, req *http.Request) error {
  11. h.client.SyncCluster()
  12. vars := mux.Vars(req)
  13. keypath := path.Join(prefix, vars["key"])
  14. field := req.FormValue("field")
  15. if len(field) == 0 {
  16. field = "value"
  17. }
  18. // Read all indices.
  19. resp, err := h.client.Get(keypath, true, true)
  20. if err != nil {
  21. return err
  22. }
  23. nodes := lockNodes{resp.Node.Nodes}
  24. // Write out the requested field.
  25. if node := nodes.First(); node != nil {
  26. switch field {
  27. case "index":
  28. w.Write([]byte(path.Base(node.Key)))
  29. case "value":
  30. w.Write([]byte(node.Value))
  31. default:
  32. return etcdErr.NewError(etcdErr.EcodeInvalidField, "Get", 0)
  33. }
  34. }
  35. return nil
  36. }