Skip to content
Open
68 changes: 42 additions & 26 deletions agent/api/agent_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ var (
agentService = agentservice.AgentService{}
)

// @Summary join the specified agent
// @Description join the specified agent
// @Tags agent
// @Accept application/json
// @Produce application/json
// @Param X-OCS-Header header string true "Authorization"
// @Param body body param.JoinApiParam true "agent info with zone name"
// @Success 200 object http.OcsAgentResponse{data=task.DagDetailDTO}
// @Failure 400 object http.OcsAgentResponse
// @Failure 500 object http.OcsAgentResponse
// @Router /api/v1/agent [post]
// @Router /api/v1/agent/join [post]
// @Summary join the specified agent
// @Description join the specified agent
// @Tags agent
// @Accept application/json
// @Produce application/json
// @Param X-OCS-Header header string true "Authorization"
// @Param body body param.JoinApiParam true "agent info with zone name"
// @Success 200 object http.OcsAgentResponse{data=task.DagDetailDTO}
// @Failure 400 object http.OcsAgentResponse
// @Failure 500 object http.OcsAgentResponse
// @Router /api/v1/agent [post]
// @Router /api/v1/agent/join [post]
func agentJoinHandler(c *gin.Context) {
var param param.JoinApiParam
if err := c.BindJSON(&param); err != nil {
common.SendResponse(c, nil, err)
return
}
if !meta.OCS_AGENT.IsSingleAgent() {
common.SendResponse(c, nil, errors.Occurf(errors.ErrBadRequest, "%s:%d is not single agent", meta.OCS_AGENT.GetIp(), meta.OCS_AGENT.GetPort()))
common.SendResponse(c, nil, errors.Occurf(errors.ErrBadRequest, "%s is not single agent", meta.OCS_AGENT.String()))
return
}

Expand Down Expand Up @@ -86,7 +86,13 @@ func agentJoinHandler(c *gin.Context) {
param.AgentInfo.String(), agentStatus.OBVersion, meta.OCS_AGENT.String(), obVersion))
return
}
dag, err = agent.CreateJoinMasterDag(param.AgentInfo, param.ZoneName)
// send token to master early.
if err = agent.SendTokenToMaster(param.AgentInfo, param.MasterPassword); err != nil {
common.SendResponse(c, nil, errors.Occur(errors.ErrTaskCreateFailed, err))
return
}

dag, err = agent.CreateJoinMasterDag(param.AgentInfo, param.ZoneName, param.MasterPassword)
}

