Correlations: Add UpdateCorrelation HTTP API (#52444)

* Correlations: add UpdateCorrelation HTTP API

* handle correlation not found

* add tests

* fix lint errors

* add bad request to API spec

* change casing

* fix casing in docs

* fix tests

* update spec
This commit is contained in:
Giordano Ricci
2022-08-03 14:18:51 +01:00
committed by GitHub
parent f61a97a0ab
commit 09c4dbdb9f
9 changed files with 738 additions and 1 deletions

View File

@@ -77,6 +77,61 @@ func (s CorrelationsService) deleteCorrelation(ctx context.Context, cmd DeleteCo
})
}
func (s CorrelationsService) updateCorrelation(ctx context.Context, cmd UpdateCorrelationCommand) (Correlation, error) {
correlation := Correlation{
UID: cmd.UID,
SourceUID: cmd.SourceUID,
}
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *sqlstore.DBSession) error {
query := &datasources.GetDataSourceQuery{
OrgId: cmd.OrgId,
Uid: cmd.SourceUID,
}
if err := s.DataSourceService.GetDataSource(ctx, query); err != nil {
return ErrSourceDataSourceDoesNotExists
}
if query.Result.ReadOnly {
return ErrSourceDataSourceReadOnly
}
if cmd.Label == nil && cmd.Description == nil {
return ErrUpdateCorrelationEmptyParams
}
update := Correlation{}
if cmd.Label != nil {
update.Label = *cmd.Label
session.MustCols("label")
}
if cmd.Description != nil {
update.Description = *cmd.Description
session.MustCols("description")
}
updateCount, err := session.Where("uid = ? AND source_uid = ?", correlation.UID, correlation.SourceUID).Limit(1).Update(update)
if updateCount == 0 {
return ErrCorrelationNotFound
}
if err != nil {
return err
}
found, err := session.Get(&correlation)
if !found {
return ErrCorrelationNotFound
}
return err
})
if err != nil {
return Correlation{}, err
}
return correlation, nil
}
func (s CorrelationsService) deleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error {
return s.SQLStore.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
_, err := session.Delete(&Correlation{SourceUID: cmd.SourceUID})