You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ly...@apache.org on 2024/09/25 05:22:23 UTC

(incubator-devlake) branch dev-1 updated: feat(blueprint): add new api to check connection tokens

This is an automated email from the ASF dual-hosted git repository.

lynwee pushed a commit to branch dev-1
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/dev-1 by this push:
     new 8b2d024b1 feat(blueprint): add new api to check connection tokens
8b2d024b1 is described below

commit 8b2d024b11ac6ab66730c009512116b2b11d6059
Author: d4x1 <15...@qq.com>
AuthorDate: Wed Sep 25 13:22:14 2024 +0800

    feat(blueprint): add new api to check connection tokens
---
 backend/core/models/blueprint.go            |  9 +++++++++
 backend/core/models/project.go              | 12 +-----------
 backend/server/api/blueprints/blueprints.go | 24 ++++++++++++++++++++++++
 backend/server/api/project/project.go       | 12 ------------
 backend/server/api/router.go                |  1 +
 backend/server/services/blueprint.go        | 24 ++++++++++++++++++++++++
 backend/server/services/project.go          | 26 --------------------------
 7 files changed, 59 insertions(+), 49 deletions(-)

diff --git a/backend/core/models/blueprint.go b/backend/core/models/blueprint.go
index f30adec9f..9dc468822 100644
--- a/backend/core/models/blueprint.go
+++ b/backend/core/models/blueprint.go
@@ -92,3 +92,12 @@ type SyncPolicy struct {
 	TimeAfter  *time.Time `json:"timeAfter"`
 	TriggerSyncPolicy
 }
+
+type ConnectionTokenCheckResult struct {
+	PluginName   string `json:"pluginName" mapstructure:"pluginName"`
+	ConnectionID uint64 `json:"connectionId" mapstructure:"connectionId"`
+	Success      bool   `json:"success" mapstructure:"success"`
+	Message      string `json:"message" mapstructure:"message"`
+}
+
+type ApiBlueprintConnectionTokenCheck []ConnectionTokenCheckResult
diff --git a/backend/core/models/project.go b/backend/core/models/project.go
index 6acafe27f..99c61785d 100644
--- a/backend/core/models/project.go
+++ b/backend/core/models/project.go
@@ -73,19 +73,9 @@ type ApiOutputProject struct {
 }
 
 type ApiProjectCheck struct {
-	Exist  bool                  `json:"exist" mapstructure:"exist"`
-	Tokens *ApiProjectCheckToken `json:"tokens,omitempty" mapstructure:"tokens"`
+	Exist bool `json:"exist" mapstructure:"exist"`
 }
 
