Glue: Add configuration support to PATCH (#56117)

* feat: add config to tests v0

* feat: add config to UpdateCorrelationCommand

* refactor: repair some tests

* refactor: repair another test

* refactor: repair last test

* refactor: remove possible test

* refactor: add comments

* refactor: add changes from code review

* refactor: implement more detailed patch

* feat: add tests for partial config update

* refactor: make error handling more detailed

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

Co-authored-by: Giordano Ricci <me@giordanoricci.com>
This commit is contained in:
Laura Benz
2022-10-10 15:36:14 +02:00
committed by GitHub
parent 680dfde90d
commit bc9a37ee8d
5 changed files with 152 additions and 28 deletions

View File

@@ -160,7 +160,7 @@ func (s *CorrelationsService) updateHandler(c *models.ReqContext) response.Respo
correlation, err := s.UpdateCorrelation(c.Req.Context(), cmd)
if err != nil {
if errors.Is(err, ErrUpdateCorrelationEmptyParams) {
return response.Error(http.StatusBadRequest, "At least one of label, description is required", err)
return response.Error(http.StatusBadRequest, "At least one of label, description or config is required", err)
}
if errors.Is(err, ErrSourceDataSourceDoesNotExists) {

View File

@@ -99,32 +99,42 @@ func (s CorrelationsService) updateCorrelation(ctx context.Context, cmd UpdateCo
return ErrSourceDataSourceReadOnly
}
if cmd.Label == nil && cmd.Description == nil {
if cmd.Label == nil && cmd.Description == nil && (cmd.Config == nil || (cmd.Config.Field == nil && cmd.Config.Target == nil && cmd.Config.Type == 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 {
found, err := session.Get(&correlation)
if !found {
return ErrCorrelationNotFound
}
if err != nil {
return err
}
found, err := session.Get(&correlation)
if !found {
return ErrCorrelationNotFound
if cmd.Label != nil {
correlation.Label = *cmd.Label
session.MustCols("label")
}
if cmd.Description != nil {
correlation.Description = *cmd.Description
session.MustCols("description")
}
if cmd.Config != nil {
session.MustCols("config")
if cmd.Config.Field != nil {
correlation.Config.Field = *cmd.Config.Field
}
if cmd.Config.Type != nil {
correlation.Config.Type = *cmd.Config.Type
}
if cmd.Config.Target != nil {
correlation.Config.Target = *cmd.Config.Target
}
}
updateCount, err := session.Where("uid = ? AND source_uid = ?", correlation.UID, correlation.SourceUID).Limit(1).Update(correlation)
if updateCount == 0 {
return ErrCorrelationNotFound
}
return err
})

View File

@@ -58,6 +58,18 @@ func (c CorrelationConfig) MarshalJSON() ([]byte, error) {
})
}
type CorrelationConfigUpdateDTO struct {
// Field used to attach the correlation link
// required:true
Field *string `json:"field"`
// Target type
// required:true
Type *CorrelationConfigType `json:"type"`
// Target data query
// required:true
Target *map[string]interface{} `json:"target"`
}
// Correlation is the model for correlations definitions
// swagger:model
type Correlation struct {
@@ -77,7 +89,7 @@ type Correlation struct {
// example: Logs to Traces
Description string `json:"description" xorm:"description"`
// Correlation Configuration
// example: { field: "job", target: { query: "job=app" } }
// example: { field: "job", type: "query", target: { query: "job=app" } }
Config CorrelationConfig `json:"config" xorm:"jsonb config"`
}
@@ -106,7 +118,7 @@ type CreateCorrelationCommand struct {
// example: Logs to Traces
Description string `json:"description"`
// Arbitrary configuration object handled in frontend
// example: { field: "job", target: { query: "job=app" } }
// example: { field: "job", type: "query", target: { query: "job=app" } }
Config CorrelationConfig `json:"config" binding:"Required"`
}
@@ -154,6 +166,9 @@ type UpdateCorrelationCommand struct {
// Optional description of the correlation
// example: Logs to Traces
Description *string `json:"description"`
// Correlation Configuration
// example: { field: "job", type: "query", target: { query: "job=app" } }
Config *CorrelationConfigUpdateDTO `json:"config"`
}
// GetCorrelationQuery is the query to retrieve a single correlation