2020-12-20 19:00:59 +01:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
|
2021-11-02 20:30:45 +01:00
|
|
|
err = template.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2021-09-09 18:50:02 +02:00
|
|
|
"insert into project__template (project_id, inventory_id, repository_id, environment_id, " +
|
2021-10-12 13:37:51 +02:00
|
|
|
"alias, playbook, arguments, override_args, description, vault_key_id, `type`, start_version," +
|
2021-10-27 14:15:36 +02:00
|
|
|
"build_template_id, view_id)" +
|
|
|
|
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
2020-12-20 19:00:59 +01:00
|
|
|
template.ProjectID,
|
|
|
|
template.InventoryID,
|
|
|
|
template.RepositoryID,
|
|
|
|
template.EnvironmentID,
|
|
|
|
template.Alias,
|
|
|
|
template.Playbook,
|
|
|
|
template.Arguments,
|
2021-09-09 18:50:02 +02:00
|
|
|
template.OverrideArguments,
|
2021-09-17 00:14:35 +02:00
|
|
|
template.Description,
|
2021-10-12 13:37:51 +02:00
|
|
|
template.VaultKeyID,
|
|
|
|
template.Type,
|
|
|
|
template.StartVersion,
|
2021-10-27 14:15:36 +02:00
|
|
|
template.BuildTemplateID,
|
|
|
|
template.ViewID)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:05:10 +02:00
|
|
|
err = db.FillTemplate(d, &newTemplate)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
newTemplate = template
|
2021-08-24 19:52:35 +02:00
|
|
|
newTemplate.ID = insertID
|
2021-09-06 13:05:10 +02:00
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) UpdateTemplate(template db.Template) error {
|
2021-11-02 20:30:45 +01:00
|
|
|
err := template.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.exec("update project__template set " +
|
2021-10-12 13:37:51 +02:00
|
|
|
"inventory_id=?, " +
|
|
|
|
"repository_id=?, " +
|
|
|
|
"environment_id=?, " +
|
|
|
|
"alias=?, " +
|
|
|
|
"playbook=?, " +
|
|
|
|
"arguments=?, " +
|
|
|
|
"override_args=?, " +
|
|
|
|
"description=?, " +
|
|
|
|
"vault_key_id=?, " +
|
|
|
|
"`type`=?, " +
|
|
|
|
"start_version=?," +
|
2021-10-27 18:22:52 +02:00
|
|
|
"build_template_id=?, " +
|
2021-10-27 14:15:36 +02:00
|
|
|
"view_id=? " +
|
2021-09-09 18:50:02 +02:00
|
|
|
"where removed = false and id=? and project_id=?",
|
2020-12-20 19:00:59 +01:00
|
|
|
template.InventoryID,
|
|
|
|
template.RepositoryID,
|
|
|
|
template.EnvironmentID,
|
|
|
|
template.Alias,
|
|
|
|
template.Playbook,
|
|
|
|
template.Arguments,
|
|
|
|
template.OverrideArguments,
|
2021-09-09 18:50:02 +02:00
|
|
|
template.Description,
|
2021-09-17 00:14:35 +02:00
|
|
|
template.VaultKeyID,
|
2021-10-12 13:37:51 +02:00
|
|
|
template.Type,
|
|
|
|
template.StartVersion,
|
|
|
|
template.BuildTemplateID,
|
2021-10-27 14:15:36 +02:00
|
|
|
template.ViewID,
|
2021-09-06 13:05:10 +02:00
|
|
|
template.ID,
|
2021-10-12 13:37:51 +02:00
|
|
|
template.ProjectID,
|
|
|
|
)
|
2020-12-20 19:00:59 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-10-27 21:48:51 +02:00
|
|
|
func (d *SqlDb) getTemplates(projectID int, viewID *int, params db.RetrieveQueryParams) (templates []db.Template, err error) {
|
2020-12-20 19:00:59 +01:00
|
|
|
q := squirrel.Select("pt.id",
|
|
|
|
"pt.project_id",
|
|
|
|
"pt.inventory_id",
|
|
|
|
"pt.repository_id",
|
|
|
|
"pt.environment_id",
|
|
|
|
"pt.alias",
|
|
|
|
"pt.playbook",
|
|
|
|
"pt.arguments",
|
2021-09-17 00:14:35 +02:00
|
|
|
"pt.override_args",
|
2021-10-12 13:37:51 +02:00
|
|
|
"pt.vault_key_id",
|
2021-10-27 21:48:51 +02:00
|
|
|
"pt.view_id",
|
2021-10-12 13:37:51 +02:00
|
|
|
"pt.`type`").
|
2021-08-30 21:42:11 +02:00
|
|
|
From("project__template pt").
|
|
|
|
Where("pt.removed = false")
|
2020-12-20 19:00:59 +01:00
|
|
|
|
2021-10-27 21:48:51 +02:00
|
|
|
if viewID != nil {
|
2021-10-28 12:04:50 +02:00
|
|
|
q = q.Where("pt.view_id=?", *viewID)
|
2021-10-27 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
order := "ASC"
|
|
|
|
if params.SortInverted {
|
|
|
|
order = "DESC"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch params.SortBy {
|
|
|
|
case "alias", "playbook":
|
|
|
|
q = q.Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pt." + params.SortBy + " " + order)
|
|
|
|
case "inventory":
|
|
|
|
q = q.LeftJoin("project__inventory pi ON (pt.inventory_id = pi.id)").
|
|
|
|
Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pi.name " + order)
|
|
|
|
case "environment":
|
|
|
|
q = q.LeftJoin("project__environment pe ON (pt.environment_id = pe.id)").
|
|
|
|
Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pe.name " + order)
|
|
|
|
case "repository":
|
|
|
|
q = q.LeftJoin("project__repository pr ON (pt.repository_id = pr.id)").
|
|
|
|
Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pr.name " + order)
|
|
|
|
default:
|
|
|
|
q = q.Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pt.alias " + order)
|
|
|
|
}
|
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
_, err = d.selectAll(&templates, query, args...)
|
2021-10-12 15:59:16 +02:00
|
|
|
|
2021-10-13 20:51:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-13 16:07:22 +02:00
|
|
|
err = db.FillTemplates(d, templates)
|
2021-10-12 15:59:16 +02:00
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-27 21:48:51 +02:00
|
|
|
func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) ( []db.Template, error) {
|
|
|
|
return d.getTemplates(projectID, nil, params)
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:17:28 +02:00
|
|
|
func (d *SqlDb) GetTemplate(projectID int, templateID int) (template db.Template, err error) {
|
|
|
|
err = d.selectOne(
|
2020-12-20 19:00:59 +01:00
|
|
|
&template,
|
2021-08-30 21:42:11 +02:00
|
|
|
"select * from project__template where project_id=? and id=? and removed = false",
|
2020-12-20 19:00:59 +01:00
|
|
|
projectID,
|
|
|
|
templateID)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:17:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.FillTemplate(d, &template)
|
|
|
|
return
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) DeleteTemplate(projectID int, templateID int) error {
|
2021-08-30 21:42:11 +02:00
|
|
|
_, err := d.exec("update project__template set removed=true where project_id=? and id=?", projectID, templateID)
|
|
|
|
|
|
|
|
return err
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|