grafana/pkg/services/ngalert/api/api_upgrade.go
Matthew Jacobson 118e4a50b7
Alerting: Remove start page of legacy upgrade preview (#82010)
Alerting: Remove start page of upgrade preview

Alerting upgrade page will now always show the summary table even before
upgrading any alerts or notification channels. There a few reasons for this:

- The information on the start page is redundant as it's now contained in the
documentation.
- Previously, if some unexpected issue prevented performing a full upgrade, a
user would have limited to no means to using the preview tool to help fix the
problem. This is because you could not see the summary table until the full
upgrade was performed at least once. Now, you can upgrade individual alerts and
notification channels from the beginning.
2024-02-15 17:34:00 -05:00

141 lines
5.1 KiB
Go

package api
import (
"errors"
"net/http"
"strconv"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/ngalert/migration"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
type UpgradeSrv struct {
log log.Logger
upgradeService migration.UpgradeService
cfg *setting.Cfg
}
func NewUpgradeSrc(
log log.Logger,
upgradeService migration.UpgradeService,
cfg *setting.Cfg,
) *UpgradeSrv {
return &UpgradeSrv{
log: log,
upgradeService: upgradeService,
cfg: cfg,
}
}
func (srv *UpgradeSrv) RoutePostUpgradeOrg(c *contextmodel.ReqContext) response.Response {
summary, err := srv.upgradeService.MigrateOrg(c.Req.Context(), c.OrgID, c.QueryBool("skipExisting"))
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, summary)
}
func (srv *UpgradeSrv) RouteGetOrgUpgrade(c *contextmodel.ReqContext) response.Response {
state, err := srv.upgradeService.GetOrgMigrationState(c.Req.Context(), c.OrgID)
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, state)
}
func (srv *UpgradeSrv) RouteDeleteOrgUpgrade(c *contextmodel.ReqContext) response.Response {
err := srv.upgradeService.RevertOrg(c.Req.Context(), c.OrgID)
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, util.DynMap{"message": "Grafana Alerting resources deleted for this organization."})
}
func (srv *UpgradeSrv) RoutePostUpgradeAlert(c *contextmodel.ReqContext, dashboardIdParam string, panelIdParam string) response.Response {
dashboardId, err := strconv.ParseInt(dashboardIdParam, 10, 64)
if err != nil {
return ErrResp(http.StatusBadRequest, err, "failed to parse dashboardId")
}
panelId, err := strconv.ParseInt(panelIdParam, 10, 64)
if err != nil {
return ErrResp(http.StatusBadRequest, err, "failed to parse panelId")
}
summary, err := srv.upgradeService.MigrateAlert(c.Req.Context(), c.OrgID, dashboardId, panelId)
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, summary)
}
func (srv *UpgradeSrv) RoutePostUpgradeDashboard(c *contextmodel.ReqContext, dashboardIdParam string) response.Response {
dashboardId, err := strconv.ParseInt(dashboardIdParam, 10, 64)
if err != nil {
return ErrResp(http.StatusBadRequest, err, "failed to parse dashboardId")
}
summary, err := srv.upgradeService.MigrateDashboardAlerts(c.Req.Context(), c.OrgID, dashboardId, c.QueryBool("skipExisting"))
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, summary)
}
func (srv *UpgradeSrv) RoutePostUpgradeAllDashboards(c *contextmodel.ReqContext) response.Response {
summary, err := srv.upgradeService.MigrateAllDashboardAlerts(c.Req.Context(), c.OrgID, c.QueryBool("skipExisting"))
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, summary)
}
func (srv *UpgradeSrv) RoutePostUpgradeChannel(c *contextmodel.ReqContext, channelIdParam string) response.Response {
channelId, err := strconv.ParseInt(channelIdParam, 10, 64)
if err != nil {
return ErrResp(http.StatusBadRequest, err, "failed to parse channelId")
}
summary, err := srv.upgradeService.MigrateChannel(c.Req.Context(), c.OrgID, channelId)
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, summary)
}
func (srv *UpgradeSrv) RoutePostUpgradeAllChannels(c *contextmodel.ReqContext) response.Response {
summary, err := srv.upgradeService.MigrateAllChannels(c.Req.Context(), c.OrgID, c.QueryBool("skipExisting"))
if err != nil {
if errors.Is(err, migration.ErrUpgradeInProgress) {
return ErrResp(http.StatusConflict, err, "Upgrade already in progress")
}
return ErrResp(http.StatusInternalServerError, err, "Server error")
}
return response.JSON(http.StatusOK, summary)
}