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
@@ -4,6 +4,8 @@ import (
"time"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/provisioning/alerting/file"
"github.com/prometheus/common/model"
)
@@ -14,6 +16,14 @@ import (
// Responses:
// 200: ProvisionedAlertRules
// swagger:route GET /api/v1/provisioning/alert-rules/export provisioning stable RouteGetAlertRulesExport
//
// Export all alert rules in provisioning file format.
//
// Responses:
// 200: AlertingFileExport
// 404: description: Not found.
// swagger:route GET /api/v1/provisioning/alert-rules/{UID} provisioning stable RouteGetAlertRule
//
// Get a specific alert rule by UID.
@@ -22,6 +32,19 @@ import (
// 200: ProvisionedAlertRule
// 404: description: Not found.
// swagger:route GET /api/v1/provisioning/alert-rules/{UID}/export provisioning stable RouteGetAlertRuleExport
//
// Export an alert rule in provisioning file format.
//
// Produces:
// - application/json
// - application/yaml
// - text/yaml
//
// Responses:
// 200: AlertingFileExport
// 404: description: Not found.
// swagger:route POST /api/v1/provisioning/alert-rules provisioning stable RoutePostAlertRule
//
// Create a new alert rule.
@@ -51,7 +74,7 @@ import (
// Responses:
// 204: description: The alert rule was deleted successfully.
// swagger:parameters RouteGetAlertRule RoutePutAlertRule RouteDeleteAlertRule
// swagger:parameters RouteGetAlertRule RoutePutAlertRule RouteDeleteAlertRule RouteGetAlertRuleExport
type AlertRuleUIDReference struct {
// Alert rule UID
// in:path
@@ -168,6 +191,19 @@ func NewAlertRules(rules []*models.AlertRule) ProvisionedAlertRules {
// 200: AlertRuleGroup
// 404: description: Not found.
// swagger:route GET /api/v1/provisioning/folder/{FolderUID}/rule-groups/{Group}/export provisioning stable RouteGetAlertRuleGroupExport
//
// Export an alert rule group in provisioning file format.
//
// Produces:
// - application/json
// - application/yaml
// - text/yaml
//
// Responses:
// 200: AlertingFileExport
// 404: description: Not found.
// swagger:route PUT /api/v1/provisioning/folder/{FolderUID}/rule-groups/{Group} provisioning stable RoutePutAlertRuleGroup
//
// Update the interval of a rule group.
@@ -179,13 +215,13 @@ func NewAlertRules(rules []*models.AlertRule) ProvisionedAlertRules {
// 200: AlertRuleGroup
// 400: ValidationError
// swagger:parameters RouteGetAlertRuleGroup RoutePutAlertRuleGroup
// swagger:parameters RouteGetAlertRuleGroup RoutePutAlertRuleGroup RouteGetAlertRuleGroupExport
type FolderUIDPathParam struct {
// in:path
FolderUID string `json:"FolderUID"`
}
// swagger:parameters RouteGetAlertRuleGroup RoutePutAlertRuleGroup
// swagger:parameters RouteGetAlertRuleGroup RoutePutAlertRuleGroup RouteGetAlertRuleGroupExport
type RuleGroupPathParam struct {
// in:path
Group string `json:"Group"`
@@ -202,6 +238,15 @@ type AlertRuleGroupMetadata struct {
Interval int64 `json:"interval"`
}
// swagger:parameters RouteGetAlertRuleGroupExport RouteGetAlertRuleExport RouteGetAlertRulesExport
type ExportQueryParams struct {
// Whether to initiate a download of the file or not.
// in: query
// required: false
// default: false
Download bool `json:"download"`
}
// swagger:model
type AlertRuleGroup struct {
Title string `json:"title"`
@@ -210,6 +255,10 @@ type AlertRuleGroup struct {
Rules []ProvisionedAlertRule `json:"rules"`
}
// AlertingFileExport is the full provisioned file export.
// swagger:model
type AlertingFileExport = file.AlertingFileExport
func (a *AlertRuleGroup) ToModel() (models.AlertRuleGroup, error) {
ruleGroup := models.AlertRuleGroup{
Title: a.Title,