[v11.0.x] Cloud migrations: create endpoint to create an access token (#86137)

Cloud migrations: create endpoint to create an access token (#84690)

* fix merge conflicts

* make token expiration configurable

(cherry picked from commit a2e21eac8c)

Co-authored-by: Bruno <brunotj2015@hotmail.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-04-15 14:53:53 +01:00
committed by GitHub
co-authored by Bruno
parent 16f1b9d4f0
commit 0fe31d86fc
12 changed files with 560 additions and 53 deletions
+15 -3
View File
@@ -7,6 +7,7 @@ import (
"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/infra/tracing"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/services/cloudmigration"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
@@ -17,16 +18,19 @@ type CloudMigrationAPI struct {
cloudMigrationsService cloudmigration.Service
routeRegister routing.RouteRegister
log log.Logger
tracer tracing.Tracer
}
func RegisterApi(
rr routing.RouteRegister,
cms cloudmigration.Service,
tracer tracing.Tracer,
) *CloudMigrationAPI {
api := &CloudMigrationAPI{
log: log.New("cloudmigrations.api"),
routeRegister: rr,
cloudMigrationsService: cms,
tracer: tracer,
}
api.registerEndpoints()
return api
@@ -43,15 +47,23 @@ func (cma *CloudMigrationAPI) registerEndpoints() {
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))
cloudMigrationRoute.Post("/token", routing.Wrap(cma.CreateToken))
}, middleware.ReqGrafanaAdmin)
}
func (cma *CloudMigrationAPI) CreateToken(c *contextmodel.ReqContext) response.Response {
err := cma.cloudMigrationsService.CreateToken(c.Req.Context())
ctx, span := cma.tracer.Start(c.Req.Context(), "MigrationAPI.CreateAccessToken")
defer span.End()
logger := cma.log.FromContext(ctx)
resp, err := cma.cloudMigrationsService.CreateToken(ctx)
if err != nil {
return response.Error(http.StatusInternalServerError, "token creation error", err)
logger.Error("creating gcom access token", "err", err.Error())
return response.Error(http.StatusInternalServerError, "creating gcom access token", err)
}
return response.Success("Token created")
return response.JSON(http.StatusOK, cloudmigration.CreateAccessTokenResponseDTO(resp))
}
func (cma *CloudMigrationAPI) GetMigrationList(c *contextmodel.ReqContext) response.Response {