Files
grafana/pkg/services/cloudmigration/api/api.go
grafana-delivery-bot[bot] b49a40d319 [v11.0.x] Grafana: define the api for the grafana cloudmigration api (not the csm api) (#86115)
Grafana: define the api for the grafana cloudmigration api (not the csm api) (#84430)

* Add cloud migration endpoints

* Built auth into creating a migration.

* Added more detail to the migration result model

* goimports

* Update pkg/services/cloudmigration/api/api.go

Co-authored-by: lean.dev <34773040+leandro-deveikis@users.noreply.github.com>

* Update pkg/services/cloudmigration/api/api.go

Co-authored-by: lean.dev <34773040+leandro-deveikis@users.noreply.github.com>

---------

Co-authored-by: Leonard Gram <leo@xlson.com>
Co-authored-by: lean.dev <34773040+leandro-deveikis@users.noreply.github.com>
(cherry picked from commit e2f155f9f7)

Co-authored-by: idafurjes <36131195+idafurjes@users.noreply.github.com>
2024-04-15 13:58:39 +01:00

120 lines
4.7 KiB
Go

package api
import (
"net/http"
"strconv"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/services/cloudmigration"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/web"
)
type CloudMigrationAPI struct {
cloudMigrationsService cloudmigration.Service
routeRegister routing.RouteRegister
log log.Logger
}
func RegisterApi(
rr routing.RouteRegister,
cms cloudmigration.Service,
) *CloudMigrationAPI {
api := &CloudMigrationAPI{
log: log.New("cloudmigrations.api"),
routeRegister: rr,
cloudMigrationsService: cms,
}
api.registerEndpoints()
return api
}
// RegisterAPIEndpoints Registers Endpoints on Grafana Router
func (cma *CloudMigrationAPI) registerEndpoints() {
cma.routeRegister.Group("/api/cloudmigration", func(cloudMigrationRoute routing.RouteRegister) {
// migration
cloudMigrationRoute.Get("/migration", routing.Wrap(cma.GetMigrationList))
cloudMigrationRoute.Post("/migration", routing.Wrap(cma.CreateMigration))
cloudMigrationRoute.Get("/migration/:id", routing.Wrap(cma.GetMigration))
cloudMigrationRoute.Delete("migration/:id", routing.Wrap(cma.DeleteMigration))
cloudMigrationRoute.Post("/migration/:id/run", routing.Wrap(cma.RunMigration))
cloudMigrationRoute.Get("/migration/:id/run", routing.Wrap(cma.GetMigrationRunList))
cloudMigrationRoute.Get("/migration/:id/run/:runID", routing.Wrap(cma.GetMigrationRun))
}, middleware.ReqGrafanaAdmin)
}
func (cma *CloudMigrationAPI) CreateToken(c *contextmodel.ReqContext) response.Response {
err := cma.cloudMigrationsService.CreateToken(c.Req.Context())
if err != nil {
return response.Error(http.StatusInternalServerError, "token creation error", err)
}
return response.Success("Token created")
}
func (cma *CloudMigrationAPI) GetMigrationList(c *contextmodel.ReqContext) response.Response {
cloudMigrations, err := cma.cloudMigrationsService.GetMigrationList(c.Req.Context())
if err != nil {
return response.Error(http.StatusInternalServerError, "migration list error", err)
}
return response.JSON(http.StatusOK, cloudMigrations)
}
func (cma *CloudMigrationAPI) GetMigration(c *contextmodel.ReqContext) response.Response {
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
if err != nil {
return response.Error(http.StatusBadRequest, "id is invalid", err)
}
cloudMigration, err := cma.cloudMigrationsService.GetMigration(c.Req.Context(), id)
if err != nil {
return response.Error(http.StatusNotFound, "migration not found", err)
}
return response.JSON(http.StatusOK, cloudMigration)
}
func (cma *CloudMigrationAPI) CreateMigration(c *contextmodel.ReqContext) response.Response {
cmd := cloudmigration.CloudMigrationRequest{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
cloudMigration, err := cma.cloudMigrationsService.CreateMigration(c.Req.Context(), cmd)
if err != nil {
return response.Error(http.StatusInternalServerError, "migration creation error", err)
}
return response.JSON(http.StatusOK, cloudMigration)
}
func (cma *CloudMigrationAPI) RunMigration(c *contextmodel.ReqContext) response.Response {
cloudMigrationRun, err := cma.cloudMigrationsService.RunMigration(c.Req.Context(), web.Params(c.Req)[":id"])
if err != nil {
return response.Error(http.StatusInternalServerError, "migration run error", err)
}
return response.JSON(http.StatusOK, cloudMigrationRun)
}
func (cma *CloudMigrationAPI) GetMigrationRun(c *contextmodel.ReqContext) response.Response {
migrationStatus, err := cma.cloudMigrationsService.GetMigrationStatus(c.Req.Context(), web.Params(c.Req)[":id"], web.Params(c.Req)[":runID"])
if err != nil {
return response.Error(http.StatusInternalServerError, "migration status error", err)
}
return response.JSON(http.StatusOK, migrationStatus)
}
func (cma *CloudMigrationAPI) GetMigrationRunList(c *contextmodel.ReqContext) response.Response {
migrationStatus, err := cma.cloudMigrationsService.GetMigrationStatusList(c.Req.Context(), web.Params(c.Req)[":id"])
if err != nil {
return response.Error(http.StatusInternalServerError, "migration status error", err)
}
return response.JSON(http.StatusOK, migrationStatus)
}
func (cma *CloudMigrationAPI) DeleteMigration(c *contextmodel.ReqContext) response.Response {
err := cma.cloudMigrationsService.DeleteMigration(c.Req.Context(), web.Params(c.Req)[":id"])
if err != nil {
return response.Error(http.StatusInternalServerError, "migration delete error", err)
}
return response.Empty(http.StatusOK)
}