diff --git a/README.md b/README.md index b30ec89..b8b75d3 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ Zookeeper HTTP rest API - [Get node data](#get-node-data) - [Errors](#errors) - [Create node recursive](#create-node-recursive) + - [Create node children](#create-node-children) + - [Errors](#errors) - [Update node](#update-node) - [Errors](#errors) - [Delete node recursive](#delete-node-recursive) @@ -22,7 +24,6 @@ Zookeeper HTTP rest API - [Binary](#binary) - [Docker build](#docker-build) - [Binary file](#binary-file) - - [Docker image](#docker-image) - [AUTHORS](#authors) - [LICENSE](#license) @@ -122,6 +123,25 @@ curl -XPUT http://127.0.0.1:8889/v1/up/two/three/four -d '{"four": "json"}' /zoorest/two/three/four ``` +### Create node children + +Method: **PATCH** + +Location: **/v1/up** + +Return string with created children path +``` +curl -XPATCH http://127.0.0.1:8889/v1/up/one/test -d 'test' +/one/test +``` + +#### Errors + +``` +curl -XPATCH http://127.0.0.1:8889/v1/up/six/test -d '{"six": "json"}' +zk: node does not exist +``` + ### Update node Method: **POST** diff --git a/rest/api.go b/rest/api.go index 914c06b..7e6db21 100644 --- a/rest/api.go +++ b/rest/api.go @@ -90,6 +90,8 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) { go func() { ch <- zk.CreateNode(path, content) }() } else if r.Method == "POST" { go func() { ch <- zk.UpdateNode(path, content) }() + } else if r.Method == "PATCH" { + go func() { ch <- zk.CreateChild(path, content) }() } else { e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ") w.WriteHeader(500) @@ -115,8 +117,6 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) path := vars["path"] - var rmPath string - rmPath = strings.Join([]string{zk.Path, path}, "") if path == "/" { e := "Skiping root path" w.WriteHeader(500) @@ -124,6 +124,9 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) { return } + var rmPath string + rmPath = strings.Join([]string{zk.Path, path}, "") + if strings.Contains(rmPath, "//") { rmPath = strings.Replace(rmPath, "//", "/", 1) } @@ -171,10 +174,10 @@ func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) { func Serve(listen string, zk ZooNode) { r := mux.NewRouter() - 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) - r.HandleFunc("/v1/up{path:[a-z0-9-_/.:]+}", zk.UP) + r.HandleFunc("/v1/ls{path:[A-Za-z0-9-_/.:]+}", zk.LS) + r.HandleFunc("/v1/get{path:[A-Za-z0-9-_/.:]+}", zk.GET) + r.HandleFunc("/v1/rmr{path:[A-Za-z0-9-_/.:]+}", zk.RM) + r.HandleFunc("/v1/up{path:[A-Za-z0-9-_/.:]+}", zk.UP) http.Handle("/", r) diff --git a/rest/zoo.go b/rest/zoo.go index 1dc21ab..47b2ab7 100644 --- a/rest/zoo.go +++ b/rest/zoo.go @@ -134,7 +134,7 @@ func (z ZooNode) CreateNode(path string, content []byte) string { return z.UpdateNode(path, content) } -// UpdateNode ... +// UpdateNode update existing node func (z ZooNode) UpdateNode(path string, content []byte) string { upPath := strings.Join([]string{z.Path, path}, "") if strings.Contains(upPath, "//") { @@ -152,6 +152,24 @@ func (z ZooNode) UpdateNode(path string, content []byte) string { return path } +// CreateChild create child in /node/path +func (z ZooNode) CreateChild(path string, content []byte) string { + crPath := strings.Join([]string{z.Path, path}, "") + if strings.Contains(crPath, "//") { + crPath = strings.Replace(crPath, "//", "/", 1) + } + if crPath == "/" { + return "Not updating root path" + } + + _, err := z.Conn.Create(crPath, content, 0, zk.WorldACL(zk.PermAll)) + if err != nil { + return err.Error() + } + + return path +} + //EnsureZooPath create zookeeper path func (z ZooNode) EnsureZooPath(path string) (string, error) { flag := int32(0)