Alerting: Create endpoints for exporting in provisioning file format (#58623)

This adds provisioning endpoints for downloading alert rules and alert rule groups in a 
format that is compatible with file provisioning. Each endpoint supports both json and 
yaml response types via Accept header as well as a query parameter 
download=true/false that will set Content-Disposition to recommend initiating a download 
or inline display.

This also makes some package changes to keep structs with potential to drift closer 
together. Eventually, other alerting file structs should also move into this new file 
package, but the rest require some refactoring that is out of scope for this PR.
This commit is contained in:
Matthew Jacobson
2023-01-27 11:39:16 -05:00
committed by GitHub
parent d5294eb8fa
commit c006df375a
21 changed files with 2507 additions and 392 deletions

View File

@@ -9,6 +9,7 @@ import (
"reflect"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v3"
"github.com/grafana/grafana/pkg/infra/tracing"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
@@ -173,7 +174,8 @@ func (r *RedirectResponse) Body() []byte {
// JSON creates a JSON response.
func JSON(status int, body interface{}) *NormalResponse {
return Respond(status, body).SetHeader("Content-Type", "application/json")
return Respond(status, body).
SetHeader("Content-Type", "application/json")
}
// JSONStreaming creates a streaming JSON response.
@@ -187,6 +189,30 @@ func JSONStreaming(status int, body interface{}) StreamingResponse {
}
}
// JSONDownload creates a JSON response indicating that it should be downloaded.
func JSONDownload(status int, body interface{}, filename string) *NormalResponse {
return JSON(status, body).
SetHeader("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, filename))
}
// YAML creates a YAML response.
func YAML(status int, body interface{}) *NormalResponse {
b, err := yaml.Marshal(body)
if err != nil {
return Error(http.StatusInternalServerError, "body yaml marshal", err)
}
// As of now, application/yaml is downloaded by default in chrome regardless of Content-Disposition, so we use text/yaml instead.
return Respond(status, b).
SetHeader("Content-Type", "text/yaml")
}
// YAMLDownload creates a YAML response indicating that it should be downloaded.
func YAMLDownload(status int, body interface{}, filename string) *NormalResponse {
return YAML(status, body).
SetHeader("Content-Type", "application/yaml").
SetHeader("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, filename))
}
// Success create a successful response
func Success(message string) *NormalResponse {
resp := make(map[string]interface{})