Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blueprint): add new api to check connection tokens #8102

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions backend/core/models/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 1 addition & 11 deletions backend/core/models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
24 changes: 24 additions & 0 deletions backend/server/api/blueprints/blueprints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package blueprints

import (
"github.com/spf13/cast"
"net/http"
"strconv"

Expand Down Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions backend/server/api/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,19 @@ 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 {
projectOutputCheck.Exist = false
} 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)
}

Expand Down
1 change: 1 addition & 0 deletions backend/server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions backend/server/services/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
26 changes: 0 additions & 26 deletions backend/server/services/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Loading