Skip to content

Commit 4133fa4

Browse files
authored
Merge pull request #3044 from actiontech/pipeline_token
Pipeline token
2 parents cc07c10 + 26b6a51 commit 4133fa4

File tree

7 files changed

+205
-3
lines changed

7 files changed

+205
-3
lines changed

sqle/api/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config *config.SqleOpti
285285
v1ProjectOpRouter.POST("/:project_name/pipelines", v1.CreatePipeline)
286286
v1ProjectOpRouter.DELETE("/:project_name/pipelines/:pipeline_id/", v1.DeletePipeline)
287287
v1ProjectOpRouter.PATCH("/:project_name/pipelines/:pipeline_id/", v1.UpdatePipeline)
288+
v1ProjectOpRouter.PATCH("/:project_name/pipelines/:pipeline_id/token/:node_id/", v1.RefreshPipelineToken)
288289

289290
// database_compare
290291
v1ProjectOpRouter.POST("/:project_name/database_comparison/execute_comparison", v1.ExecuteDatabaseComparison)

sqle/api/controller/v1/pipeline.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ func (req UpdatePipelineReqV1) convertToSvcPipeline(projectUID string, pipelineI
320320
// @Success 200 {object} controller.BaseRes
321321
// @router /v1/projects/{project_name}/pipelines/{pipeline_id}/ [patch]
322322
func UpdatePipeline(c echo.Context) error {
323-
324323
req := new(UpdatePipelineReqV1)
325324
if err := controller.BindAndValidateReq(c, req); err != nil {
326325
return controller.JSONBaseErrorReq(c, err)
@@ -386,3 +385,30 @@ func DeletePipeline(c echo.Context) error {
386385

387386
return c.JSON(http.StatusOK, controller.NewBaseReq(nil))
388387
}
388+
389+
// @Summary 刷新流水线节点token
390+
// @Description refresh pipeline token
391+
// @Id refreshPipelineNodeTokenV1
392+
// @Tags pipeline
393+
// @Security ApiKeyAuth
394+
// @Param project_name path string true "project name"
395+
// @Param pipeline_id path string true "pipeline id"
396+
// @Param node_id path string true "node id"
397+
// @Success 200 {object} controller.BaseRes
398+
// @router /v1/projects/{project_name}/pipelines/{pipeline_id}/token/{node_id}/ [patch]
399+
func RefreshPipelineToken(c echo.Context) error {
400+
pipelineID, err := strconv.Atoi(c.Param("pipeline_id"))
401+
if err != nil {
402+
return controller.JSONBaseErrorReq(c, err)
403+
}
404+
nodeID, err := strconv.Atoi(c.Param("node_id"))
405+
if err != nil {
406+
return controller.JSONBaseErrorReq(c, err)
407+
}
408+
var pipelineSvc pipeline.PipelineSvc
409+
err = pipelineSvc.RefreshPipelineToken(uint(pipelineID), uint(nodeID), controller.GetUserID(c))
410+
if err != nil {
411+
return controller.JSONBaseErrorReq(c, err)
412+
}
413+
return c.JSON(http.StatusOK, controller.NewBaseReq(nil))
414+
}

sqle/docs/docs.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,6 +4420,52 @@ var doc = `{
44204420
}
44214421
}
44224422
},
4423+
"/v1/projects/{project_name}/pipelines/{pipeline_id}/token/{node_id}/": {
4424+
"patch": {
4425+
"security": [
4426+
{
4427+
"ApiKeyAuth": []
4428+
}
4429+
],
4430+
"description": "refresh pipeline token",
4431+
"tags": [
4432+
"pipeline"
4433+
],
4434+
"summary": "刷新流水线节点token",
4435+
"operationId": "refreshPipelineNodeTokenV1",
4436+
"parameters": [
4437+
{
4438+
"type": "string",
4439+
"description": "project name",
4440+
"name": "project_name",
4441+
"in": "path",
4442+
"required": true
4443+
},
4444+
{
4445+
"type": "string",
4446+
"description": "pipeline id",
4447+
"name": "pipeline_id",
4448+
"in": "path",
4449+
"required": true
4450+
},
4451+
{
4452+
"type": "string",
4453+
"description": "node id",
4454+
"name": "node_id",
4455+
"in": "path",
4456+
"required": true
4457+
}
4458+
],
4459+
"responses": {
4460+
"200": {
4461+
"description": "OK",
4462+
"schema": {
4463+
"$ref": "#/definitions/controller.BaseRes"
4464+
}
4465+
}
4466+
}
4467+
}
4468+
},
44234469
"/v1/projects/{project_name}/report_push_configs": {
44244470
"get": {
44254471
"security": [
@@ -16992,6 +17038,10 @@ var doc = `{
1699217038
"last_collection_time": {
1699317039
"type": "string"
1699417040
},
17041+
"token_exp": {
17042+
"type": "integer",
17043+
"example": 1747129752
17044+
},
1699517045
"total_sql_nums": {
1699617046
"type": "integer"
1699717047
},
@@ -18224,6 +18274,9 @@ var doc = `{
1822418274
"type": {
1822518275
"type": "string",
1822618276
"example": "全局配置"
18277+
},
18278+
"version": {
18279+
"type": "integer"
1822718280
}
1822818281
}
1822918282
},

sqle/docs/swagger.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,6 +4404,52 @@
44044404
}
44054405
}
44064406
},
4407+
"/v1/projects/{project_name}/pipelines/{pipeline_id}/token/{node_id}/": {
4408+
"patch": {
4409+
"security": [
4410+
{
4411+
"ApiKeyAuth": []
4412+
}
4413+
],
4414+
"description": "refresh pipeline token",
4415+
"tags": [
4416+
"pipeline"
4417+
],
4418+
"summary": "刷新流水线节点token",
4419+
"operationId": "refreshPipelineNodeTokenV1",
4420+
"parameters": [
4421+
{
4422+
"type": "string",
4423+
"description": "project name",
4424+
"name": "project_name",
4425+
"in": "path",
4426+
"required": true
4427+
},
4428+
{
4429+
"type": "string",
4430+
"description": "pipeline id",
4431+
"name": "pipeline_id",
4432+
"in": "path",
4433+
"required": true
4434+
},
4435+
{
4436+
"type": "string",
4437+
"description": "node id",
4438+
"name": "node_id",
4439+
"in": "path",
4440+
"required": true
4441+
}
4442+
],
4443+
"responses": {
4444+
"200": {
4445+
"description": "OK",
4446+
"schema": {
4447+
"$ref": "#/definitions/controller.BaseRes"
4448+
}
4449+
}
4450+
}
4451+
}
4452+
},
44074453
"/v1/projects/{project_name}/report_push_configs": {
44084454
"get": {
44094455
"security": [
@@ -16976,6 +17022,10 @@
1697617022
"last_collection_time": {
1697717023
"type": "string"
1697817024
},
17025+
"token_exp": {
17026+
"type": "integer",
17027+
"example": 1747129752
17028+
},
1697917029
"total_sql_nums": {
1698017030
"type": "integer"
1698117031
},
@@ -18208,6 +18258,9 @@
1820818258
"type": {
1820918259
"type": "string",
1821018260
"example": "全局配置"
18261+
},
18262+
"version": {
18263+
"type": "integer"
1821118264
}
1821218265
}
1821318266
},

sqle/docs/swagger.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,9 @@ definitions:
32213221
type: string
32223222
last_collection_time:
32233223
type: string
3224+
token_exp:
3225+
example: 1747129752
3226+
type: integer
32243227
total_sql_nums:
32253228
type: integer
32263229
unsolved_sql_nums:
@@ -4061,6 +4064,8 @@ definitions:
40614064
type:
40624065
example: 全局配置
40634066
type: string
4067+
version:
4068+
type: integer
40644069
type: object
40654070
v1.RuleRespV1:
40664071
properties:
@@ -9676,6 +9681,36 @@ paths:
96769681
summary: 更新流水线
96779682
tags:
96789683
- pipeline
9684+
/v1/projects/{project_name}/pipelines/{pipeline_id}/token/{node_id}/:
9685+
patch:
9686+
description: refresh pipeline token
9687+
operationId: refreshPipelineNodeTokenV1
9688+
parameters:
9689+
- description: project name
9690+
in: path
9691+
name: project_name
9692+
required: true
9693+
type: string
9694+
- description: pipeline id
9695+
in: path
9696+
name: pipeline_id
9697+
required: true
9698+
type: string
9699+
- description: node id
9700+
in: path
9701+
name: node_id
9702+
required: true
9703+
type: string
9704+
responses:
9705+
"200":
9706+
description: OK
9707+
schema:
9708+
$ref: '#/definitions/controller.BaseRes'
9709+
security:
9710+
- ApiKeyAuth: []
9711+
summary: 刷新流水线节点token
9712+
tags:
9713+
- pipeline
96799714
/v1/projects/{project_name}/report_push_configs:
96809715
get:
96819716
description: Get report push config list

sqle/model/pipline.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ func (s *Storage) GetPipelineDetail(projectID ProjectUID, pipelineID uint) (*Pip
131131
return pipeline, nil
132132
}
133133

134+
func (s *Storage) GetPipelineNode(pipelineID uint, nodeID uint) (*PipelineNode, error) {
135+
var node PipelineNode
136+
err := s.db.Model(PipelineNode{}).Where("pipeline_id = ? AND id = ?", pipelineID, nodeID).First(&node).Error
137+
if err != nil {
138+
return nil, errors.New(errors.ConnectStorageError, err)
139+
}
140+
return &node, nil
141+
}
142+
134143
func (s *Storage) GetPipelineNodes(pipelineID uint) ([]*PipelineNode, error) {
135144
var nodes []*PipelineNode
136145
err := s.db.Model(PipelineNode{}).Where("pipeline_id = ?", pipelineID).Find(&nodes).Error
@@ -203,3 +212,13 @@ func (s *Storage) UpdatePipeline(pipe *Pipeline, newNodes []*PipelineNode) error
203212
return nil
204213
})
205214
}
215+
216+
func (s *Storage) UpdatePipelineNode(newNode *PipelineNode) error {
217+
return s.Tx(func(txDB *gorm.DB) error {
218+
// 3 更新节点属性
219+
if err := txDB.Save(newNode).Error; err != nil {
220+
return fmt.Errorf("failed to update pipeline node: %w", err)
221+
}
222+
return nil
223+
})
224+
}

sqle/server/pipeline/pipeline.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package pipeline
33
import (
44
"context"
55
"fmt"
6-
"github.com/actiontech/sqle/sqle/errors"
76
"net"
87
"net/url"
98
"time"
109

10+
"github.com/actiontech/sqle/sqle/errors"
11+
1112
dmsCommonJwt "github.com/actiontech/dms/pkg/dms-common/api/jwt"
1213
"github.com/actiontech/sqle/sqle/api/controller"
1314
scannerCmd "github.com/actiontech/sqle/sqle/cmd/scannerd/command"
@@ -54,7 +55,7 @@ func (node PipelineNode) IntegrationInfo(ctx context.Context, projectName string
5455

5556
switch model.PipelineNodeType(node.NodeType) {
5657
case model.NodeTypeAudit:
57-
var cmdUsage = locale.Bundle.LocalizeMsgByCtx(ctx, locale.PipelineCmdUsage)
58+
cmdUsage := locale.Bundle.LocalizeMsgByCtx(ctx, locale.PipelineCmdUsage)
5859

5960
var cmd string
6061
var cmdType string
@@ -427,3 +428,17 @@ func (svc PipelineSvc) DeletePipeline(projectUID string, pipelineID uint) error
427428
s := model.GetStorage()
428429
return s.DeletePipeline(model.ProjectUID(projectUID), pipelineID)
429430
}
431+
432+
func (svc PipelineSvc) RefreshPipelineToken(pipelineID uint, nodeID uint, userID string) error {
433+
s := model.GetStorage()
434+
modelPiplineNode, err := s.GetPipelineNode(pipelineID, nodeID)
435+
if err != nil {
436+
return err
437+
}
438+
newToken, err := svc.newToken(userID, modelPiplineNode.NodeVersion, modelPiplineNode.UUID)
439+
if err != nil {
440+
return err
441+
}
442+
modelPiplineNode.Token = newToken
443+
return s.UpdatePipelineNode(modelPiplineNode)
444+
}

0 commit comments

Comments
 (0)