From a72b798aa799c14d30a6a63d72295a4a43a1abed Mon Sep 17 00:00:00 2001 From: Song Gao Date: Thu, 26 Sep 2024 18:01:54 +0800 Subject: [PATCH] update Signed-off-by: Song Gao --- docs/en_US/api/restapi/connection.md | 15 +++++++++++++++ docs/zh_CN/api/restapi/connection.md | 15 +++++++++++++++ internal/server/connection.go | 4 +++- internal/server/connection_test.go | 2 +- pkg/connection/pool.go | 5 ++++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/docs/en_US/api/restapi/connection.md b/docs/en_US/api/restapi/connection.md index 1dda4e980e..3748bf67a3 100644 --- a/docs/en_US/api/restapi/connection.md +++ b/docs/en_US/api/restapi/connection.md @@ -19,6 +19,21 @@ POST http://localhost:9081/connections } ``` +### Update connection + +To update a connection, provide the connection's id, type, and configuration parameters. Currently, `mqtt`/`nng`/`httppush`/`websocket`/`edgex`/`sql` types of connections are supported. Here we take updating the mqtt connection as an example. If the connection is referenced by a rule, it cannot be updated. + +```shell +PUT http://localhost:9081/connections/connection-1 +{ + "id": "connecton-1", + "typ":"mqtt", + "props": { + "server": "tcp://127.0.0.1:1883" + } +} +``` + ### Get all connection information ```shell diff --git a/docs/zh_CN/api/restapi/connection.md b/docs/zh_CN/api/restapi/connection.md index 2e046f4249..a0f8bc4f40 100644 --- a/docs/zh_CN/api/restapi/connection.md +++ b/docs/zh_CN/api/restapi/connection.md @@ -19,6 +19,21 @@ POST http://localhost:9081/connections } ``` +### 更新连接 + +更新连接要提供连接的 id, 类型和配置参数。目前已经支持了 `mqtt`/`nng`/`httppush`/`websocket`/`edgex`/`sql` 类型的连接,这里以更新 mqtt 连接为例。如果连接被规则引用中,则无法被更新。 + +```shell +PUT http://localhost:9081/connections/connection-1 +{ + "id": "connecton-1", + "typ":"mqtt", + "props": { + "server": "tcp://127.0.0.1:1883" + } +} +``` + ### 获取所有连接信息 ```shell diff --git a/internal/server/connection.go b/internal/server/connection.go index 7faecc8b86..6ded09d817 100644 --- a/internal/server/connection.go +++ b/internal/server/connection.go @@ -18,6 +18,7 @@ import ( "encoding/json" "io" "net/http" + "strconv" "github.com/gorilla/mux" @@ -53,7 +54,8 @@ func connectionsHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) w.Write([]byte("success")) case http.MethodGet: - metaList := connection.GetAllConnectionsMeta() + forceAll, _ := strconv.ParseBool(r.URL.Query().Get("forceAll")) + metaList := connection.GetAllConnectionsMeta(forceAll) resp := make([]*ConnectionResponse, 0) for _, meta := range metaList { resp = append(resp, getConnectionRespByMeta(meta)) diff --git a/internal/server/connection_test.go b/internal/server/connection_test.go index 67af34aad5..b8807279ae 100644 --- a/internal/server/connection_test.go +++ b/internal/server/connection_test.go @@ -46,7 +46,7 @@ func (suite *RestTestSuite) TestGetConnectionStatus() { require.Equal(suite.T(), http.StatusCreated, w.Code) time.Sleep(100 * time.Millisecond) - req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/connections", bytes.NewBufferString("any")) + req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/connections?forceAll=true", bytes.NewBufferString("any")) w = httptest.NewRecorder() suite.r.ServeHTTP(w, req) require.Equal(suite.T(), http.StatusOK, w.Code) diff --git a/pkg/connection/pool.go b/pkg/connection/pool.go index 37ad129d1b..f0a97debe7 100644 --- a/pkg/connection/pool.go +++ b/pkg/connection/pool.go @@ -167,11 +167,14 @@ func createNamedConnection(ctx api.StreamContext, id, typ string, props map[stri return meta.cw, nil } -func GetAllConnectionsMeta() []*Meta { +func GetAllConnectionsMeta(forceAll bool) []*Meta { globalConnectionManager.RLock() defer globalConnectionManager.RUnlock() metaList := make([]*Meta, 0) for _, meta := range globalConnectionManager.connectionPool { + if !meta.Named && !forceAll { + continue + } metaList = append(metaList, meta) } return metaList