diff --git a/server/pkg/api/rest/controller/connectionInfo.go b/server/pkg/api/rest/controller/connectionInfo.go index f922724..ff461fc 100644 --- a/server/pkg/api/rest/controller/connectionInfo.go +++ b/server/pkg/api/rest/controller/connectionInfo.go @@ -121,7 +121,7 @@ func checkCreateConnectionInfoReq(sourceGroupID string, createConnectionInfoReq return connectionInfo, nil } -func doGetConnectionInfo(connID string) (*model.ConnectionInfo, error) { +func doGetConnectionInfo(connID string, refresh bool) (*model.ConnectionInfo, error) { connectionInfo, err := dao.ConnectionInfoGet(connID) if err != nil { return nil, err @@ -132,34 +132,36 @@ func doGetConnectionInfo(connID string) (*model.ConnectionInfo, error) { return nil, err } - c := &ssh.SSH{ - Options: ssh.DefaultSSHOptions(), - } + if refresh { + c := &ssh.SSH{ + Options: ssh.DefaultSSHOptions(), + } - err = c.NewClientConn(*connectionInfo) - if err != nil { - oldConnectionInfo.ConnectionStatus = model.ConnectionInfoStatusFailed - oldConnectionInfo.ConnectionFailedMessage = err.Error() - } else { - c.Close() - oldConnectionInfo.ConnectionStatus = model.ConnectionInfoStatusSuccess - oldConnectionInfo.ConnectionFailedMessage = "" - } + err = c.NewClientConn(*connectionInfo) + if err != nil { + oldConnectionInfo.ConnectionStatus = model.ConnectionInfoStatusFailed + oldConnectionInfo.ConnectionFailedMessage = err.Error() + } else { + c.Close() + oldConnectionInfo.ConnectionStatus = model.ConnectionInfoStatusSuccess + oldConnectionInfo.ConnectionFailedMessage = "" + } - err = c.RunAgent(*connectionInfo) - if err != nil { - oldConnectionInfo.AgentStatus = model.ConnectionInfoStatusFailed - oldConnectionInfo.AgentFailedMessage = err.Error() - } else { - c.Close() - oldConnectionInfo.AgentStatus = model.ConnectionInfoStatusSuccess - oldConnectionInfo.AgentFailedMessage = "" - } + err = c.RunAgent(*connectionInfo) + if err != nil { + oldConnectionInfo.AgentStatus = model.ConnectionInfoStatusFailed + oldConnectionInfo.AgentFailedMessage = err.Error() + } else { + c.Close() + oldConnectionInfo.AgentStatus = model.ConnectionInfoStatusSuccess + oldConnectionInfo.AgentFailedMessage = "" + } - err = dao.ConnectionInfoUpdate(oldConnectionInfo) - if err != nil { - return nil, errors.New("Error occurred while updating the connection information. " + - "(ID: " + oldConnectionInfo.ID + ", Error: " + err.Error() + ")") + err = dao.ConnectionInfoUpdate(oldConnectionInfo) + if err != nil { + return nil, errors.New("Error occurred while updating the connection information. " + + "(ID: " + oldConnectionInfo.ID + ", Error: " + err.Error() + ")") + } } connectionInfo, err = encryptSecrets(oldConnectionInfo) @@ -181,7 +183,7 @@ func doCreateConnectionInfo(connectionInfo *model.ConnectionInfo) (*model.Connec return nil, err } - connectionInfo, err = doGetConnectionInfo(connectionInfo.ID) + connectionInfo, err = doGetConnectionInfo(connectionInfo.ID, true) if err != nil { return nil, err } @@ -275,7 +277,7 @@ func GetConnectionInfo(c echo.Context) error { return common.ReturnErrorMsg(c, "Please provide the connId.") } - connectionInfo, err := doGetConnectionInfo(connID) + connectionInfo, err := doGetConnectionInfo(connID, false) if err != nil { return common.ReturnErrorMsg(c, err.Error()) } @@ -302,7 +304,7 @@ func GetConnectionInfoDirectly(c echo.Context) error { return common.ReturnErrorMsg(c, "Please provide the connId.") } - connectionInfo, err := doGetConnectionInfo(connID) + connectionInfo, err := doGetConnectionInfo(connID, false) if err != nil { return common.ReturnErrorMsg(c, err.Error()) } @@ -464,7 +466,7 @@ func UpdateConnectionInfo(c echo.Context) error { return common.ReturnErrorMsg(c, err.Error()) } - connectionInfo, err := doGetConnectionInfo(oldConnectionInfo.ID) + connectionInfo, err := doGetConnectionInfo(oldConnectionInfo.ID, true) if err != nil { return common.ReturnErrorMsg(c, err.Error()) } @@ -514,3 +516,68 @@ func DeleteConnectionInfo(c echo.Context) error { return c.JSONPretty(http.StatusOK, model.SimpleMsg{Message: "success"}, " ") } + +// RefreshConnectionInfoStatus godoc +// +// @ID refresh-connection-info-status +// @Summary Refresh Connection Info Status +// @Description Refresh the connection info status. +// @Tags [On-premise] ConnectionInfo +// @Accept json +// @Produce json +// @Param sgId path string true "ID of the SourceGroup" +// @Param connId path string true "ID of the connectionInfo" +// @Success 200 {object} model.SimpleMsg "Successfully refresh the source group" +// @Failure 400 {object} common.ErrorResponse "Sent bad request." +// @Failure 500 {object} common.ErrorResponse "Failed to refresh the source group" +// @Router /source_group/{sgId}/connection_info/{connId}/refresh [put] +func RefreshConnectionInfoStatus(c echo.Context) error { + sgID := c.Param("sgId") + if sgID == "" { + return common.ReturnErrorMsg(c, "Please provide the sgId.") + } + + _, err := dao.SourceGroupGet(sgID) + if err != nil { + return common.ReturnErrorMsg(c, err.Error()) + } + + connID := c.Param("connId") + if connID == "" { + return common.ReturnErrorMsg(c, "Please provide the connId.") + } + + _, err = doGetConnectionInfo(connID, true) + if err != nil { + return common.ReturnErrorMsg(c, err.Error()) + } + + return c.JSONPretty(http.StatusOK, model.SimpleMsg{Message: "success"}, " ") +} + +// RefreshConnectionInfoStatusDirectly godoc +// +// @ID refresh-connection-info-status-directly +// @Summary Refresh Connection Info Status Directly +// @Description Refresh the connection info status directly. +// @Tags [On-premise] ConnectionInfo +// @Accept json +// @Produce json +// @Param connId path string true "ID of the connectionInfo" +// @Success 200 {object} model.SimpleMsg "Successfully refresh the source group" +// @Failure 400 {object} common.ErrorResponse "Sent bad request." +// @Failure 500 {object} common.ErrorResponse "Failed to refresh the source group" +// @Router /connection_info/{connId}/refresh [put] +func RefreshConnectionInfoStatusDirectly(c echo.Context) error { + connID := c.Param("connId") + if connID == "" { + return common.ReturnErrorMsg(c, "Please provide the connId.") + } + + _, err := doGetConnectionInfo(connID, true) + if err != nil { + return common.ReturnErrorMsg(c, err.Error()) + } + + return c.JSONPretty(http.StatusOK, model.SimpleMsg{Message: "success"}, " ") +} diff --git a/server/pkg/api/rest/controller/sourceGroup.go b/server/pkg/api/rest/controller/sourceGroup.go index a1f04c2..1ba8a96 100644 --- a/server/pkg/api/rest/controller/sourceGroup.go +++ b/server/pkg/api/rest/controller/sourceGroup.go @@ -485,7 +485,7 @@ func RefreshSourceGroupConnectionInfoStatus(c echo.Context) error { wg.Done() }() - _, err := doGetConnectionInfo(connectionInfo.ID) + _, err := doGetConnectionInfo(connectionInfo.ID, true) if err != nil { errMsgLock.Lock() if errMsg != "" { diff --git a/server/pkg/api/rest/docs/docs.go b/server/pkg/api/rest/docs/docs.go index 432dc8b..cecd2d2 100644 --- a/server/pkg/api/rest/docs/docs.go +++ b/server/pkg/api/rest/docs/docs.go @@ -206,6 +206,51 @@ const docTemplate = `{ } } }, + "/connection_info/{connId}/refresh": { + "put": { + "description": "Refresh the connection info status directly.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[On-premise] ConnectionInfo" + ], + "summary": "Refresh Connection Info Status Directly", + "operationId": "refresh-connection-info-status-directly", + "parameters": [ + { + "type": "string", + "description": "ID of the connectionInfo", + "name": "connId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Successfully refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_model.SimpleMsg" + } + }, + "400": { + "description": "Sent bad request.", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + }, + "500": { + "description": "Failed to refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + } + } + } + }, "/readyz": { "get": { "description": "Check Honeybee is ready", @@ -1211,6 +1256,58 @@ const docTemplate = `{ } } }, + "/source_group/{sgId}/connection_info/{connId}/refresh": { + "put": { + "description": "Refresh the connection info status.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[On-premise] ConnectionInfo" + ], + "summary": "Refresh Connection Info Status", + "operationId": "refresh-connection-info-status", + "parameters": [ + { + "type": "string", + "description": "ID of the SourceGroup", + "name": "sgId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "ID of the connectionInfo", + "name": "connId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Successfully refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_model.SimpleMsg" + } + }, + "400": { + "description": "Sent bad request.", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + }, + "500": { + "description": "Failed to refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + } + } + } + }, "/source_group/{sgId}/connection_info/{connId}/software": { "get": { "description": "Get the software information of the connection information.", diff --git a/server/pkg/api/rest/docs/swagger.json b/server/pkg/api/rest/docs/swagger.json index 69e3b0b..edccf7b 100644 --- a/server/pkg/api/rest/docs/swagger.json +++ b/server/pkg/api/rest/docs/swagger.json @@ -199,6 +199,51 @@ } } }, + "/connection_info/{connId}/refresh": { + "put": { + "description": "Refresh the connection info status directly.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[On-premise] ConnectionInfo" + ], + "summary": "Refresh Connection Info Status Directly", + "operationId": "refresh-connection-info-status-directly", + "parameters": [ + { + "type": "string", + "description": "ID of the connectionInfo", + "name": "connId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Successfully refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_model.SimpleMsg" + } + }, + "400": { + "description": "Sent bad request.", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + }, + "500": { + "description": "Failed to refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + } + } + } + }, "/readyz": { "get": { "description": "Check Honeybee is ready", @@ -1204,6 +1249,58 @@ } } }, + "/source_group/{sgId}/connection_info/{connId}/refresh": { + "put": { + "description": "Refresh the connection info status.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[On-premise] ConnectionInfo" + ], + "summary": "Refresh Connection Info Status", + "operationId": "refresh-connection-info-status", + "parameters": [ + { + "type": "string", + "description": "ID of the SourceGroup", + "name": "sgId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "ID of the connectionInfo", + "name": "connId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Successfully refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_model.SimpleMsg" + } + }, + "400": { + "description": "Sent bad request.", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + }, + "500": { + "description": "Failed to refresh the source group", + "schema": { + "$ref": "#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse" + } + } + } + } + }, "/source_group/{sgId}/connection_info/{connId}/software": { "get": { "description": "Get the software information of the connection information.", diff --git a/server/pkg/api/rest/docs/swagger.yaml b/server/pkg/api/rest/docs/swagger.yaml index 02671b5..7235c78 100644 --- a/server/pkg/api/rest/docs/swagger.yaml +++ b/server/pkg/api/rest/docs/swagger.yaml @@ -1456,6 +1456,36 @@ paths: summary: Get ConnectionInfo Directly tags: - '[On-premise] ConnectionInfo' + /connection_info/{connId}/refresh: + put: + consumes: + - application/json + description: Refresh the connection info status directly. + operationId: refresh-connection-info-status-directly + parameters: + - description: ID of the connectionInfo + in: path + name: connId + required: true + type: string + produces: + - application/json + responses: + "200": + description: Successfully refresh the source group + schema: + $ref: '#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_model.SimpleMsg' + "400": + description: Sent bad request. + schema: + $ref: '#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse' + "500": + description: Failed to refresh the source group + schema: + $ref: '#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse' + summary: Refresh Connection Info Status Directly + tags: + - '[On-premise] ConnectionInfo' /readyz: get: consumes: @@ -2131,6 +2161,41 @@ paths: summary: Get Kubernetes Information tags: - '[Get] Get source info' + /source_group/{sgId}/connection_info/{connId}/refresh: + put: + consumes: + - application/json + description: Refresh the connection info status. + operationId: refresh-connection-info-status + parameters: + - description: ID of the SourceGroup + in: path + name: sgId + required: true + type: string + - description: ID of the connectionInfo + in: path + name: connId + required: true + type: string + produces: + - application/json + responses: + "200": + description: Successfully refresh the source group + schema: + $ref: '#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_model.SimpleMsg' + "400": + description: Sent bad request. + schema: + $ref: '#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse' + "500": + description: Failed to refresh the source group + schema: + $ref: '#/definitions/github_com_cloud-barista_cm-honeybee_server_pkg_api_rest_common.ErrorResponse' + summary: Refresh Connection Info Status + tags: + - '[On-premise] ConnectionInfo' /source_group/{sgId}/connection_info/{connId}/software: get: consumes: diff --git a/server/pkg/api/rest/route/connectionInfo.go b/server/pkg/api/rest/route/connectionInfo.go index 58855ea..d550137 100644 --- a/server/pkg/api/rest/route/connectionInfo.go +++ b/server/pkg/api/rest/route/connectionInfo.go @@ -13,6 +13,8 @@ func RegisterConnectionInfo(e *echo.Echo) { e.GET("/"+strings.ToLower(common.ShortModuleName)+"/source_group/:sgId/connection_info", controller.ListConnectionInfo) e.PUT("/"+strings.ToLower(common.ShortModuleName)+"/source_group/:sgId/connection_info/:connId", controller.UpdateConnectionInfo) e.DELETE("/"+strings.ToLower(common.ShortModuleName)+"/source_group/:sgId/connection_info/:connId", controller.DeleteConnectionInfo) + e.PUT("/"+strings.ToLower(common.ShortModuleName)+"/source_group/:sgId/connection_info/:connId/refresh", controller.RefreshConnectionInfoStatus) e.GET("/"+strings.ToLower(common.ShortModuleName)+"/connection_info/:connId", controller.GetConnectionInfoDirectly) + e.PUT("/"+strings.ToLower(common.ShortModuleName)+"/connection_info/:connId/refresh", controller.RefreshConnectionInfoStatusDirectly) }