2021-05-07 12:08:34 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2021-05-08 19:25:00 +02:00
|
|
|
"go.etcd.io/bbolt"
|
2021-05-07 12:08:34 +02:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) {
|
2021-05-08 19:25:00 +02:00
|
|
|
err = d.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(makeBucketId(db.InventoryObject, projectID))
|
2021-05-07 12:08:34 +02:00
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
id := []byte(strconv.Itoa(inventoryID))
|
|
|
|
str := b.Get(id)
|
|
|
|
if str == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal(str, &inventory)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:25:00 +02:00
|
|
|
if inventory.KeyID != nil {
|
|
|
|
inventory.Key, err = d.GetAccessKey(projectID, *inventory.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.SSHKeyID != nil {
|
|
|
|
inventory.SSHKey, err = d.GetAccessKey(projectID, *inventory.SSHKeyID)
|
|
|
|
}
|
2021-05-07 12:08:34 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetInventories(projectID int, params db.RetrieveQueryParams) (inventories []db.Inventory, err error) {
|
2021-05-08 19:25:00 +02:00
|
|
|
err = d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(makeBucketId(db.InventoryObject, projectID))
|
2021-05-07 12:08:34 +02:00
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return inventories, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteInventory(projectID int, inventoryID int) error {
|
2021-05-08 19:25:00 +02:00
|
|
|
return d.db.Update(func (tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(makeBucketId(db.InventoryObject, projectID))
|
2021-05-07 12:08:34 +02:00
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
return b.Delete([]byte(strconv.Itoa(inventoryID)))
|
|
|
|
})
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteInventorySoft(projectID int, inventoryID int) error {
|
|
|
|
return d.DeleteInventory(projectID, inventoryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) UpdateInventory(inventory db.Inventory) error {
|
2021-05-08 19:25:00 +02:00
|
|
|
err := d.db.Update(func(tx *bbolt.Tx) error {
|
2021-05-07 12:08:34 +02:00
|
|
|
b := tx.Bucket([]byte("inventory_" + strconv.Itoa(inventory.ProjectID)))
|
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
id := []byte(strconv.Itoa(inventory.ID))
|
|
|
|
if b.Get(id) == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
str, err2 := json.Marshal(inventory)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Put(id, str)
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
|
2021-05-08 19:25:00 +02:00
|
|
|
err = d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
b, err2 := tx.CreateBucketIfNotExists(makeBucketId(db.InventoryObject, inventory.ProjectID))
|
2021-05-07 12:08:34 +02:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := b.NextSequence()
|
|
|
|
newInventory = inventory
|
|
|
|
newInventory.ID = int(id)
|
|
|
|
str, err2 := json.Marshal(newInventory)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Put([]byte(strconv.Itoa(newInventory.ID)), str)
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|