Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update connection api and doc #3242

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/en_US/api/restapi/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions docs/zh_CN/api/restapi/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/server/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"io"
"net/http"
"strconv"

"github.com/gorilla/mux"

Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion internal/server/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pkg/connection/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@
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

Check warning on line 176 in pkg/connection/pool.go

View check run for this annotation

Codecov / codecov/patch

pkg/connection/pool.go#L176

Added line #L176 was not covered by tests
}
metaList = append(metaList, meta)
}
return metaList
Expand Down
Loading