From 4d99e9b1891e3ad9a354de2c566f78735f917efb Mon Sep 17 00:00:00 2001 From: seokho-son Date: Wed, 17 Mar 2021 17:56:10 +0900 Subject: [PATCH 1/2] Add tumblebug obj delete feature --- src/api/rest/server/common/utility.go | 57 +++++++++- src/api/rest/server/server.go | 6 +- src/core/common/utility.go | 24 +++++ src/docs/docs.go | 100 ++++++++++++++++-- src/docs/swagger.json | 100 ++++++++++++++++-- src/docs/swagger.yaml | 68 ++++++++++-- .../sequentialFullTest/delete-object.sh | 17 +++ .../delete-objects-becareful.sh | 17 +++ .../official/sequentialFullTest/get-object.sh | 2 +- .../sequentialFullTest/list-object.sh | 2 +- 10 files changed, 365 insertions(+), 28 deletions(-) create mode 100755 test/official/sequentialFullTest/delete-object.sh create mode 100755 test/official/sequentialFullTest/delete-objects-becareful.sh diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index aeb84e1d8..55362b3e1 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -122,7 +122,7 @@ type ObjectList struct { // @Success 200 {object} common.SimpleMsg // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg -// @Router /objectList [get] +// @Router /objects [get] func RestGetObjectList(c echo.Context) error { parentKey := c.QueryParam("key") fmt.Printf("[Get Tumblebug Object List] with Key: %s \n", parentKey) @@ -148,7 +148,7 @@ func RestGetObjectList(c echo.Context) error { // @Success 200 {object} common.SimpleMsg // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg -// @Router /objectValue [get] +// @Router /object [get] func RestGetObjectValue(c echo.Context) error { parentKey := c.QueryParam("key") fmt.Printf("[Get Tumblebug Object Value] with Key: %s \n", parentKey) @@ -162,4 +162,57 @@ func RestGetObjectValue(c echo.Context) error { json.Unmarshal([]byte(content), &contentJSON) return c.JSON(http.StatusOK, &contentJSON) +} + +// func RestDeleteObjectValue is a rest api wrapper for DeleteObject. +// RestDeleteObjectValue godoc +// @Summary Delete value of an object +// @Description Delete value of an object +// @Tags Admin +// @Accept json +// @Produce json +// @Param key query string true "delete object value by key" +// @Success 200 {object} common.SimpleMsg +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /object [delete] +func RestDeleteObject(c echo.Context) error { + parentKey := c.QueryParam("key") + fmt.Printf("[Delete Tumblebug Object] with Key: %s \n", parentKey) + + content, err := common.GetObjectValue(parentKey) + if err != nil || content == "" { + return SendMessage(c, http.StatusOK, "Cannot find [" + parentKey+ "] object") + } + + err = common.DeleteObject(parentKey) + if err != nil { + return SendMessage(c, http.StatusOK, "Cannot delete [" + parentKey+ "] object") + } + + return SendMessage(c, http.StatusOK, "The object has been deleted") +} + +// func RestDeleteObjects is a rest api wrapper for DeleteObjects. +// RestDeleteObjects godoc +// @Summary Delete objects +// @Description Delete objects +// @Tags Admin +// @Accept json +// @Produce json +// @Param key query string true "Delete child objects based on the given key string" +// @Success 200 {object} common.SimpleMsg +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /objects [delete] +func RestDeleteObjects(c echo.Context) error { + parentKey := c.QueryParam("key") + fmt.Printf("[Delete Tumblebug child Objects] with Key: %s \n", parentKey) + + err := common.DeleteObjects(parentKey) + if err != nil { + return SendMessage(c, http.StatusOK, "Cannot delete objects") + } + + return SendMessage(c, http.StatusOK, "Objects have been deleted") } \ No newline at end of file diff --git a/src/api/rest/server/server.go b/src/api/rest/server/server.go index b9d7e3110..3ccdec139 100644 --- a/src/api/rest/server/server.go +++ b/src/api/rest/server/server.go @@ -128,8 +128,10 @@ func ApiServer() { e.GET("/tumblebug/config", rest_common.RestGetAllConfig) e.DELETE("/tumblebug/config", rest_common.RestDelAllConfig) - e.GET("/tumblebug/objectList", rest_common.RestGetObjectList) - e.GET("/tumblebug/objectValue", rest_common.RestGetObjectValue) + e.GET("/tumblebug/objects", rest_common.RestGetObjectList) + e.GET("/tumblebug/object", rest_common.RestGetObjectValue) + e.DELETE("/tumblebug/object", rest_common.RestDeleteObject) + e.DELETE("/tumblebug/objects", rest_common.RestDeleteObjects) g := e.Group("/tumblebug/ns", common.NsValidation()) diff --git a/src/core/common/utility.go b/src/core/common/utility.go index 2f40ae4db..e8ab33e56 100644 --- a/src/core/common/utility.go +++ b/src/core/common/utility.go @@ -725,3 +725,27 @@ func GetObjectValue(key string) (string, error) { } return keyValue.Value, nil } + +// func DeleteObject delete the object. +func DeleteObject(key string) error { + + err := CBStore.Delete(key) + if err != nil { + CBLog.Error(err) + return err + } + return nil +} + +// func DeleteObjects delete objects. +func DeleteObjects(key string) error { + keyValue, _ := CBStore.GetList(key, true) + for _, v := range keyValue { + err := CBStore.Delete(v.Key) + if err != nil { + CBLog.Error(err) + return err + } + } + return nil +} diff --git a/src/docs/docs.go b/src/docs/docs.go index b532343e0..20384f468 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -3098,9 +3098,9 @@ var doc = `{ } } }, - "/objectList": { + "/object": { "get": { - "description": "List all objects for a given key", + "description": "Get value of an object", "consumes": [ "application/json" ], @@ -3110,11 +3110,53 @@ var doc = `{ "tags": [ "Admin" ], - "summary": "List all objects for a given key", + "summary": "Get value of an object", "parameters": [ { "type": "string", - "description": "retrieve objects by key", + "description": "get object value by key", + "name": "key", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete value of an object", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "Delete value of an object", + "parameters": [ + { + "type": "string", + "description": "delete object value by key", "name": "key", "in": "query", "required": true @@ -3142,9 +3184,9 @@ var doc = `{ } } }, - "/objectValue": { + "/objects": { "get": { - "description": "Get value of an object", + "description": "List all objects for a given key", "consumes": [ "application/json" ], @@ -3154,11 +3196,53 @@ var doc = `{ "tags": [ "Admin" ], - "summary": "Get value of an object", + "summary": "List all objects for a given key", "parameters": [ { "type": "string", - "description": "get object value by key", + "description": "retrieve objects by key", + "name": "key", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete objects", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "Delete objects", + "parameters": [ + { + "type": "string", + "description": "Delete child objects based on the given key string", "name": "key", "in": "query", "required": true diff --git a/src/docs/swagger.json b/src/docs/swagger.json index a8b755cb5..13a3c519d 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -3083,9 +3083,9 @@ } } }, - "/objectList": { + "/object": { "get": { - "description": "List all objects for a given key", + "description": "Get value of an object", "consumes": [ "application/json" ], @@ -3095,11 +3095,53 @@ "tags": [ "Admin" ], - "summary": "List all objects for a given key", + "summary": "Get value of an object", "parameters": [ { "type": "string", - "description": "retrieve objects by key", + "description": "get object value by key", + "name": "key", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete value of an object", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "Delete value of an object", + "parameters": [ + { + "type": "string", + "description": "delete object value by key", "name": "key", "in": "query", "required": true @@ -3127,9 +3169,9 @@ } } }, - "/objectValue": { + "/objects": { "get": { - "description": "Get value of an object", + "description": "List all objects for a given key", "consumes": [ "application/json" ], @@ -3139,11 +3181,53 @@ "tags": [ "Admin" ], - "summary": "Get value of an object", + "summary": "List all objects for a given key", "parameters": [ { "type": "string", - "description": "get object value by key", + "description": "retrieve objects by key", + "name": "key", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete objects", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "Delete objects", + "parameters": [ + { + "type": "string", + "description": "Delete child objects based on the given key string", "name": "key", "in": "query", "required": true diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index beb899acf..ebb23636d 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -3113,13 +3113,13 @@ paths: summary: Get VNet tags: - VNet - /objectList: - get: + /object: + delete: consumes: - application/json - description: List all objects for a given key + description: Delete value of an object parameters: - - description: retrieve objects by key + - description: delete object value by key in: query name: key required: true @@ -3139,10 +3139,9 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/common.SimpleMsg' - summary: List all objects for a given key + summary: Delete value of an object tags: - Admin - /objectValue: get: consumes: - application/json @@ -3171,6 +3170,63 @@ paths: summary: Get value of an object tags: - Admin + /objects: + delete: + consumes: + - application/json + description: Delete objects + parameters: + - description: Delete child objects based on the given key string + in: query + name: key + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/common.SimpleMsg' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: Delete objects + tags: + - Admin + get: + consumes: + - application/json + description: List all objects for a given key + parameters: + - description: retrieve objects by key + in: query + name: key + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/common.SimpleMsg' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: List all objects for a given key + tags: + - Admin securityDefinitions: BasicAuth: type: basic diff --git a/test/official/sequentialFullTest/delete-object.sh b/test/official/sequentialFullTest/delete-object.sh new file mode 100755 index 000000000..6408d2497 --- /dev/null +++ b/test/official/sequentialFullTest/delete-object.sh @@ -0,0 +1,17 @@ + + FILE=../conf.env + if [ ! -f "$FILE" ]; then + echo "$FILE does not exist." + exit + fi + + source ../conf.env + AUTH="Authorization: Basic $(echo -n $ApiUsername:$ApiPassword | base64)" + + echo "####################################################################" + echo "## 0. Object: Delete" + echo "####################################################################" + + KEY=${1} + + curl -H "${AUTH}" -sX DELETE http://$TumblebugServer/tumblebug/object?key=$KEY | json_pp diff --git a/test/official/sequentialFullTest/delete-objects-becareful.sh b/test/official/sequentialFullTest/delete-objects-becareful.sh new file mode 100755 index 000000000..78b4c0235 --- /dev/null +++ b/test/official/sequentialFullTest/delete-objects-becareful.sh @@ -0,0 +1,17 @@ + + FILE=../conf.env + if [ ! -f "$FILE" ]; then + echo "$FILE does not exist." + exit + fi + + source ../conf.env + AUTH="Authorization: Basic $(echo -n $ApiUsername:$ApiPassword | base64)" + + echo "####################################################################" + echo "## 0. Object: Delete Child Objects" + echo "####################################################################" + + KEY=${1} + + curl -H "${AUTH}" -sX DELETE http://$TumblebugServer/tumblebug/objects?key=$KEY | json_pp diff --git a/test/official/sequentialFullTest/get-object.sh b/test/official/sequentialFullTest/get-object.sh index ffdd6ed4c..2524a58ff 100755 --- a/test/official/sequentialFullTest/get-object.sh +++ b/test/official/sequentialFullTest/get-object.sh @@ -14,4 +14,4 @@ KEY=${1} - curl -H "${AUTH}" -sX GET http://$TumblebugServer/tumblebug/objectValue?key=$KEY | json_pp + curl -H "${AUTH}" -sX GET http://$TumblebugServer/tumblebug/object?key=$KEY | json_pp diff --git a/test/official/sequentialFullTest/list-object.sh b/test/official/sequentialFullTest/list-object.sh index e71f4ffd9..c93d9df9d 100755 --- a/test/official/sequentialFullTest/list-object.sh +++ b/test/official/sequentialFullTest/list-object.sh @@ -14,4 +14,4 @@ KEY=${1} - curl -H "${AUTH}" -sX GET http://$TumblebugServer/tumblebug/objectList?key=$KEY | json_pp + curl -H "${AUTH}" -sX GET http://$TumblebugServer/tumblebug/objects?key=$KEY | json_pp From ffc6b7ca57c39967f0c9f5fce558bbcc2d0b8322 Mon Sep 17 00:00:00 2001 From: seokho-son Date: Wed, 17 Mar 2021 20:44:06 +0900 Subject: [PATCH 2/2] Change function names with comment fix --- src/api/rest/server/common/utility.go | 24 ++++++++++++------------ src/api/rest/server/server.go | 4 ++-- src/docs/docs.go | 8 ++++---- src/docs/swagger.json | 8 ++++---- src/docs/swagger.yaml | 8 ++++---- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index 55362b3e1..181c2b437 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -111,8 +111,8 @@ type ObjectList struct { Object []string `json:"object"` } -// func RestGetObjectList is a rest api wrapper for GetObjectList. -// RestGetObjectList godoc +// func RestGetObjects is a rest api wrapper for GetObjectList. +// RestGetObjects godoc // @Summary List all objects for a given key // @Description List all objects for a given key // @Tags Admin @@ -123,7 +123,7 @@ type ObjectList struct { // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg // @Router /objects [get] -func RestGetObjectList(c echo.Context) error { +func RestGetObjects(c echo.Context) error { parentKey := c.QueryParam("key") fmt.Printf("[Get Tumblebug Object List] with Key: %s \n", parentKey) @@ -137,8 +137,8 @@ func RestGetObjectList(c echo.Context) error { return c.JSON(http.StatusOK, &objectList) } -// func RestGetObjectValue is a rest api wrapper for GetObjectValue. -// RestGetObjectValue godoc +// func RestGetObject is a rest api wrapper for GetObject. +// RestGetObject godoc // @Summary Get value of an object // @Description Get value of an object // @Tags Admin @@ -149,7 +149,7 @@ func RestGetObjectList(c echo.Context) error { // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg // @Router /object [get] -func RestGetObjectValue(c echo.Context) error { +func RestGetObject(c echo.Context) error { parentKey := c.QueryParam("key") fmt.Printf("[Get Tumblebug Object Value] with Key: %s \n", parentKey) @@ -164,10 +164,10 @@ func RestGetObjectValue(c echo.Context) error { return c.JSON(http.StatusOK, &contentJSON) } -// func RestDeleteObjectValue is a rest api wrapper for DeleteObject. -// RestDeleteObjectValue godoc -// @Summary Delete value of an object -// @Description Delete value of an object +// func RestDeleteObject is a rest api wrapper for DeleteObject. +// RestDeleteObject godoc +// @Summary Delete an object +// @Description Delete an object // @Tags Admin // @Accept json // @Produce json @@ -195,8 +195,8 @@ func RestDeleteObject(c echo.Context) error { // func RestDeleteObjects is a rest api wrapper for DeleteObjects. // RestDeleteObjects godoc -// @Summary Delete objects -// @Description Delete objects +// @Summary Delete child objects along with the given object +// @Description Delete child objects along with the given object // @Tags Admin // @Accept json // @Produce json diff --git a/src/api/rest/server/server.go b/src/api/rest/server/server.go index 3ccdec139..eb3a5980e 100644 --- a/src/api/rest/server/server.go +++ b/src/api/rest/server/server.go @@ -128,8 +128,8 @@ func ApiServer() { e.GET("/tumblebug/config", rest_common.RestGetAllConfig) e.DELETE("/tumblebug/config", rest_common.RestDelAllConfig) - e.GET("/tumblebug/objects", rest_common.RestGetObjectList) - e.GET("/tumblebug/object", rest_common.RestGetObjectValue) + e.GET("/tumblebug/object", rest_common.RestGetObject) + e.GET("/tumblebug/objects", rest_common.RestGetObjects) e.DELETE("/tumblebug/object", rest_common.RestDeleteObject) e.DELETE("/tumblebug/objects", rest_common.RestDeleteObjects) diff --git a/src/docs/docs.go b/src/docs/docs.go index 20384f468..ccdeeaa4b 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -3142,7 +3142,7 @@ var doc = `{ } }, "delete": { - "description": "Delete value of an object", + "description": "Delete an object", "consumes": [ "application/json" ], @@ -3152,7 +3152,7 @@ var doc = `{ "tags": [ "Admin" ], - "summary": "Delete value of an object", + "summary": "Delete an object", "parameters": [ { "type": "string", @@ -3228,7 +3228,7 @@ var doc = `{ } }, "delete": { - "description": "Delete objects", + "description": "Delete child objects along with the given object", "consumes": [ "application/json" ], @@ -3238,7 +3238,7 @@ var doc = `{ "tags": [ "Admin" ], - "summary": "Delete objects", + "summary": "Delete child objects along with the given object", "parameters": [ { "type": "string", diff --git a/src/docs/swagger.json b/src/docs/swagger.json index 13a3c519d..07a6b73e6 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -3127,7 +3127,7 @@ } }, "delete": { - "description": "Delete value of an object", + "description": "Delete an object", "consumes": [ "application/json" ], @@ -3137,7 +3137,7 @@ "tags": [ "Admin" ], - "summary": "Delete value of an object", + "summary": "Delete an object", "parameters": [ { "type": "string", @@ -3213,7 +3213,7 @@ } }, "delete": { - "description": "Delete objects", + "description": "Delete child objects along with the given object", "consumes": [ "application/json" ], @@ -3223,7 +3223,7 @@ "tags": [ "Admin" ], - "summary": "Delete objects", + "summary": "Delete child objects along with the given object", "parameters": [ { "type": "string", diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index ebb23636d..fd7ac905f 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -3117,7 +3117,7 @@ paths: delete: consumes: - application/json - description: Delete value of an object + description: Delete an object parameters: - description: delete object value by key in: query @@ -3139,7 +3139,7 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/common.SimpleMsg' - summary: Delete value of an object + summary: Delete an object tags: - Admin get: @@ -3174,7 +3174,7 @@ paths: delete: consumes: - application/json - description: Delete objects + description: Delete child objects along with the given object parameters: - description: Delete child objects based on the given key string in: query @@ -3196,7 +3196,7 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/common.SimpleMsg' - summary: Delete objects + summary: Delete child objects along with the given object tags: - Admin get: