mirror of
https://github.com/grafana/grafana.git
synced 2026-08-18 17:15:08 -05: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:
@@ -21,6 +21,7 @@ func (s *CorrelationsService) registerAPIEndpoints() {
|
||||
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))
|
||||
entities.Delete("/:correlationUID", middleware.ReqSignedIn, authorize(ac.ReqOrgAdmin, ac.EvalPermission(datasources.ActionWrite, uidScope)), routing.Wrap(s.deleteHandler))
|
||||
entities.Patch("/:correlationUID", middleware.ReqSignedIn, authorize(ac.ReqOrgAdmin, ac.EvalPermission(datasources.ActionWrite, uidScope)), routing.Wrap(s.updateHandler))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -127,3 +128,66 @@ type DeleteCorrelationResponse struct {
|
||||
// in: body
|
||||
Body DeleteCorrelationResponseBody `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:route PATCH /datasources/uid/{sourceUID}/correlations/{correlationUID} correlations updateCorrelation
|
||||
//
|
||||
// Updates a correlation.
|
||||
//
|
||||
// Responses:
|
||||
// 200: updateCorrelationResponse
|
||||
// 400: badRequestError
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (s *CorrelationsService) updateHandler(c *models.ReqContext) response.Response {
|
||||
cmd := UpdateCorrelationCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
cmd.UID = web.Params(c.Req)[":correlationUID"]
|
||||
cmd.SourceUID = web.Params(c.Req)[":uid"]
|
||||
cmd.OrgId = c.OrgId
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if errors.Is(err, ErrSourceDataSourceDoesNotExists) {
|
||||
return response.Error(http.StatusNotFound, "Data source not found", err)
|
||||
}
|
||||
|
||||
if errors.Is(err, ErrCorrelationNotFound) {
|
||||
return response.Error(http.StatusNotFound, "Correlation 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 update correlation", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, UpdateCorrelationResponseBody{Message: "Correlation updated", Result: correlation})
|
||||
}
|
||||
|
||||
// swagger:parameters updateCorrelation
|
||||
type UpdateCorrelationParams struct {
|
||||
// in:path
|
||||
// required:true
|
||||
DatasourceUID string `json:"sourceUID"`
|
||||
// in:path
|
||||
// required:true
|
||||
CorrelationUID string `json:"correlationUID"`
|
||||
// in: body
|
||||
Body UpdateCorrelationCommand `json:"body"`
|
||||
}
|
||||
|
||||
//swagger:response updateCorrelationResponse
|
||||
type UpdateCorrelationResponse struct {
|
||||
// in: body
|
||||
Body UpdateCorrelationResponseBody `json:"body"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user