From f2bc4457e07c5fdec42a899ed4e8828ee5295636 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Wed, 15 Feb 2017 19:13:12 +0300 Subject: [PATCH] rmr --- rest/api.go | 12 ++++++++++++ rest/zoo.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/rest/api.go b/rest/api.go index 0bb0e7c..f2c679d 100644 --- a/rest/api.go +++ b/rest/api.go @@ -50,6 +50,17 @@ func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) { w.Write(data) } +// RM ... +func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + path := vars["path"] + + go func() { zk.RMR(path) }() + + w.WriteHeader(200) + // w.Write(data) +} + // GET ... func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -83,6 +94,7 @@ func Serve(listen string, zk ZooNode) { r.HandleFunc("/v1/ls{path:[a-z0-9-_/.:]+}", zk.LS) r.HandleFunc("/v1/get{path:[a-z0-9-_/.:]+}", zk.GET) + r.HandleFunc("/v1/rmr{path:[a-z0-9-_/.:]+}", zk.RM) http.Handle("/", r) diff --git a/rest/zoo.go b/rest/zoo.go index dad9b35..8dc01ef 100644 --- a/rest/zoo.go +++ b/rest/zoo.go @@ -92,3 +92,34 @@ func (z ZooNode) GetNode(path string) Get { return g } + +//RMR remove Zk node recursive +func (z ZooNode) RMR(path string) { + var rmPath string + rmPath = strings.Join([]string{z.Path, path}, "") + if path == "/" { + return + } + + if strings.Contains(rmPath, "//") { + rmPath = strings.Replace(rmPath, "//", "/", 1) + } + + log.Print("rm: ", rmPath) + c, _, err := z.Conn.Children(rmPath) + if err != nil { + log.Print("[zk ERROR] ", err) + } + log.Print("[WARNING] Trying delete ", path) + if len(c) > 0 { + for _, child := range c { + childPath := strings.Join([]string{rmPath, child}, "/") + z.RMR(childPath) + } + } + err = z.Conn.Delete(path, -1) + if err != nil { + log.Print("[zk ERROR] ", err) + } + log.Print("[WARNING] ", path, " deleted") +}