Skip to content

Commit

Permalink
server: api: rest: Add connection info refresh APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Nov 9, 2024
1 parent 2693219 commit 35b4849
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 31 deletions.
127 changes: 97 additions & 30 deletions server/pkg/api/rest/controller/connectionInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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"}, " ")
}
2 changes: 1 addition & 1 deletion server/pkg/api/rest/controller/sourceGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
97 changes: 97 additions & 0 deletions server/pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.",
Expand Down
Loading

0 comments on commit 35b4849

Please sign in to comment.