-type TokenResultSuccessAndMessage struct {
-	PluginName   string `json:"pluginName" mapstructure:"pluginName"`
-	ConnectionID uint64 `json:"connectionId" mapstructure:"connectionId"`
-	Success      bool   `json:"success" mapstructure:"success"`
-	Message      string `json:"message" mapstructure:"message"`
-}
-
-type ApiProjectCheckToken []TokenResultSuccessAndMessage
-
 type Store struct {
 	StoreKey   string          `gorm:"primaryKey;type:varchar(255)"`
 	StoreValue json.RawMessage `gorm:"type:json;serializer:json"`
diff --git a/backend/server/api/blueprints/blueprints.go b/backend/server/api/blueprints/blueprints.go
index 23383d468..17376d2c8 100644
--- a/backend/server/api/blueprints/blueprints.go
+++ b/backend/server/api/blueprints/blueprints.go
@@ -18,6 +18,7 @@ limitations under the License.
 package blueprints
 
 import (
+	"github.com/spf13/cast"
 	"net/http"
 	"strconv"
 
@@ -226,6 +227,29 @@ func GetBlueprintPipelines(c *gin.Context) {
 	shared.ApiOutputSuccess(c, shared.ResponsePipelines{Pipelines: pipelines, Count: count}, http.StatusOK)
 }
 
+// @Summary get connection token check by blueprint id
+// @Description get connection token check by blueprint id
+// @Tags framework/blueprints
+// @Accept application/json
+// @Param blueprintId path int true "blueprint id"
+// @Success 200  {object} models.ApiBlueprintConnectionTokenCheck
+// @Failure 400  {object} shared.ApiBody "Bad Request"
+// @Failure 500  {object} shared.ApiBody "Internal Error"
+// @Router /blueprints/{blueprintId}/connections-token-check [get]
+func GetBlueprintConnectionTokenCheck(c *gin.Context) {
+	blueprintId, err := cast.ToUint64E(c.Param("blueprintId"))
+	if err != nil {
+		shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad blueprintID format supplied"))
+		return
+	}
+	resp, err := services.CheckBlueprintConnectionTokens(blueprintId)
+	if err != nil {
+		shared.ApiOutputError(c, errors.Default.Wrap(err, "error getting blue print connection token check"))
+		return
+	}
+	shared.ApiOutputSuccess(c, resp, http.StatusOK)
+}
+
 // @Summary delete blueprint by id
 // @Description delete blueprint by id
 // @Tags framework/blueprints
diff --git a/backend/server/api/project/project.go b/backend/server/api/project/project.go
index 43687da73..fd5c14ebb 100644
--- a/backend/server/api/project/project.go
+++ b/backend/server/api/project/project.go
@@ -58,14 +58,12 @@ func GetProject(c *gin.Context) {
 // @Tags framework/projects
 // @Accept application/json
 // @Param projectName path string true "project name"
-// @Param check_token query int false "need to check token validity or not"
 // @Success 200  {object} models.ApiProjectCheck
 // @Failure 400  {string} errcode.Error "Bad Request"
 // @Failure 500  {string} errcode.Error "Internal Error"
 // @Router /projects/{projectName}/check [get]
 func GetProjectCheck(c *gin.Context) {
 	projectName := c.Param("projectName")
-
 	projectOutputCheck := &models.ApiProjectCheck{}
 	_, err := services.GetProject(projectName)
 	if err != nil {
@@ -73,16 +71,6 @@ func GetProjectCheck(c *gin.Context) {
 	} else {
 		projectOutputCheck.Exist = true
 	}
-
-	if c.Query("check_token") == "1" {
-		checkTokenResult, err := services.CheckProjectTokens(projectName)
-		if err != nil {
-			shared.ApiOutputError(c, errors.Default.Wrap(err, "error check project tokens"))
-			return
-		}
-		projectOutputCheck.Tokens = checkTokenResult
-	}
-
 	shared.ApiOutputSuccess(c, projectOutputCheck, http.StatusOK) // //shared.ApiOutputSuccess(c, projectOutputCheck, http.StatusOK)
 }
 
diff --git a/backend/server/api/router.go b/backend/server/api/router.go
index 721517cf5..226eb3035 100644
--- a/backend/server/api/router.go
+++ b/backend/server/api/router.go
@@ -59,6 +59,7 @@ func RegisterRouter(r *gin.Engine, basicRes context.BasicRes) {
 	r.GET("/blueprints/:blueprintId", blueprints.Get)
 	r.POST("/blueprints/:blueprintId/trigger", blueprints.Trigger)
 	r.GET("/blueprints/:blueprintId/pipelines", blueprints.GetBlueprintPipelines)
+	r.GET("/blueprints/:blueprintId/connections-token-check", blueprints.GetBlueprintConnectionTokenCheck)
 
 	r.POST("/tasks/:taskId/rerun", task.PostRerun)
 
diff --git a/backend/server/services/blueprint.go b/backend/server/services/blueprint.go
index ff88211dd..aced9eeea 100644
--- a/backend/server/services/blueprint.go
+++ b/backend/server/services/blueprint.go
@@ -117,6 +117,30 @@ func GetBlueprint(blueprintId uint64, shouldSanitize bool) (*models.Blueprint, e
 	return blueprint, nil
 }
 
+func CheckBlueprintConnectionTokens(blueprintId uint64) (*models.ApiBlueprintConnectionTokenCheck, errors.Error) {
+	blueprint, err := GetBlueprint(blueprintId, false)
+	if err != nil {
+		return nil, err
+	}
+	var ret models.ApiBlueprintConnectionTokenCheck
+	for _, connection := range blueprint.Connections {
+		pluginName := connection.PluginName
+		connectionId := connection.ConnectionId
+		connectionTokenResult := models.ConnectionTokenCheckResult{
+			PluginName:   pluginName,
+			ConnectionID: connectionId,
+			Success:      true,
+			Message:      "success",
+		}
+		if err := checkConnectionToken(logger, *connection); err != nil {
+			connectionTokenResult.Success = false
+			connectionTokenResult.Message = err.Error()
+		}
+		ret = append(ret, connectionTokenResult)
+	}
+	return &ret, nil
+}
+
 // GetBlueprintByProjectName returns the detail of a given ProjectName
 func GetBlueprintByProjectName(projectName string) (*models.Blueprint, errors.Error) {
 	if projectName == "" {
diff --git a/backend/server/services/project.go b/backend/server/services/project.go
index 96afb6bf4..614f10680 100644
--- a/backend/server/services/project.go
+++ b/backend/server/services/project.go
@@ -193,32 +193,6 @@ func GetProject(name string) (*models.ApiOutputProject, errors.Error) {
 	return makeProjectOutput(project, false)
 }
 
-func CheckProjectTokens(name string) (*models.ApiProjectCheckToken, errors.Error) {
-	blueprint, err := GetBlueprintByProjectName(name)
-	if err != nil {
-		return nil, err
-	}
-	var ret models.ApiProjectCheckToken
-	for _, connection := range blueprint.Connections {
-		pluginName := connection.PluginName
-		connectionId := connection.ConnectionId
-		connectionTokenResult := models.TokenResultSuccessAndMessage{
-			PluginName:   pluginName,
-			ConnectionID: connectionId,
-			Success:      true,
-			Message:      "success",
-		}
-		if err := checkConnectionToken(logger, *connection); err != nil {
-			connectionTokenResult.Success = false
-			connectionTokenResult.Message = err.Error()
-
-		}
-		ret = append(ret, connectionTokenResult)
-	}
-
-	return &ret, nil
-}
-
 // PatchProject FIXME ...
 func PatchProject(name string, body map[string]interface{}) (*models.ApiOutputProject, errors.Error) {
 	projectInput := &models.ApiInputProject{}