Browse Source

feat(tool):Can 【tools/kubernetes】support update rule ? #650 (#659)

Co-authored-by: EMqmyd <mayuedong@emx.io>
EMQmyd 4 years ago
parent
commit
624bfaf834

+ 25 - 1
tools/kubernetes/README-CN.md

@@ -193,7 +193,31 @@ commandDir: "./sample/" //命令文件夹路径
 }
 ```
 
-### 3.8 显示规则 rule1 的状态
+### 3.8 替换并重启规则 rule1
+
+```json
+{
+    "commands":[
+        {
+            "url":"/rules/rule1",
+            "description":"update rule1",
+            "method":"put",
+            "data":{
+                "id":"rule1",
+                "sql":"SELECT * FROM stream1",
+                "actions":[
+                    {
+                        "log":{
+                        }
+                    }]
+            }
+        }]
+}
+```
+
+
+
+### 3.9 显示规则 rule1 的状态
 
 ```json
 {

+ 23 - 1
tools/kubernetes/README.md

@@ -195,7 +195,29 @@ Execute the command of `go build -o tools/kubernetes/kuiper-kubernetes-tool tool
 }
 ```
 
-### 3.8 Show the status of rule1
+### 3.8 Replace and restart rules rule1
+
+```json
+{
+    "commands":[
+        {
+            "url":"/rules/rule1",
+            "description":"update rule1",
+            "method":"put",
+            "data":{
+                "id":"rule1",
+                "sql":"SELECT * FROM stream1",
+                "actions":[
+                    {
+                        "log":{
+                        }
+                    }]
+            }
+        }]
+}
+```
+
+### 3.9 Show the status of rule1
 
 ```json
 {

+ 9 - 0
tools/kubernetes/common/conf.go

@@ -176,6 +176,15 @@ func Post(inHead, inBody string) (data []byte, err error) {
 	return fetchContents(request)
 }
 
+func Put(inHead, inBody string) (data []byte, err error) {
+	request, err := http.NewRequest(http.MethodPut, inHead, bytes.NewBuffer([]byte(inBody)))
+	if nil != err {
+		return nil, err
+	}
+	request.Header.Set("Content-Type", "application/json")
+	return fetchContents(request)
+}
+
 func Delete(inUrl string) (data []byte, err error) {
 	request, err := http.NewRequest(http.MethodDelete, inUrl, nil)
 	if nil != err {

+ 14 - 0
tools/kubernetes/sample/sample.json

@@ -46,6 +46,20 @@
             }
         },
         {
+            "url":"/rules/rule1",
+            "description":"update rule1",
+            "method":"put",
+            "data":{
+                "id":"rule1",
+                "sql":"SELECT id FROM stream1",
+                "actions":[
+                    {
+                        "log":{
+                        }
+                    }]
+            }
+        },
+        {
             "url":"/rules",
             "description":"create rule2",
             "method":"post",

+ 3 - 0
tools/kubernetes/util/util.go

@@ -44,6 +44,9 @@ func (this *command) call(host string) bool {
 	case "delete", "DELETE":
 		resp, err = common.Delete(head)
 		break
+	case "put", "PUT":
+		resp, err = common.Put(head, string(body))
+		break
 	default:
 		this.strLog = fmt.Sprintf("no such method : %s", this.Method)
 		return false

+ 3 - 2
tools/kubernetes/util/util_test.go

@@ -28,10 +28,11 @@ func TestCall(t *testing.T) {
 		},
 		{
 			cmd: command{
-				Url:    `/streams`,
+				Url:    `/streams/stream1`,
 				Method: `put`,
+				Data:   struct{ sql string }{sql: `create stream stream1 (id bigint, name string) WITH ( datasource = \"topic/temperature\", FORMAT = \"json\", KEY = \"id\");`},
 			},
-			exp: false,
+			exp: true,
 		},
 		{
 			cmd: command{