Semaphore/db/sql/inventory.go

76 lines
2.0 KiB
Go
Raw Normal View History

package sql
2024-12-17 17:49:51 +01:00
import (
"github.com/Masterminds/squirrel"
"github.com/semaphoreui/semaphore/db"
)
func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) {
err = d.getObject(projectID, db.InventoryProps, inventoryID, &inventory)
if err != nil {
return
}
err = db.FillInventory(d, &inventory)
return
}
2024-12-17 17:49:51 +01:00
func (d *SqlDb) GetInventories(projectID int, params db.RetrieveQueryParams, types []db.InventoryType) ([]db.Inventory, error) {
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)
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)
}
2022-02-03 08:05:13 +01:00
func (d *SqlDb) DeleteInventory(projectID int, inventoryID int) error {
return d.deleteObject(projectID, db.InventoryProps, inventoryID)
}
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=?",
inventory.Name,
inventory.Type,
inventory.SSHKeyID,
inventory.Inventory,
2021-09-17 12:10:36 +02:00
inventory.BecomeKeyID,
inventory.TemplateID,
2024-05-22 19:10:32 +02:00
inventory.RepositoryID,
inventory.ID)
return err
}
func (d *SqlDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
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 "+
"(?, ?, ?, ?, ?, ?, ?, ?)",
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,
inventory.TemplateID,
inventory.RepositoryID)
if err != nil {
return
}
newInventory = inventory
newInventory.ID = insertID
return
}