renew_handler.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package v2
  2. import (
  3. "path"
  4. "net/http"
  5. "strconv"
  6. "github.com/gorilla/mux"
  7. etcdErr "github.com/coreos/etcd/error"
  8. )
  9. // renewLockHandler attempts to update the TTL on an existing lock.
  10. // Returns a 200 OK if successful. Returns non-200 on error.
  11. func (h *handler) renewLockHandler(w http.ResponseWriter, req *http.Request) error {
  12. h.client.SyncCluster()
  13. // Read the lock path.
  14. vars := mux.Vars(req)
  15. keypath := path.Join(prefix, vars["key"])
  16. // Parse new TTL parameter.
  17. ttl, err := strconv.Atoi(req.FormValue("ttl"))
  18. if err != nil {
  19. return etcdErr.NewError(etcdErr.EcodeTTLNaN, "Renew", 0)
  20. }
  21. // Read and set defaults for index and value.
  22. index := req.FormValue("index")
  23. value := req.FormValue("value")
  24. if len(index) == 0 && len(value) == 0 {
  25. return etcdErr.NewError(etcdErr.EcodeIndexOrValueRequired, "Renew", 0)
  26. }
  27. if len(index) == 0 {
  28. // If index is not specified then look it up by value.
  29. resp, err := h.client.Get(keypath, true, true)
  30. if err != nil {
  31. return err
  32. }
  33. nodes := lockNodes{resp.Node.Nodes}
  34. node, _ := nodes.FindByValue(value)
  35. if node == nil {
  36. return etcdErr.NewError(etcdErr.EcodeKeyNotFound, "Renew", 0)
  37. }
  38. index = path.Base(node.Key)
  39. } else if len(value) == 0 {
  40. // If value is not specified then default it to the previous value.
  41. resp, err := h.client.Get(path.Join(keypath, index), true, false)
  42. if err != nil {
  43. return err
  44. }
  45. value = resp.Node.Value
  46. }
  47. // Renew the lock, if it exists.
  48. if _, err = h.client.Update(path.Join(keypath, index), value, uint64(ttl)); err != nil {
  49. return err
  50. }
  51. return nil
  52. }