v2_http_delete.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2014 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "net/http"
  16. "strconv"
  17. etcdErr "github.com/coreos/etcd/error"
  18. )
  19. func (p *participant) DeleteHandler(w http.ResponseWriter, req *http.Request) error {
  20. if !p.node.IsLeader() {
  21. return p.redirect(w, req, p.node.Leader())
  22. }
  23. key := req.URL.Path[len("/v2/keys"):]
  24. recursive := (req.FormValue("recursive") == "true")
  25. dir := (req.FormValue("dir") == "true")
  26. req.ParseForm()
  27. _, valueOk := req.Form["prevValue"]
  28. _, indexOk := req.Form["prevIndex"]
  29. if !valueOk && !indexOk {
  30. return p.serveDelete(w, req, key, dir, recursive)
  31. }
  32. var err error
  33. prevIndex := uint64(0)
  34. prevValue := req.Form.Get("prevValue")
  35. if indexOk {
  36. prevIndexStr := req.Form.Get("prevIndex")
  37. prevIndex, err = strconv.ParseUint(prevIndexStr, 10, 64)
  38. // bad previous index
  39. if err != nil {
  40. return etcdErr.NewError(etcdErr.EcodeIndexNaN, "CompareAndDelete", p.Store.Index())
  41. }
  42. }
  43. if valueOk {
  44. if prevValue == "" {
  45. return etcdErr.NewError(etcdErr.EcodePrevValueRequired, "CompareAndDelete", p.Store.Index())
  46. }
  47. }
  48. return p.serveCAD(w, req, key, prevValue, prevIndex)
  49. }
  50. func (p *participant) serveDelete(w http.ResponseWriter, req *http.Request, key string, dir, recursive bool) error {
  51. ret, err := p.Delete(key, dir, recursive)
  52. if err == nil {
  53. p.handleRet(w, ret)
  54. return nil
  55. }
  56. return err
  57. }
  58. func (p *participant) serveCAD(w http.ResponseWriter, req *http.Request, key string, prevValue string, prevIndex uint64) error {
  59. ret, err := p.CAD(key, prevValue, prevIndex)
  60. if err == nil {
  61. p.handleRet(w, ret)
  62. return nil
  63. }
  64. return err
  65. }