2024-10-02 17:49:33 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/db"
|
2024-10-02 17:49:33 +02:00
|
|
|
"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
|
2024-10-22 21:50:31 +02:00
|
|
|
|
|
|
|
switch vault.Type {
|
|
|
|
case "password":
|
|
|
|
vault.Script = nil
|
|
|
|
case "script":
|
|
|
|
vault.VaultKeyID = nil
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:35:20 +02:00
|
|
|
_, 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
|
|
|
|
}
|
2024-11-26 07:48:32 +01:00
|
|
|
|
|
|
|
func (d *BoltDb) deleteTemplateVault(projectID int, vaultID int, tx *bbolt.Tx) error {
|
|
|
|
return d.deleteObject(projectID, db.TemplateVaultProps, intObjectID(vaultID), tx)
|
|
|
|
}
|