mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Correlations: Add CreateCorrelation HTTP API (#51630)
* Correlations: add migration
* Correlations: Add CreateCorrelation API
* Correlations: Make correlations work with provisioning
* Handle version changes
* Fix lining error
* lint fixes
* rebuild betterer results
* add a UID to each correlation
* Fix lint errors
* add docs
* better wording in API docs
* remove leftover comment
* handle ds updates
* Fix error message typo
* add bad data test
* make correlations a separate table
* skip readonly check when provisioning correlations
* delete stale correlations when datasources are deleted
* restore provisioned readonly ds
* publish deletion event with full data
* generate swagger and HTTP API docs
* apply source datasource permission to create correlation API
* Fix tests & lint errors
* ignore empty deletion events
* fix last lint errors
* fix more lint error
* Only publish deletion event if datasource was actually deleted
* delete DS provisioning deletes correlations, added & fixed tests
* Fix unmarshalling tests
* Fix linting errors
* Fix deltion event tests
* fix small linting error
* fix lint errors
* update betterer
* fix test
* make path singular
* Revert "make path singular"
This reverts commit 420c3d315e.
* add integration tests
* remove unneeded id from correlations table
* update spec
* update leftover references to CorrelationDTO
* fix tests
* cleanup tests
* fix lint error
This commit is contained in:
49
pkg/services/correlations/api.go
Normal file
49
pkg/services/correlations/api.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package correlations
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func (s *CorrelationsService) registerAPIEndpoints() {
|
||||
uidScope := datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":uid"))
|
||||
authorize := ac.Middleware(s.AccessControl)
|
||||
|
||||
s.RouteRegister.Group("/api/datasources/uid/:uid/correlations", func(entities routing.RouteRegister) {
|
||||
entities.Post("/", middleware.ReqSignedIn, authorize(ac.ReqOrgAdmin, ac.EvalPermission(datasources.ActionWrite, uidScope)), routing.Wrap(s.createHandler))
|
||||
})
|
||||
}
|
||||
|
||||
// createHandler handles POST /datasources/uid/:uid/correlations
|
||||
func (s *CorrelationsService) createHandler(c *models.ReqContext) response.Response {
|
||||
cmd := CreateCorrelationCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
cmd.SourceUID = web.Params(c.Req)[":uid"]
|
||||
cmd.OrgId = c.OrgId
|
||||
|
||||
correlation, err := s.CreateCorrelation(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrSourceDataSourceDoesNotExists) || errors.Is(err, ErrTargetDataSourceDoesNotExists) {
|
||||
return response.Error(http.StatusNotFound, "Data source not found", err)
|
||||
}
|
||||
|
||||
if errors.Is(err, ErrSourceDataSourceReadOnly) {
|
||||
return response.Error(http.StatusForbidden, "Data source is read only", err)
|
||||
}
|
||||
|
||||
return response.Error(http.StatusInternalServerError, "Failed to add correlation", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, CreateCorrelationResponse{Result: correlation, Message: "Correlation created"})
|
||||
}
|
||||
74
pkg/services/correlations/correlations.go
Normal file
74
pkg/services/correlations/correlations.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package correlations
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
)
|
||||
|
||||
func ProvideService(sqlStore *sqlstore.SQLStore, routeRegister routing.RouteRegister, ds datasources.DataSourceService, ac accesscontrol.AccessControl, bus bus.Bus) *CorrelationsService {
|
||||
s := &CorrelationsService{
|
||||
SQLStore: sqlStore,
|
||||
RouteRegister: routeRegister,
|
||||
log: log.New("correlations"),
|
||||
DataSourceService: ds,
|
||||
AccessControl: ac,
|
||||
}
|
||||
|
||||
s.registerAPIEndpoints()
|
||||
|
||||
bus.AddEventListener(s.handleDatasourceDeletion)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
CreateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) (Correlation, error)
|
||||
DeleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error
|
||||
DeleteCorrelationsByTargetUID(ctx context.Context, cmd DeleteCorrelationsByTargetUIDCommand) error
|
||||
}
|
||||
|
||||
type CorrelationsService struct {
|
||||
SQLStore *sqlstore.SQLStore
|
||||
RouteRegister routing.RouteRegister
|
||||
log log.Logger
|
||||
DataSourceService datasources.DataSourceService
|
||||
AccessControl accesscontrol.AccessControl
|
||||
}
|
||||
|
||||
func (s CorrelationsService) CreateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) (Correlation, error) {
|
||||
return s.createCorrelation(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s CorrelationsService) DeleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error {
|
||||
return s.deleteCorrelationsBySourceUID(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s CorrelationsService) DeleteCorrelationsByTargetUID(ctx context.Context, cmd DeleteCorrelationsByTargetUIDCommand) error {
|
||||
return s.deleteCorrelationsByTargetUID(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s CorrelationsService) handleDatasourceDeletion(ctx context.Context, event *events.DataSourceDeleted) error {
|
||||
return s.SQLStore.InTransaction(ctx, func(ctx context.Context) error {
|
||||
if err := s.deleteCorrelationsBySourceUID(ctx, DeleteCorrelationsBySourceUIDCommand{
|
||||
SourceUID: event.UID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.deleteCorrelationsByTargetUID(ctx, DeleteCorrelationsByTargetUIDCommand{
|
||||
TargetUID: event.UID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
70
pkg/services/correlations/database.go
Normal file
70
pkg/services/correlations/database.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package correlations
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
// createCorrelation adds a correlation
|
||||
func (s CorrelationsService) createCorrelation(ctx context.Context, cmd CreateCorrelationCommand) (Correlation, error) {
|
||||
correlation := Correlation{
|
||||
UID: util.GenerateShortUID(),
|
||||
SourceUID: cmd.SourceUID,
|
||||
TargetUID: cmd.TargetUID,
|
||||
Label: cmd.Label,
|
||||
Description: cmd.Description,
|
||||
}
|
||||
|
||||
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *sqlstore.DBSession) error {
|
||||
var err error
|
||||
|
||||
query := &datasources.GetDataSourceQuery{
|
||||
OrgId: cmd.OrgId,
|
||||
Uid: cmd.SourceUID,
|
||||
}
|
||||
if err = s.DataSourceService.GetDataSource(ctx, query); err != nil {
|
||||
return ErrSourceDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
if !cmd.SkipReadOnlyCheck && query.Result.ReadOnly {
|
||||
return ErrSourceDataSourceReadOnly
|
||||
}
|
||||
|
||||
if err = s.DataSourceService.GetDataSource(ctx, &datasources.GetDataSourceQuery{
|
||||
OrgId: cmd.OrgId,
|
||||
Uid: cmd.TargetUID,
|
||||
}); err != nil {
|
||||
return ErrTargetDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
_, err = session.Insert(correlation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
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})
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s CorrelationsService) deleteCorrelationsByTargetUID(ctx context.Context, cmd DeleteCorrelationsByTargetUIDCommand) error {
|
||||
return s.SQLStore.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
|
||||
_, err := session.Delete(&Correlation{TargetUID: cmd.TargetUID})
|
||||
return err
|
||||
})
|
||||
}
|
||||
66
pkg/services/correlations/models.go
Normal file
66
pkg/services/correlations/models.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package correlations
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrSourceDataSourceReadOnly = errors.New("source data source is read only")
|
||||
ErrSourceDataSourceDoesNotExists = errors.New("source data source does not exist")
|
||||
ErrTargetDataSourceDoesNotExists = errors.New("target data source does not exist")
|
||||
ErrCorrelationFailedGenerateUniqueUid = errors.New("failed to generate unique correlation UID")
|
||||
ErrCorrelationIdentifierNotSet = errors.New("source identifier and org id are needed to be able to edit correlations")
|
||||
)
|
||||
|
||||
// Correlation is the model for correlations definitions
|
||||
type Correlation struct {
|
||||
// Unique identifier of the correlation
|
||||
// example: 50xhMlg9k
|
||||
UID string `json:"uid" xorm:"pk 'uid'"`
|
||||
// UID of the data source the correlation originates from
|
||||
// example:d0oxYRg4z
|
||||
SourceUID string `json:"sourceUid" xorm:"pk 'source_uid'"`
|
||||
// UID of the data source the correlation points to
|
||||
// example:PE1C5CBDA0504A6A3
|
||||
TargetUID string `json:"targetUid" xorm:"target_uid"`
|
||||
// Label identifying the correlation
|
||||
// example: My Label
|
||||
Label string `json:"label" xorm:"label"`
|
||||
// Description of the correlation
|
||||
// example: Logs to Traces
|
||||
Description string `json:"description" xorm:"description"`
|
||||
}
|
||||
|
||||
// CreateCorrelationResponse is the response struct for CreateCorrelationCommand
|
||||
// swagger:model
|
||||
type CreateCorrelationResponse struct {
|
||||
Result Correlation `json:"result"`
|
||||
// example: Correlation created
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// CreateCorrelationCommand is the command for creating a correlation
|
||||
// swagger:model
|
||||
type CreateCorrelationCommand struct {
|
||||
// UID of the data source for which correlation is created.
|
||||
SourceUID string `json:"-"`
|
||||
OrgId int64 `json:"-"`
|
||||
SkipReadOnlyCheck bool `json:"-"`
|
||||
// Target data source UID to which the correlation is created
|
||||
// example:PE1C5CBDA0504A6A3
|
||||
TargetUID string `json:"targetUid" binding:"Required"`
|
||||
// Optional label identifying the correlation
|
||||
// example: My label
|
||||
Label string `json:"label"`
|
||||
// Optional description of the correlation
|
||||
// example: Logs to Traces
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type DeleteCorrelationsBySourceUIDCommand struct {
|
||||
SourceUID string
|
||||
}
|
||||
|
||||
type DeleteCorrelationsByTargetUIDCommand struct {
|
||||
TargetUID string
|
||||
}
|
||||
Reference in New Issue
Block a user