release_handler.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // releaseLockHandler deletes the lock.
  9. func (h *handler) releaseLockHandler(w http.ResponseWriter, req *http.Request) error {
  10. h.client.SyncCluster()
  11. vars := mux.Vars(req)
  12. keypath := path.Join(prefix, vars["key"])
  13. // Read index and value parameters.
  14. index := req.FormValue("index")
  15. value := req.FormValue("value")
  16. if len(index) == 0 && len(value) == 0 {
  17. return etcdErr.NewError(etcdErr.EcodeIndexOrValueRequired, "Release", 0)
  18. } else if len(index) != 0 && len(value) != 0 {
  19. return etcdErr.NewError(etcdErr.EcodeIndexValueMutex, "Release", 0)
  20. }
  21. // Look up index by value if index is missing.
  22. if len(index) == 0 {
  23. resp, err := h.client.Get(keypath, true, true)
  24. if err != nil {
  25. return err
  26. }
  27. nodes := lockNodes{resp.Node.Nodes}
  28. node, _ := nodes.FindByValue(value)
  29. if node == nil {
  30. return etcdErr.NewError(etcdErr.EcodeKeyNotFound, "Release", 0)
  31. }
  32. index = path.Base(node.Key)
  33. }
  34. // Delete the lock.
  35. if _, err := h.client.Delete(path.Join(keypath, index), false); err != nil {
  36. return err
  37. }
  38. return nil
  39. }