2024-10-02 17:49:33 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *BoltDb) GetTemplateVaults(projectID int, templateID int) (vaults []db.TemplateVault, err error) {
|
|
|
|
err = d.getObjects(projectID, db.TemplateVaultProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
|
|
|
|
return referringObj.(db.TemplateVault).TemplateID == templateID
|
|
|
|
}, &vaults)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-10-07 11:35:20 +02:00
|
|
|
for i := range vaults {
|
|
|
|
err = db.FillTemplateVault(d, projectID, &vaults[i])
|
2024-10-02 17:49:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:41:36 +02:00
|
|
|
func (d *BoltDb) CreateTemplateVault(vault db.TemplateVault) (newVault db.TemplateVault, err error) {
|
|
|
|
var newTpl interface{}
|
|
|
|
newTpl, err = d.createObject(vault.ProjectID, db.TemplateVaultProps, vault)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newVault = newTpl.(db.TemplateVault)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:49:33 +02:00
|
|
|
func (d *BoltDb) UpdateTemplateVaults(projectID int, templateID int, vaults []db.TemplateVault) (err error) {
|
|
|
|
if vaults == nil {
|
|
|
|
vaults = []db.TemplateVault{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldVaults []db.TemplateVault
|
|
|
|
oldVaults, err = d.GetTemplateVaults(projectID, templateID)
|
|
|
|
|
2024-10-07 11:35:20 +02:00
|
|
|
err = d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
for _, vault := range oldVaults {
|
|
|
|
err = d.deleteObject(projectID, db.TemplateVaultProps, intObjectID(vault.ID), tx)
|
2024-10-02 17:49:33 +02:00
|
|
|
if err != nil {
|
2024-10-07 11:35:20 +02:00
|
|
|
return err
|
2024-10-02 17:49:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:35:20 +02:00
|
|
|
for _, vault := range vaults {
|
|
|
|
vault.ProjectID = projectID
|
|
|
|
vault.TemplateID = templateID
|
|
|
|
_, err = d.createObjectTx(tx, projectID, db.TemplateVaultProps, vault)
|
2024-10-02 17:49:33 +02:00
|
|
|
if err != nil {
|
2024-10-07 11:35:20 +02:00
|
|
|
return err
|
2024-10-02 17:49:33 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-07 11:35:20 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2024-10-02 17:49:33 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|