Semaphore/db/sql/integration.go

419 lines
12 KiB
Go
Raw Normal View History

2023-07-03 01:41:13 +02:00
package sql
import (
2024-03-03 11:57:39 +01:00
"github.com/Masterminds/squirrel"
"github.com/ansible-semaphore/semaphore/db"
"strings"
// fmt"
2024-03-03 11:57:39 +01:00
log "github.com/sirupsen/logrus"
2023-07-03 01:41:13 +02:00
)
2024-02-11 20:52:14 +01:00
func (d *SqlDb) CreateIntegration(integration db.Integration) (newIntegration db.Integration, err error) {
err = integration.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
insertID, err := d.insert(
"id",
2024-02-12 10:50:01 +01:00
"insert into project__integration "+
"(project_id, name, template_id, auth_method, auth_secret_id, auth_header) values "+
2024-03-03 16:39:18 +01:00
"(?, ?, ?, ?, ?, ?)",
2024-02-11 20:52:14 +01:00
integration.ProjectID,
integration.Name,
2024-02-12 10:50:01 +01:00
integration.TemplateID,
integration.AuthMethod,
integration.AuthSecretID,
integration.AuthHeader)
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
newIntegration = integration
newIntegration.ID = insertID
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrations(projectID int, params db.RetrieveQueryParams) (integrations []db.Integration, err error) {
2024-02-11 21:22:35 +01:00
err = d.getProjectObjects(projectID, db.IntegrationProps, params, &integrations)
2024-02-11 20:52:14 +01:00
return integrations, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetAllIntegrations() (integrations []db.Integration, err error) {
var integrationObjects interface{}
integrationObjects, err = d.GetAllObjects(db.IntegrationProps)
integrations = integrationObjects.([]db.Integration)
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegration(projectID int, integrationID int) (integration db.Integration, err error) {
err = d.getObject(projectID, db.IntegrationProps, integrationID, &integration)
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationRefs(projectID int, integrationID int) (referrers db.IntegrationReferrers, err error) {
2023-07-03 01:41:13 +02:00
var extractorReferrer []db.ObjectReferrer
2024-02-11 20:52:14 +01:00
extractorReferrer, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationExtractorProps, integrationID)
referrers = db.IntegrationReferrers{
IntegrationExtractors: extractorReferrer,
2023-07-03 01:41:13 +02:00
}
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) DeleteIntegration(projectID int, integrationID int) error {
extractors, err := d.GetIntegrationExtractors(integrationID, db.RetrieveQueryParams{})
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2023-07-03 01:41:13 +02:00
for extractor := range extractors {
2024-02-11 20:52:14 +01:00
d.DeleteIntegrationExtractor(integrationID, extractors[extractor].ID)
}
2024-02-11 20:52:14 +01:00
return d.deleteObject(projectID, db.IntegrationProps, integrationID)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) UpdateIntegration(integration db.Integration) error {
err := integration.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2023-07-03 01:41:13 +02:00
_, err = d.exec(
2024-02-12 10:50:01 +01:00
"update project__integration set name=?, template_id=?, auth_method=?, auth_secret_id=?, auth_header=? where id=?",
2024-02-11 20:52:14 +01:00
integration.Name,
integration.TemplateID,
2024-02-12 10:50:01 +01:00
integration.ID,
integration.AuthMethod,
integration.AuthSecretID,
integration.AuthHeader)
2023-07-03 01:41:13 +02:00
return err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) CreateIntegrationExtractor(integrationExtractor db.IntegrationExtractor) (newIntegrationExtractor db.IntegrationExtractor, err error) {
err = integrationExtractor.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
insertID, err := d.insert(
"id",
2024-02-11 20:52:14 +01:00
"insert into project__integration_extractor (name, integration_id) values (?, ?)",
integrationExtractor.Name,
integrationExtractor.IntegrationID)
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
newIntegrationExtractor = integrationExtractor
newIntegrationExtractor.ID = insertID
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 23:39:57 +01:00
func (d *SqlDb) GetIntegrationExtractor(integrationID int, extractorID int) (extractor db.IntegrationExtractor, err error) {
query, args, err := squirrel.Select("e.*").
2024-02-11 20:52:14 +01:00
From("project__integration_extractor as e").
Where(squirrel.And{
2024-02-11 20:52:14 +01:00
squirrel.Eq{"integration_id": integrationID},
squirrel.Eq{"id": extractorID},
}).
OrderBy("e.name").
ToSql()
if err != nil {
return
}
err = d.selectOne(&extractor, query, args...)
return extractor, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationExtractors(integrationID int, params db.RetrieveQueryParams) ([]db.IntegrationExtractor, error) {
var extractors []db.IntegrationExtractor
2024-02-11 23:27:03 +01:00
err := d.getObjectsByReferrer(integrationID, db.IntegrationExtractorProps, db.IntegrationExtractorProps, params, &extractors)
2023-07-03 01:41:13 +02:00
return extractors, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationExtractorRefs(integrationID int, extractorID int) (refs db.IntegrationExtractorReferrers, err error) {
refs.IntegrationMatchers, err = d.GetObjectReferences(db.IntegrationMatcherProps, db.IntegrationExtractorProps, extractorID)
refs.IntegrationExtractValues, err = d.GetObjectReferences(db.IntegrationExtractValueProps, db.IntegrationExtractorProps, extractorID)
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationExtractValuesByExtractorID(extractorID int) (values []db.IntegrationExtractValue, err error) {
var sqlError error
query, args, sqlError := squirrel.Select("v.*").
2024-02-11 20:52:14 +01:00
From("project__integration_extract_value as v").
Where(squirrel.Eq{"extractor_id": extractorID}).
OrderBy("v.id").
ToSql()
if sqlError != nil {
2024-02-11 20:52:14 +01:00
return []db.IntegrationExtractValue{}, sqlError
}
2023-07-03 01:41:13 +02:00
err = d.selectOne(&values, query, args...)
2023-07-03 01:41:13 +02:00
return values, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationMatchersByExtractorID(extractorID int) (matchers []db.IntegrationMatcher, err error) {
var sqlError error
query, args, sqlError := squirrel.Select("m.*").
2024-02-11 20:52:14 +01:00
From("project__integration_matcher as m").
Where(squirrel.Eq{"extractor_id": extractorID}).
OrderBy("m.id").
ToSql()
if sqlError != nil {
2024-02-11 20:52:14 +01:00
return []db.IntegrationMatcher{}, sqlError
}
2023-07-03 01:41:13 +02:00
err = d.selectOne(&matchers, query, args...)
2023-07-03 01:41:13 +02:00
return matchers, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) DeleteIntegrationExtractor(integrationID int, extractorID int) error {
values, err := d.GetIntegrationExtractValuesByExtractorID(extractorID)
if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
return err
}
for value := range values {
2024-02-11 20:52:14 +01:00
err = d.DeleteIntegrationExtractValue(extractorID, values[value].ID)
if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
log.Error(err)
return err
}
}
2024-02-11 20:52:14 +01:00
matchers, errExtractor := d.GetIntegrationMatchersByExtractorID(extractorID)
if errExtractor != nil && !strings.Contains(errExtractor.Error(), "no rows in result set") {
log.Error(errExtractor)
return errExtractor
}
for matcher := range matchers {
2024-02-11 20:52:14 +01:00
err = d.DeleteIntegrationMatcher(extractorID, matchers[matcher].ID)
if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
log.Error(err)
return err
}
}
2024-02-11 20:52:14 +01:00
return d.deleteObjectByReferencedID(integrationID, db.IntegrationProps, db.IntegrationExtractorProps, extractorID)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) UpdateIntegrationExtractor(integrationExtractor db.IntegrationExtractor) error {
err := integrationExtractor.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return err
}
2023-07-03 01:41:13 +02:00
_, err = d.exec(
2024-02-11 20:52:14 +01:00
"update project__integration_extractor set name=? where id=?",
integrationExtractor.Name,
integrationExtractor.ID)
2023-07-03 01:41:13 +02:00
return err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) CreateIntegrationExtractValue(value db.IntegrationExtractValue) (newValue db.IntegrationExtractValue, err error) {
err = value.Validate()
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
insertID, err := d.insert("id",
2024-02-11 23:39:57 +01:00
"insert into project__integration_extract_value "+
"(value_source, body_data_type, `key`, `variable`, `name`, extractor_id) values "+
"(?, ?, ?, ?, ?, ?)",
value.ValueSource,
value.BodyDataType,
value.Key,
value.Variable,
value.Name,
value.ExtractorID)
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
newValue = value
newValue.ID = insertID
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationExtractValues(extractorID int, params db.RetrieveQueryParams) ([]db.IntegrationExtractValue, error) {
var values []db.IntegrationExtractValue
2024-02-11 23:39:57 +01:00
err := d.getObjectsByReferrer(extractorID, db.IntegrationExtractValueProps, db.IntegrationExtractValueProps, params, &values)
return values, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetAllIntegrationExtractValues() (values []db.IntegrationExtractValue, err error) {
var valueObjects interface{}
2024-02-11 20:52:14 +01:00
valueObjects, err = d.GetAllObjects(db.IntegrationExtractValueProps)
values = valueObjects.([]db.IntegrationExtractValue)
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 23:39:57 +01:00
func (d *SqlDb) GetIntegrationExtractValue(extractorID int, valueID int) (value db.IntegrationExtractValue, err error) {
query, args, err := squirrel.Select("v.*").
2024-02-11 20:52:14 +01:00
From("project__integration_extract_value as v").
Where(squirrel.Eq{"id": valueID}).
OrderBy("v.id").
ToSql()
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
err = d.selectOne(&value, query, args...)
2023-07-03 01:41:13 +02:00
return value, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationExtractValueRefs(extractorID int, valueID int) (refs db.IntegrationExtractorChildReferrers, err error) {
refs.IntegrationExtractors, err = d.GetObjectReferences(db.IntegrationExtractorProps, db.IntegrationExtractValueProps, extractorID)
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) DeleteIntegrationExtractValue(extractorID int, valueID int) error {
return d.deleteObjectByReferencedID(extractorID, db.IntegrationExtractorProps, db.IntegrationExtractValueProps, valueID)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) UpdateIntegrationExtractValue(integrationExtractValue db.IntegrationExtractValue) error {
err := integrationExtractValue.Validate()
if err != nil {
return err
}
_, err = d.exec(
2024-03-03 16:39:18 +01:00
"update project__integration_extract_value set value_source=?, body_data_type=?, `key`=?, `variable`=?, `name`=? where `id`=?",
2024-02-11 20:52:14 +01:00
integrationExtractValue.ValueSource,
integrationExtractValue.BodyDataType,
integrationExtractValue.Key,
integrationExtractValue.Variable,
integrationExtractValue.Name,
integrationExtractValue.ID)
return err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) CreateIntegrationMatcher(matcher db.IntegrationMatcher) (newMatcher db.IntegrationMatcher, err error) {
err = matcher.Validate()
if err != nil {
return
}
insertID, err := d.insert(
"id",
2024-02-11 23:27:03 +01:00
"insert into project__integration_matcher "+
"(match_type, `method`, body_data_type, `key`, `value`, extractor_id, `name`) values "+
"(?, ?, ?, ?, ?, ?, ?)",
matcher.MatchType,
matcher.Method,
matcher.BodyDataType,
matcher.Key,
matcher.Value,
matcher.ExtractorID,
matcher.Name)
if err != nil {
return
}
newMatcher = matcher
newMatcher.ID = insertID
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationMatchers(extractorID int, params db.RetrieveQueryParams) (matchers []db.IntegrationMatcher, err error) {
query, args, err := squirrel.Select("m.*").
2024-02-11 20:52:14 +01:00
From("project__integration_matcher as m").
Where(squirrel.Eq{"extractor_id": extractorID}).
OrderBy("m.id").
ToSql()
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
_, err = d.selectAll(&matchers, query, args...)
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetAllIntegrationMatchers() (matchers []db.IntegrationMatcher, err error) {
var matcherObjects interface{}
2024-02-11 20:52:14 +01:00
matcherObjects, err = d.GetAllObjects(db.IntegrationMatcherProps)
matchers = matcherObjects.([]db.IntegrationMatcher)
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 23:39:57 +01:00
func (d *SqlDb) GetIntegrationMatcher(extractorID int, matcherID int) (matcher db.IntegrationMatcher, err error) {
query, args, err := squirrel.Select("m.*").
2024-02-11 20:52:14 +01:00
From("project__integration_matcher as m").
Where(squirrel.Eq{"id": matcherID}).
OrderBy("m.id").
ToSql()
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
err = d.selectOne(&matcher, query, args...)
2023-07-03 01:41:13 +02:00
return matcher, err
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) GetIntegrationMatcherRefs(extractorID int, matcherID int) (refs db.IntegrationExtractorChildReferrers, err error) {
refs.IntegrationExtractors, err = d.GetObjectReferences(db.IntegrationExtractorProps, db.IntegrationMatcherProps, matcherID)
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) DeleteIntegrationMatcher(extractorID int, matcherID int) error {
return d.deleteObjectByReferencedID(extractorID, db.IntegrationExtractorProps, db.IntegrationMatcherProps, matcherID)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func (d *SqlDb) UpdateIntegrationMatcher(integrationMatcher db.IntegrationMatcher) error {
err := integrationMatcher.Validate()
if err != nil {
return err
}
_, err = d.exec(
2024-03-03 16:39:18 +01:00
"update project__integration_matcher set match_type=?, `method`=?, body_data_type=?, `key`=?, `value`=?, `name`=? where `id`=?",
2024-02-11 20:52:14 +01:00
integrationMatcher.MatchType,
integrationMatcher.Method,
integrationMatcher.BodyDataType,
integrationMatcher.Key,
integrationMatcher.Value,
integrationMatcher.Name,
integrationMatcher.ID)
return err
2023-07-03 01:41:13 +02:00
}