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

fix(api): delete application deployment strategies #3148

Merged
merged 1 commit into from
Aug 6, 2018
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
2 changes: 1 addition & 1 deletion engine/api/application/application_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func DeleteDeploymentStrategy(db gorp.SqlExecutor, projID, appID, pfID int64) er
SELECT project_platform.id
FROM project_platform
WHERE project_platform.project_id = $2
AND project_platform.platform_model_id = $3
AND project_platform.id = $3
)`

_, err := db.Exec(query, appID, projID, pfID)
Expand Down
15 changes: 5 additions & 10 deletions engine/api/application_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"strings"

"github.com/gorilla/mux"

Expand Down Expand Up @@ -145,25 +144,21 @@ func (api *API) deleteApplicationDeploymentStrategyConfigHandler() Handler {
return sdk.WrapError(err, "deleteApplicationDeploymentStrategyConfigHandler> unable to load application")
}

ws, err := workflow.LoadAllByPlatformName(tx, proj.ID, pfName)
isUsed, err := workflow.IsDeploymentPlatformUsed(tx, proj.ID, app.ID, pfName)
if err != nil {
return sdk.WrapError(err, "deleteApplicationDeploymentStrategyConfigHandler> unable to load workflows")
return sdk.WrapError(err, "deleteApplicationDeploymentStrategyConfigHandler> unable to check if platform is used")
}

if len(ws) > 0 {
var wNames = make([]string, len(ws))
for i, w := range ws {
wNames[i] = w.Name
}
return sdk.NewError(sdk.ErrForbidden, fmt.Errorf("platform used by %s", strings.Join(wNames, ",")))
if isUsed {
return sdk.NewError(sdk.ErrForbidden, fmt.Errorf("platform is still used in a workflow"))
}

if _, has := app.DeploymentStrategies[pfName]; !has {
return sdk.WrapError(sdk.ErrNotFound, "deleteApplicationDeploymentStrategyConfigHandler> unable to find strategy")
}

delete(app.DeploymentStrategies, pfName)
if err := application.DeleteDeploymentStrategy(tx, proj.ID, app.ID, pf.PlatformModelID); err != nil {
if err := application.DeleteDeploymentStrategy(tx, proj.ID, app.ID, pf.ID); err != nil {
return sdk.WrapError(err, "deleteApplicationDeploymentStrategyConfigHandler")
}

Expand Down
36 changes: 12 additions & 24 deletions engine/api/workflow/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,33 +1092,21 @@ func UpdateFavorite(db gorp.SqlExecutor, workflowID int64, u *sdk.User, add bool
return sdk.WrapError(err, "UpdateFavorite>")
}

// LoadAllByPlatformName loads all workflow using a platform (project_platform table)
func LoadAllByPlatformName(db gorp.SqlExecutor, projID int64, pfName string) ([]sdk.Workflow, error) {
// IsDeploymentPlatformUsed checks if a deployment platform is used on any workflow
func IsDeploymentPlatformUsed(db gorp.SqlExecutor, projectID int64, appID int64, pfName string) (bool, error) {
query := `
SELECT workflow.*
FROM workflow
JOIN workflow_node ON workflow.id = workflow_node.workflow_id
JOIN workflow_node_context ON workflow_node.id = workflow_node_context.workflow_node_id
JOIN project_platform ON project_platform.project_id = workflow.project_id
WHERE workflow.project_id = $1
AND project_platform.name = $2
AND workflow_node_context.project_platform_id = project_platform.id
SELECT count(1)
FROM workflow_node_context
JOIN project_platform ON project_platform.id = workflow_node_context.project_platform_id
WHERE workflow_node_context.application_id = $2
AND project_platform.project_id = $1
AND project_platform.name = $3
`

res := []sdk.Workflow{}
dbRes := []Workflow{}
if _, err := db.Select(&dbRes, query, projID, pfName); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, sdk.WrapError(err, "LoadAllByPlatformName> Unable to load workflows")
nb, err := db.SelectInt(query, projectID, appID, pfName)
if err != nil {
return false, sdk.WrapError(err, "IsDeploymentPlatformUsed")
}

for _, w := range dbRes {
if err := w.PostGet(db); err != nil {
return nil, sdk.WrapError(err, "LoadAllByPlatformName> Unable to execute post get")
}
res = append(res, sdk.Workflow(w))
}
return res, nil
return nb > 0, nil
}