2020-12-20 19:00:59 +01:00
|
|
|
package sql
|
|
|
|
|
2024-12-17 17:49:51 +01:00
|
|
|
import (
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/semaphoreui/semaphore/db"
|
|
|
|
)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
2021-05-06 10:32:13 +02:00
|
|
|
func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) {
|
2021-05-13 21:45:54 +02:00
|
|
|
err = d.getObject(projectID, db.InventoryProps, inventoryID, &inventory)
|
2021-05-06 10:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:17:28 +02:00
|
|
|
err = db.FillInventory(d, &inventory)
|
2021-05-06 10:32:13 +02:00
|
|
|
return
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
2024-12-17 17:49:51 +01:00
|
|
|
func (d *SqlDb) GetInventories(projectID int, params db.RetrieveQueryParams, types []db.InventoryType) ([]db.Inventory, error) {
|
2020-12-20 19:00:59 +01:00
|
|
|
var inventories []db.Inventory
|
2024-12-17 17:49:51 +01:00
|
|
|
err := d.getObjects(projectID, db.InventoryProps, params, func(builder squirrel.SelectBuilder) squirrel.SelectBuilder {
|
|
|
|
if len(types) == 0 {
|
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
2024-12-17 23:18:42 +01:00
|
|
|
return builder.Where(squirrel.Eq{"type": types})
|
2024-12-17 17:49:51 +01:00
|
|
|
}, &inventories)
|
2020-12-20 19:00:59 +01:00
|
|
|
return inventories, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func (d *SqlDb) GetInventoryRefs(projectID int, inventoryID int) (db.ObjectReferrers, error) {
|
|
|
|
return d.getObjectRefs(projectID, db.InventoryProps, inventoryID)
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func (d *SqlDb) DeleteInventory(projectID int, inventoryID int) error {
|
|
|
|
return d.deleteObject(projectID, db.InventoryProps, inventoryID)
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) UpdateInventory(inventory db.Inventory) error {
|
2024-03-18 15:33:40 +01:00
|
|
|
|
2021-08-24 17:20:34 +02:00
|
|
|
_, err := d.exec(
|
2024-12-17 23:01:10 +01:00
|
|
|
"update project__inventory set name=?, type=?, ssh_key_id=?, inventory=?, become_key_id=?, template_id=?, repository_id=? where id=?",
|
2020-12-20 19:00:59 +01:00
|
|
|
inventory.Name,
|
|
|
|
inventory.Type,
|
|
|
|
inventory.SSHKeyID,
|
|
|
|
inventory.Inventory,
|
2021-09-17 12:10:36 +02:00
|
|
|
inventory.BecomeKeyID,
|
2024-12-17 16:13:30 +01:00
|
|
|
inventory.TemplateID,
|
2024-05-22 19:10:32 +02:00
|
|
|
inventory.RepositoryID,
|
|
|
|
inventory.ID)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
|
2021-08-24 19:52:35 +02:00
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2024-12-17 23:01:10 +01:00
|
|
|
"insert into project__inventory (project_id, name, type, ssh_key_id, inventory, become_key_id, template_id, repository_id) values "+
|
2024-05-22 19:00:28 +02:00
|
|
|
"(?, ?, ?, ?, ?, ?, ?, ?)",
|
2020-12-20 19:00:59 +01:00
|
|
|
inventory.ProjectID,
|
|
|
|
inventory.Name,
|
|
|
|
inventory.Type,
|
|
|
|
inventory.SSHKeyID,
|
2021-09-17 12:10:36 +02:00
|
|
|
inventory.Inventory,
|
2024-03-10 13:13:44 +01:00
|
|
|
inventory.BecomeKeyID,
|
2024-12-17 16:13:30 +01:00
|
|
|
inventory.TemplateID,
|
2024-05-22 19:00:28 +02:00
|
|
|
inventory.RepositoryID)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newInventory = inventory
|
2021-08-24 19:52:35 +02:00
|
|
|
newInventory.ID = insertID
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|