Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pipectl migrate with get then set at pipectl side #5506

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions pkg/app/pipectl/cmd/migrate/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

for _, appID := range c.applications {
input.Logger.Info("migrating database", zap.String("application", appID))
if err := c.migrateApplication(ctx, client, appID); err != nil {
if err := c.migrateApplication(ctx, client, appID, input.Logger); err != nil {

Check warning on line 58 in pkg/app/pipectl/cmd/migrate/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/migrate/database.go#L58

Added line #L58 was not covered by tests
input.Logger.Error("failed to migrate database", zap.String("application", appID), zap.Error(err))
return err
}
Expand All @@ -65,16 +65,33 @@
return nil
}

func (c *database) migrateApplication(ctx context.Context, client apiservice.Client, appID string) error {
req := &apiservice.MigrateDatabaseRequest{
Target: &apiservice.MigrateDatabaseRequest_Application_{
Application: &apiservice.MigrateDatabaseRequest_Application{
ApplicationId: appID,
},
},
func (c *database) migrateApplication(ctx context.Context, client apiservice.Client, appID string, logger *zap.Logger) error {
app, err := client.GetApplication(ctx, &apiservice.GetApplicationRequest{ApplicationId: appID})
if err != nil {
logger.Error("failed to get application", zap.Error(err), zap.String("application", appID))
return err

Check warning on line 72 in pkg/app/pipectl/cmd/migrate/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/migrate/database.go#L68-L72

Added lines #L68 - L72 were not covered by tests
}
if _, err := client.MigrateDatabase(ctx, req); err != nil {

if len(app.GetApplication().GetDeployTargets()) > 0 {
logger.Info("skip migrating database because the deploy target is already set", zap.String("application", appID))
return nil
}

Check warning on line 78 in pkg/app/pipectl/cmd/migrate/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/migrate/database.go#L75-L78

Added lines #L75 - L78 were not covered by tests

provider := app.GetApplication().GetPlatformProvider()

if provider == "" {
logger.Info("skip migrating database because the platform provider is not set", zap.String("application", appID))
return nil
}

Check warning on line 85 in pkg/app/pipectl/cmd/migrate/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/migrate/database.go#L80-L85

Added lines #L80 - L85 were not covered by tests

// Migrate database for the application.
if _, err := client.UpdateApplicationDeployTargets(ctx, &apiservice.UpdateApplicationDeployTargetsRequest{
ApplicationId: appID,
DeployTargets: []string{provider},
}); err != nil {
logger.Error("failed to update application deploy targets", zap.Error(err), zap.String("application", appID))

Check warning on line 92 in pkg/app/pipectl/cmd/migrate/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/migrate/database.go#L88-L92

Added lines #L88 - L92 were not covered by tests
return err
}

return nil
}
48 changes: 21 additions & 27 deletions pkg/app/server/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,27 @@
return &apiservice.RenameApplicationConfigFileResponse{}, nil
}

func (a *API) UpdateApplicationDeployTargets(ctx context.Context, req *apiservice.UpdateApplicationDeployTargetsRequest) (*apiservice.UpdateApplicationDeployTargetsResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_WRITE, a.logger)
if err != nil {
return nil, err
}

Check warning on line 470 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L466-L470

Added lines #L466 - L470 were not covered by tests

app, err := getApplication(ctx, a.applicationStore, req.ApplicationId, a.logger)
if err != nil {
a.logger.Warn("failed to get application", zap.Error(err))
return nil, err
}
if app.ProjectId != key.ProjectId {
a.logger.Warn("requested application does not belong to your project", zap.String("applicationID", app.Id), zap.String("requestProjectID", key.ProjectId), zap.String("applicationProjectID", app.ProjectId))
return nil, status.Error(codes.PermissionDenied, fmt.Sprintf("requested application %s does not belong to your project", req.GetApplicationId()))
}
if err := a.applicationStore.UpdateDeployTargets(ctx, req.GetApplicationId(), req.GetDeployTargets()); err != nil {
return nil, gRPCStoreError(err, fmt.Sprintf("failed to update application %s deploy targets", req.ApplicationId))
}
return &apiservice.UpdateApplicationDeployTargetsResponse{}, nil

Check warning on line 484 in pkg/app/server/grpcapi/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/server/grpcapi/api.go#L472-L484

Added lines #L472 - L484 were not covered by tests
}

func (a *API) GetDeployment(ctx context.Context, req *apiservice.GetDeploymentRequest) (*apiservice.GetDeploymentResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_ONLY, a.logger)
if err != nil {
Expand Down Expand Up @@ -1023,33 +1044,6 @@
}, nil
}

func (a *API) MigrateDatabase(ctx context.Context, req *apiservice.MigrateDatabaseRequest) (*apiservice.MigrateDatabaseResponse, error) {
if _, err := requireAPIKey(ctx, model.APIKey_READ_WRITE, a.logger); err != nil {
return nil, err
}

switch { //nolint:gocritic // we plan to add more cases
case req.GetApplication() != nil:
if err := a.migrateApplication(ctx, req.GetApplication()); err != nil {
a.logger.Error("failed to migrate application", zap.Error(err))
return nil, err
}
return &apiservice.MigrateDatabaseResponse{}, nil
}
return nil, status.Error(codes.Unimplemented, "Not implemented")
}

func (a *API) migrateApplication(ctx context.Context, app *apiservice.MigrateDatabaseRequest_Application) error {
application, err := getApplication(ctx, a.applicationStore, app.ApplicationId, a.logger)
if err != nil {
return gRPCStoreError(err, "get application")
}
if err := a.applicationStore.UpdateDeployTargets(ctx, app.ApplicationId, []string{application.PlatformProvider}); err != nil {
return gRPCStoreError(err, "update application")
}
return nil
}

// requireAPIKey checks the existence of an API key inside the given context
// and ensures that it has enough permissions for the give role.
func requireAPIKey(ctx context.Context, role model.APIKey_Role, logger *zap.Logger) (*model.APIKey, error) {
Expand Down
Loading