Semaphore/db/bolt/integrations.go

302 lines
8.4 KiB
Go
Raw Permalink Normal View History

2023-07-03 01:41:13 +02:00
package bolt
import (
"errors"
"fmt"
2024-03-07 10:32:25 +01:00
"reflect"
2024-11-23 12:02:18 +01:00
"github.com/semaphoreui/semaphore/db"
"go.etcd.io/bbolt"
2023-07-03 01:41:13 +02:00
)
/*
2024-02-11 20:52:14 +01:00
Integrations
2023-07-03 01:41:13 +02:00
*/
2024-02-11 20:52:14 +01:00
func (d *BoltDb) CreateIntegration(integration db.Integration) (db.Integration, error) {
err := integration.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
2024-02-11 20:52:14 +01:00
return db.Integration{}, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
newIntegration, err := d.createObject(integration.ProjectID, db.IntegrationProps, integration)
return newIntegration.(db.Integration), err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *BoltDb) GetIntegrations(projectID int, params db.RetrieveQueryParams) (integrations []db.Integration, err error) {
err = d.getObjects(projectID, db.IntegrationProps, params, nil, &integrations)
return integrations, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *BoltDb) GetIntegration(projectID int, integrationID int) (integration db.Integration, err error) {
err = d.getObject(projectID, db.IntegrationProps, intObjectID(integrationID), &integration)
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
return
}
2024-02-11 20:52:14 +01:00
func (d *BoltDb) UpdateIntegration(integration db.Integration) error {
err := integration.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2024-02-11 20:52:14 +01:00
return d.updateObject(integration.ProjectID, db.IntegrationProps, integration)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *BoltDb) GetIntegrationRefs(projectID int, integrationID int) (db.IntegrationReferrers, error) {
//return d.getObjectRefs(projectID, db.IntegrationProps, integrationID)
return db.IntegrationReferrers{}, nil
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) DeleteIntegrationExtractValue(projectID int, valueID int, integrationID int) error {
2024-03-04 14:39:48 +01:00
return d.deleteObject(projectID, db.IntegrationExtractValueProps, intObjectID(valueID), nil)
2023-07-03 01:41:13 +02:00
}
2024-03-04 12:24:47 +01:00
func (d *BoltDb) CreateIntegrationExtractValue(projectId int, value db.IntegrationExtractValue) (db.IntegrationExtractValue, error) {
2023-07-03 01:41:13 +02:00
err := value.Validate()
if err != nil {
2024-02-11 20:52:14 +01:00
return db.IntegrationExtractValue{}, err
2023-07-03 01:41:13 +02:00
}
2024-03-04 14:39:48 +01:00
newValue, err := d.createObject(projectId, db.IntegrationExtractValueProps, value)
2024-02-11 20:52:14 +01:00
return newValue.(db.IntegrationExtractValue), err
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) GetIntegrationExtractValues(projectID int, params db.RetrieveQueryParams, integrationID int) (values []db.IntegrationExtractValue, err error) {
2024-02-11 20:52:14 +01:00
values = make([]db.IntegrationExtractValue, 0)
2024-01-08 13:56:50 +01:00
2024-06-29 15:14:47 +02:00
err = d.getObjects(projectID, db.IntegrationExtractValueProps, params, func(i interface{}) bool {
v := i.(db.IntegrationExtractValue)
return v.IntegrationID == integrationID
}, &values)
2024-01-08 13:56:50 +01:00
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) GetIntegrationExtractValue(projectID int, valueID int, integrationID int) (value db.IntegrationExtractValue, err error) {
2024-03-04 14:39:48 +01:00
err = d.getObject(projectID, db.IntegrationExtractValueProps, intObjectID(valueID), &value)
2023-07-03 01:41:13 +02:00
return value, err
}
2024-03-04 12:24:47 +01:00
func (d *BoltDb) UpdateIntegrationExtractValue(projectID int, integrationExtractValue db.IntegrationExtractValue) error {
2024-02-11 20:52:14 +01:00
err := integrationExtractValue.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2024-03-04 14:39:48 +01:00
return d.updateObject(projectID, db.IntegrationExtractValueProps, integrationExtractValue)
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) GetIntegrationExtractValueRefs(projectID int, valueID int, integrationID int) (db.IntegrationExtractorChildReferrers, error) {
2024-03-04 14:39:48 +01:00
return d.getIntegrationExtractorChildrenRefs(projectID, db.IntegrationExtractValueProps, valueID)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
2023-07-03 01:41:13 +02:00
/*
2024-02-11 20:52:14 +01:00
Integration Matcher
2023-07-03 01:41:13 +02:00
*/
2024-03-04 12:36:24 +01:00
func (d *BoltDb) CreateIntegrationMatcher(projectID int, matcher db.IntegrationMatcher) (db.IntegrationMatcher, error) {
2023-07-03 01:41:13 +02:00
err := matcher.Validate()
if err != nil {
2024-02-11 20:52:14 +01:00
return db.IntegrationMatcher{}, err
2023-07-03 01:41:13 +02:00
}
2024-03-04 14:39:48 +01:00
newMatcher, err := d.createObject(projectID, db.IntegrationMatcherProps, matcher)
2024-02-11 20:52:14 +01:00
return newMatcher.(db.IntegrationMatcher), err
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) GetIntegrationMatchers(projectID int, params db.RetrieveQueryParams, integrationID int) (matchers []db.IntegrationMatcher, err error) {
2024-02-11 20:52:14 +01:00
matchers = make([]db.IntegrationMatcher, 0)
2023-07-03 01:41:13 +02:00
2024-06-29 15:14:47 +02:00
err = d.getObjects(projectID, db.IntegrationMatcherProps, db.RetrieveQueryParams{}, func(i interface{}) bool {
2024-06-30 19:50:46 +02:00
v := i.(db.IntegrationMatcher)
2024-06-29 15:14:47 +02:00
return v.IntegrationID == integrationID
}, &matchers)
2023-07-03 01:41:13 +02:00
return
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) GetIntegrationMatcher(projectID int, matcherID int, integrationID int) (matcher db.IntegrationMatcher, err error) {
2024-02-11 20:52:14 +01:00
var matchers []db.IntegrationMatcher
2024-03-06 15:51:59 +01:00
matchers, err = d.GetIntegrationMatchers(projectID, db.RetrieveQueryParams{}, integrationID)
2023-07-03 01:41:13 +02:00
for _, v := range matchers {
if v.ID == matcherID {
matcher = v
}
}
return
}
2024-03-04 12:36:24 +01:00
func (d *BoltDb) UpdateIntegrationMatcher(projectID int, integrationMatcher db.IntegrationMatcher) error {
2024-02-11 20:52:14 +01:00
err := integrationMatcher.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2024-03-04 14:39:48 +01:00
return d.updateObject(projectID, db.IntegrationMatcherProps, integrationMatcher)
2023-07-03 01:41:13 +02:00
}
func (d *BoltDb) deleteIntegrationMatcher(projectID int, matcherID int, integrationID int, tx *bbolt.Tx) error {
return d.deleteObject(projectID, db.IntegrationMatcherProps, intObjectID(matcherID), tx)
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) DeleteIntegrationMatcher(projectID int, matcherID int, integrationID int) error {
return d.deleteIntegrationMatcher(projectID, matcherID, integrationID, nil)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *BoltDb) DeleteIntegration(projectID int, integrationID int) error {
return d.deleteIntegration(projectID, integrationID, nil)
}
func (d *BoltDb) deleteIntegration(projectID int, integrationID int, tx *bbolt.Tx) error {
2024-03-06 15:51:59 +01:00
matchers, err := d.GetIntegrationMatchers(projectID, db.RetrieveQueryParams{}, integrationID)
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2024-03-06 15:51:59 +01:00
for m := range matchers {
d.deleteIntegrationMatcher(projectID, matchers[m].ID, integrationID, tx)
2023-07-03 01:41:13 +02:00
}
return d.deleteObject(projectID, db.IntegrationProps, intObjectID(integrationID), tx)
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *BoltDb) GetIntegrationMatcherRefs(projectID int, matcherID int, integrationID int) (db.IntegrationExtractorChildReferrers, error) {
2024-03-04 14:39:48 +01:00
return d.getIntegrationExtractorChildrenRefs(projectID, db.IntegrationMatcherProps, matcherID)
2023-07-03 01:41:13 +02:00
}
2024-03-07 10:32:25 +01:00
var integrationAliasProps = db.ObjectProps{
TableName: "integration_alias",
Type: reflect.TypeOf(db.IntegrationAlias{}),
PrimaryColumnName: "alias",
}
func (d *BoltDb) GetIntegrationAliases(projectID int, integrationID *int) (res []db.IntegrationAlias, err error) {
err = d.getObjects(projectID, db.IntegrationAliasProps, db.RetrieveQueryParams{}, func(i interface{}) bool {
alias := i.(db.IntegrationAlias)
2024-03-21 11:21:21 +01:00
if alias.IntegrationID == nil && integrationID == nil {
return true
} else if alias.IntegrationID != nil && integrationID != nil {
return *alias.IntegrationID == *integrationID
}
return false
}, &res)
2024-03-07 10:32:25 +01:00
return
}
func (d *BoltDb) GetIntegrationsByAlias(alias string) (res []db.Integration, err error) {
2024-03-07 10:32:25 +01:00
var aliasObj db.IntegrationAlias
err = d.getObject(-1, integrationAliasProps, strObjectID(alias), &aliasObj)
if err != nil {
return
}
if aliasObj.IntegrationID == nil {
2024-03-21 11:21:21 +01:00
err = d.getObjects(aliasObj.ProjectID, db.IntegrationProps, db.RetrieveQueryParams{}, func(i interface{}) bool {
integration := i.(db.Integration)
return integration.Searchable
}, &res)
2024-03-21 11:21:21 +01:00
if err != nil {
return
}
} else {
var integration db.Integration
integration, err = d.GetIntegration(aliasObj.ProjectID, *aliasObj.IntegrationID)
if err != nil {
return
}
res = append(res, integration)
}
2024-03-07 10:32:25 +01:00
return
}
func (d *BoltDb) CreateIntegrationAlias(alias db.IntegrationAlias) (res db.IntegrationAlias, err error) {
_, err = d.GetIntegrationsByAlias(alias.Alias)
if err == nil {
err = fmt.Errorf("alias already exists")
}
if !errors.Is(err, db.ErrNotFound) {
return
}
newAlias, err := d.createObject(alias.ProjectID, db.IntegrationAliasProps, alias)
if err != nil {
return
}
res = newAlias.(db.IntegrationAlias)
_, err = d.createObject(-1, integrationAliasProps, alias)
if err != nil {
_ = d.DeleteIntegrationAlias(alias.ProjectID, alias.ID)
return
}
return
}
func (d *BoltDb) DeleteIntegrationAlias(projectID int, aliasID int) (err error) {
var alias db.IntegrationAlias
err = d.getObject(projectID, db.IntegrationAliasProps, intObjectID(aliasID), &alias)
if err != nil {
return
}
err = d.deleteObject(projectID, db.IntegrationAliasProps, intObjectID(aliasID), nil)
if err != nil {
return
}
err = d.deleteObject(-1, integrationAliasProps, strObjectID(alias.Alias), nil)
if err != nil {
return
}
return
2024-03-07 10:32:25 +01:00
}
2024-03-23 13:37:15 +01:00
func (d *BoltDb) GetAllSearchableIntegrations() (integrations []db.Integration, err error) {
integrations = make([]db.Integration, 0)
projects, err := d.GetAllProjects()
if err != nil {
return
}
for _, project := range projects {
var projectIntegrations []db.Integration
projectIntegrations, err = d.GetIntegrations(project.ID, db.RetrieveQueryParams{})
if err != nil {
return
}
2024-11-23 12:02:18 +01:00
integrations = append(integrations, projectIntegrations...)
2024-03-23 13:37:15 +01:00
}
return
}