From 96d8d51a7d205ce10e18543fe5cd8605478c108f Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Mon, 15 Aug 2022 16:03:08 +0300 Subject: [PATCH] Add cluster endpoint for listing the nodes --- docs/docs.go | 64 +++++++++++++++++++++++++++++-------- docs/swagger.json | 64 +++++++++++++++++++++++++++++-------- docs/swagger.yaml | 46 +++++++++++++++++++------- http/handler/api/cluster.go | 29 +++++++++++++++++ http/server.go | 1 + 5 files changed, 167 insertions(+), 37 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index f3c2e1f8..ebd011f6 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -209,6 +209,38 @@ const docTemplate = `{ } } }, + "/api/v3/cluster": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "List of nodes in the cluster", + "produces": [ + "application/json" + ], + "summary": "List of nodes in the cluster", + "operationId": "cluster-3-get-cluster", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/api.ClusterNode" + } + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/api/v3/cluster/node": { "post": { "security": [ @@ -2142,7 +2174,10 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/api.SRTChannels" + "type": "array", + "items": { + "$ref": "#/definitions/api.SRTChannel" + } } } } @@ -2509,6 +2544,12 @@ const docTemplate = `{ "address": { "type": "string" }, + "id": { + "type": "string" + }, + "last_update": { + "type": "integer" + }, "state": { "type": "string" } @@ -3755,7 +3796,7 @@ const docTemplate = `{ } } }, - "api.SRTChannels": { + "api.SRTChannel": { "type": "object", "properties": { "connections": { @@ -3773,19 +3814,16 @@ const docTemplate = `{ } } }, - "publisher": { - "type": "object", - "additionalProperties": { - "type": "integer" - } + "name": { + "type": "string" + }, + "socketid": { + "type": "integer" }, "subscriber": { - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "type": "integer" - } + "type": "array", + "items": { + "type": "integer" } } } diff --git a/docs/swagger.json b/docs/swagger.json index bacdacfc..f969770c 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -201,6 +201,38 @@ } } }, + "/api/v3/cluster": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "List of nodes in the cluster", + "produces": [ + "application/json" + ], + "summary": "List of nodes in the cluster", + "operationId": "cluster-3-get-cluster", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/api.ClusterNode" + } + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/api.Error" + } + } + } + } + }, "/api/v3/cluster/node": { "post": { "security": [ @@ -2134,7 +2166,10 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/api.SRTChannels" + "type": "array", + "items": { + "$ref": "#/definitions/api.SRTChannel" + } } } } @@ -2501,6 +2536,12 @@ "address": { "type": "string" }, + "id": { + "type": "string" + }, + "last_update": { + "type": "integer" + }, "state": { "type": "string" } @@ -3747,7 +3788,7 @@ } } }, - "api.SRTChannels": { + "api.SRTChannel": { "type": "object", "properties": { "connections": { @@ -3765,19 +3806,16 @@ } } }, - "publisher": { - "type": "object", - "additionalProperties": { - "type": "integer" - } + "name": { + "type": "string" + }, + "socketid": { + "type": "integer" }, "subscriber": { - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "type": "integer" - } + "type": "array", + "items": { + "type": "integer" } } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index aef51779..bb5e1bea 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -60,6 +60,10 @@ definitions: properties: address: type: string + id: + type: string + last_update: + type: integer state: type: string type: object @@ -879,7 +883,7 @@ definitions: name: type: string type: object - api.SRTChannels: + api.SRTChannel: properties: connections: additionalProperties: @@ -891,16 +895,14 @@ definitions: $ref: '#/definitions/api.SRTLog' type: array type: object - publisher: - additionalProperties: - type: integer - type: object + name: + type: string + socketid: + type: integer subscriber: - additionalProperties: - items: - type: integer - type: array - type: object + items: + type: integer + type: array type: object api.SRTConnection: properties: @@ -1867,6 +1869,26 @@ paths: schema: type: string summary: Swagger UI for this API + /api/v3/cluster: + get: + description: List of nodes in the cluster + operationId: cluster-3-get-cluster + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/api.ClusterNode' + type: array + "404": + description: Not Found + schema: + $ref: '#/definitions/api.Error' + security: + - ApiKeyAuth: [] + summary: List of nodes in the cluster /api/v3/cluster/node: post: consumes: @@ -3131,7 +3153,9 @@ paths: description: OK schema: items: - $ref: '#/definitions/api.SRTChannels' + items: + $ref: '#/definitions/api.SRTChannel' + type: array type: array security: - ApiKeyAuth: [] diff --git a/http/handler/api/cluster.go b/http/handler/api/cluster.go index 8f17941e..e595f06a 100644 --- a/http/handler/api/cluster.go +++ b/http/handler/api/cluster.go @@ -22,6 +22,35 @@ func NewCluster(cluster cluster.Cluster) *ClusterHandler { } } +// GetCluster returns the list of nodes in the cluster +// @Summary List of nodes in the cluster +// @Description List of nodes in the cluster +// @ID cluster-3-get-cluster +// @Produce json +// @Success 200 {array} api.ClusterNode +// @Failure 404 {object} api.Error +// @Security ApiKeyAuth +// @Router /api/v3/cluster [get] +func (h *ClusterHandler) GetCluster(c echo.Context) error { + nodes := h.cluster.ListNodes() + + list := []api.ClusterNode{} + + for _, node := range nodes { + state := node.State() + n := api.ClusterNode{ + Address: node.Address(), + ID: state.ID, + LastUpdate: state.LastUpdate.Unix(), + State: state.State, + } + + list = append(list, n) + } + + return c.JSON(http.StatusOK, list) +} + // AddNode adds a new node // @Summary Add a new node // @Description Add a new node to the cluster diff --git a/http/server.go b/http/server.go index 332302d2..2f4fec9a 100644 --- a/http/server.go +++ b/http/server.go @@ -659,6 +659,7 @@ func (s *server) setRoutesV3(v3 *echo.Group) { // v3 Cluster if s.v3handler.cluster != nil { + v3.GET("/cluster", s.v3handler.cluster.GetCluster) v3.GET("/cluster/node/:id", s.v3handler.cluster.GetNode) v3.GET("/cluster/node/:id/proxy", s.v3handler.cluster.GetNodeProxy)