if err != nil {
Expand All @@ -96,18 +102,18 @@ func agentJoinHandler(c *gin.Context) {
common.SendResponse(c, task.NewDagDetailDTO(dag), nil)
}

// @Summary remove the specified agent
// @Description remove the specified agent
// @Tags agent
// @Accept application/json
// @Produce application/json
// @Param X-OCS-Header header string true "Authorization"
// @Param body body meta.AgentInfo true "agent info"
// @Success 200 object http.OcsAgentResponse{data=task.DagDetailDTO}
// @Failure 400 object http.OcsAgentResponse
// @Failure 500 object http.OcsAgentResponse
// @Router /api/v1/agent [delete]
// @Router /api/v1/agent/remove [post]
// @Summary remove the specified agent
// @Description remove the specified agent
// @Tags agent
// @Accept application/json
// @Produce application/json
// @Param X-OCS-Header header string true "Authorization"
// @Param body body meta.AgentInfo true "agent info"
// @Success 200 object http.OcsAgentResponse{data=task.DagDetailDTO}
// @Failure 400 object http.OcsAgentResponse
// @Failure 500 object http.OcsAgentResponse
// @Router /api/v1/agent [delete]
// @Router /api/v1/agent/remove [post]
func agentRemoveHandler(c *gin.Context) {
var param meta.AgentInfo
if err := c.BindJSON(&param); err != nil {
Expand Down Expand Up @@ -178,3 +184,13 @@ func agentRemoveHandler(c *gin.Context) {
}
common.SendResponse(c, task.NewDagDetailDTO(dag), nil)
}

func agentSetPasswordHandler(c *gin.Context) {
var param param.SetAgentPasswordParam
if err := c.BindJSON(&param); err != nil {
common.SendResponse(c, nil, err)
return
}

common.SendResponse(c, nil, agentService.SetAgentPassword(param.Password))
}
7 changes: 7 additions & 0 deletions agent/api/agent_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func InitOcsAgentRoutes(s *http2.State, r *gin.Engine, isLocalRoute bool) {
constant.URI_API_V1+constant.URI_TENANT_GROUP+constant.URI_PATH_PARAM_NAME+constant.URI_BACKUP+constant.URI_CONFIG,
constant.URI_API_V1+constant.URI_TENANT_GROUP+constant.URI_RESTORE,
constant.URI_API_V1+constant.URI_RESTORE+constant.URI_WINDOWS,
constant.URI_API_V1+constant.URI_INIT,
constant.URI_TASK_RPC_PREFIX+constant.URI_SUB_TASK,
constant.URI_API_V1+constant.URI_TENANT_GROUP,
constant.URI_TENANT_API_PREFIX+constant.URI_PATH_PARAM_NAME+constant.URI_ROOTPASSWORD,
constant.URI_TENANT_API_PREFIX+constant.URI_PATH_PARAM_NAME+constant.URI_USER,
constant.URI_TENANT_API_PREFIX+constant.URI_PATH_PARAM_NAME,
constant.URI_OBPROXY_API_PREFIX,
constant.URI_TENANT_API_PREFIX+constant.URI_PATH_PARAM_NAME+constant.URI_VARIABLES,
),
common.SetContentType,
)
Expand Down Expand Up @@ -107,6 +112,7 @@ func InitOcsAgentRoutes(s *http2.State, r *gin.Engine, isLocalRoute bool) {
InitTenantRoutes(v1, isLocalRoute)
InitBackupRoutes(v1, isLocalRoute)
InitRestoreRoutes(v1, isLocalRoute)
InitObproxyRoutes(v1, isLocalRoute)

// ob routes
ob.POST(constant.URI_INIT, obInitHandler)
Expand All @@ -126,6 +132,7 @@ func InitOcsAgentRoutes(s *http2.State, r *gin.Engine, isLocalRoute bool) {
agent.POST(constant.URI_REMOVE, agentRemoveHandler)
agent.POST(constant.URI_UPGRADE, agentUpgradeHandler)
agent.POST(constant.URI_UPGRADE+constant.URI_CHECK, agentUpgradeCheckHandler)
agent.POST(constant.URI_PASSWORD, agentSetPasswordHandler)

// agents routes
agents.GET(constant.URI_STATUS, GetAllAgentStatus(s))
Expand Down
8 changes: 4 additions & 4 deletions agent/api/common/forward_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func autoForward(c *gin.Context) {
agentService := agentservice.AgentService{}
master := agentService.GetMasterAgentInfo()
if master == nil {
SendResponse(c, nil, errors.Occur(errors.ErrUnauthorized))
SendResponse(c, nil, errors.Occur(errors.ErrUnauthorized, "master agent not found"))
return
}

Expand All @@ -62,13 +62,13 @@ func autoForward(c *gin.Context) {

headerByte, exist := c.Get(constant.OCS_HEADER)
if headerByte == nil || !exist {
SendResponse(c, nil, errors.Occur(errors.ErrUnauthorized))
SendResponse(c, nil, errors.Occur(errors.ErrUnauthorized, "header not found"))
return
}

header, ok := headerByte.(secure.HttpHeader)
if !ok {
SendResponse(c, nil, errors.Occur(errors.ErrUnauthorized))
SendResponse(c, nil, errors.Occur(errors.ErrUnauthorized, "header type error"))
return
}

Expand Down Expand Up @@ -104,7 +104,7 @@ func sendRequsetForForward(c *gin.Context, ctx context.Context, agentInfo meta.A
}
request.SetBody(body)

uri := fmt.Sprintf("%s://%s:%d%s", global.Protocol, agentInfo.GetIp(), agentInfo.GetPort(), c.Request.URL)
uri := fmt.Sprintf("%s://%s%s", global.Protocol, agentInfo.String(), c.Request.URL)
response, err := request.Execute(c.Request.Method, uri)
if err != nil {
log.WithError(err).Errorf("API response failed : [%v %v, client=%v, agent=%v]", c.Request.Method, c.Request.URL, c.ClientIP(), agentInfo.String())
Expand Down
Loading