github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/server/v2/delete_handler.go (about) 1 package v2 2 3 import ( 4 "net/http" 5 "strconv" 6 7 etcdErr "github.com/coreos/etcd/error" 8 "github.com/coreos/etcd/third_party/github.com/gorilla/mux" 9 ) 10 11 func DeleteHandler(w http.ResponseWriter, req *http.Request, s Server) error { 12 vars := mux.Vars(req) 13 key := "/" + vars["key"] 14 15 recursive := (req.FormValue("recursive") == "true") 16 dir := (req.FormValue("dir") == "true") 17 18 req.ParseForm() 19 _, valueOk := req.Form["prevValue"] 20 _, indexOk := req.Form["prevIndex"] 21 22 if !valueOk && !indexOk { 23 c := s.Store().CommandFactory().CreateDeleteCommand(key, dir, recursive) 24 return s.Dispatch(c, w, req) 25 } 26 27 var err error 28 prevIndex := uint64(0) 29 prevValue := req.Form.Get("prevValue") 30 31 if indexOk { 32 prevIndexStr := req.Form.Get("prevIndex") 33 prevIndex, err = strconv.ParseUint(prevIndexStr, 10, 64) 34 35 // bad previous index 36 if err != nil { 37 return etcdErr.NewError(etcdErr.EcodeIndexNaN, "CompareAndDelete", s.Store().Index()) 38 } 39 } 40 41 if valueOk { 42 if prevValue == "" { 43 return etcdErr.NewError(etcdErr.EcodePrevValueRequired, "CompareAndDelete", s.Store().Index()) 44 } 45 } 46 47 c := s.Store().CommandFactory().CreateCompareAndDeleteCommand(key, prevValue, prevIndex) 48 return s.Dispatch(c, w, req) 49 }