mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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})
|
||||
|
||||
Reference in New Issue
Block a user