Semaphore/db/sql/integration.go

360 lines
10 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"
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) {
2024-03-06 14:28:24 +01:00
//var extractorReferrer []db.ObjectReferrer
//extractorReferrer, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationExtractorProps, integrationID)
//referrers = db.IntegrationReferrers{
// IntegrationExtractors: extractorReferrer,
//}
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 {
2024-03-06 14:28:24 +01:00
//extractors, err := d.GetIntegrationExtractors(0, db.RetrieveQueryParams{}, integrationID)
//
//if err != nil {
// return err
//}
//
//for extractor := range extractors {
// d.DeleteIntegrationExtractor(0, extractors[extractor].ID, integrationID)
//}
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-03-03 19:21:50 +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.AuthMethod,
integration.AuthSecretID,
2024-03-03 19:49:00 +01:00
integration.AuthHeader,
integration.ID)
2023-07-03 01:41:13 +02:00
return err
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationExtractValuesByExtractorID(integrationID 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").
2024-03-06 15:51:59 +01:00
Where(squirrel.Eq{"integration_id": integrationID}).
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-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationMatchersByExtractorID(integrationID 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").
2024-03-06 15:51:59 +01:00
Where(squirrel.Eq{"integration_id": integrationID}).
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-03-06 15:51:59 +01:00
//func (d *SqlDb) DeleteIntegrationExtractor(projectID int, integrationID int, integrationID int) error {
// values, err := d.GetIntegrationExtractValuesByExtractorID(integrationID)
2024-03-06 14:28:24 +01:00
// if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
// return err
// }
//
// for value := range values {
//
2024-03-06 15:51:59 +01:00
// err = d.DeleteIntegrationExtractValue(0, values[value].ID, integrationID)
2024-03-06 14:28:24 +01:00
// if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
// log.Error(err)
// return err
// }
// }
//
2024-03-06 15:51:59 +01:00
// matchers, errExtractor := d.GetIntegrationMatchersByExtractorID(integrationID)
2024-03-06 14:28:24 +01:00
// if errExtractor != nil && !strings.Contains(errExtractor.Error(), "no rows in result set") {
// log.Error(errExtractor)
// return errExtractor
// }
//
// for matcher := range matchers {
2024-03-06 15:51:59 +01:00
// err = d.DeleteIntegrationMatcher(0, matchers[matcher].ID, integrationID)
2024-03-06 14:28:24 +01:00
// if err != nil && !strings.Contains(err.Error(), "no rows in result set") {
// log.Error(err)
// return err
// }
// }
//
2024-03-06 15:51:59 +01:00
// return d.deleteObjectByReferencedID(integrationID, db.IntegrationProps, db.IntegrationExtractorProps, integrationID)
2024-03-06 14:28:24 +01:00
//}
//
//func (d *SqlDb) UpdateIntegrationExtractor(projectID int, integrationExtractor db.IntegrationExtractor) error {
// err := integrationExtractor.Validate()
//
// if err != nil {
// return err
// }
//
// _, err = d.exec(
// "update project__integration_extractor set name=? where id=?",
// integrationExtractor.Name,
// integrationExtractor.ID)
//
// return err
//}
2023-07-03 01:41:13 +02:00
2024-03-04 12:24:47 +01:00
func (d *SqlDb) CreateIntegrationExtractValue(projectId int, 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 "+
2024-03-06 14:28:24 +01:00
"(value_source, body_data_type, `key`, `variable`, `name`, integration_id) values "+
2024-02-11 23:39:57 +01:00
"(?, ?, ?, ?, ?, ?)",
value.ValueSource,
value.BodyDataType,
value.Key,
value.Variable,
value.Name,
2024-03-06 14:28:24 +01:00
value.IntegrationID)
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-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationExtractValues(projectID int, params db.RetrieveQueryParams, integrationID int) ([]db.IntegrationExtractValue, error) {
2024-02-11 20:52:14 +01:00
var values []db.IntegrationExtractValue
2024-03-06 15:51:59 +01:00
err := d.getObjectsByReferrer(integrationID, db.IntegrationProps, 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-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationExtractValue(projectID int, valueID int, integrationID 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-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationExtractValueRefs(projectID int, valueID int, integrationID int) (refs db.IntegrationExtractorChildReferrers, err error) {
refs.Integrations, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationExtractValueProps, integrationID)
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *SqlDb) DeleteIntegrationExtractValue(projectID int, valueID int, integrationID int) error {
return d.deleteObjectByReferencedID(integrationID, db.IntegrationProps, db.IntegrationExtractValueProps, valueID)
2023-07-03 01:41:13 +02:00
}
2024-03-04 12:24:47 +01:00
func (d *SqlDb) UpdateIntegrationExtractValue(projectID int, integrationExtractValue db.IntegrationExtractValue) error {
2024-02-11 20:52:14 +01:00
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-03-04 12:36:24 +01:00
func (d *SqlDb) CreateIntegrationMatcher(projectID int, 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 "+
2024-03-06 14:28:24 +01:00
"(match_type, `method`, body_data_type, `key`, `value`, integration_id, `name`) values "+
2024-02-11 23:27:03 +01:00
"(?, ?, ?, ?, ?, ?, ?)",
matcher.MatchType,
matcher.Method,
matcher.BodyDataType,
matcher.Key,
matcher.Value,
2024-03-06 14:28:24 +01:00
matcher.IntegrationID,
matcher.Name)
if err != nil {
return
}
newMatcher = matcher
newMatcher.ID = insertID
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationMatchers(projectID int, params db.RetrieveQueryParams, integrationID int) (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").
2024-03-06 15:51:59 +01:00
Where(squirrel.Eq{"integration_id": integrationID}).
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-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationMatcher(projectID int, matcherID int, integrationID 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-03-06 15:51:59 +01:00
func (d *SqlDb) GetIntegrationMatcherRefs(projectID int, matcherID int, integrationID int) (refs db.IntegrationExtractorChildReferrers, err error) {
2024-03-06 14:28:24 +01:00
refs.Integrations, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationMatcherProps, matcherID)
2023-07-03 01:41:13 +02:00
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 15:51:59 +01:00
func (d *SqlDb) DeleteIntegrationMatcher(projectID int, matcherID int, integrationID int) error {
return d.deleteObjectByReferencedID(integrationID, db.IntegrationProps, db.IntegrationMatcherProps, matcherID)
2023-07-03 01:41:13 +02:00
}
2024-03-04 12:36:24 +01:00
func (d *SqlDb) UpdateIntegrationMatcher(projectID int, integrationMatcher db.IntegrationMatcher) error {
2024-02-11 20:52:14 +01:00
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
}