From e25b0fcb471bef55a58602c6657624be5a17c094 Mon Sep 17 00:00:00 2001 From: Tanmoy Sarkar <57363826+tanmoysrt@users.noreply.github.com> Date: Mon, 19 Feb 2024 02:17:31 +0530 Subject: [PATCH] feat: remove graphql endpoints and use only rest api for restore (#275) (cherry picked from commit d28feaebe2ea1dca3d857a67c3e9c836a96cd2b3) --- docs/rest_api.md | 6 +- .../core/pv_restore.operations.go | 2 +- swiftwave_service/core/types.go | 7 +- .../graphql/application.resolvers.go | 2 +- swiftwave_service/graphql/base.resolvers.go | 2 +- .../graphql/deployment.resolvers.go | 2 +- .../graphql/deployment_log.resolvers.go | 2 +- .../docker_config_generator.resolvers.go | 2 +- swiftwave_service/graphql/domain.resolvers.go | 2 +- swiftwave_service/graphql/generated.go | 200 ------------------ .../graphql/git_credential.resolvers.go | 2 +- .../image_registry_credential.resolvers.go | 2 +- .../graphql/ingress_rule.resolvers.go | 2 +- swiftwave_service/graphql/model/models_gen.go | 10 +- .../graphql/persistent_volume.resolvers.go | 2 +- .../persistent_volume_backup.resolvers.go | 2 +- .../persistent_volume_binding.resolvers.go | 2 +- .../persistent_volume_restore.resolvers.go | 44 +--- .../graphql/redirect_rule.resolvers.go | 2 +- .../graphql/runtime_log.resolvers.go | 2 +- .../schema/persistent_volume_restore.graphqls | 3 - .../graphql/user.graphqls.resolvers.go | 2 +- swiftwave_service/rest/persistent_volume.go | 60 ++++-- swiftwave_service/rest/server.go | 2 +- .../cancel_pending_tasks_local_queue.go | 2 + .../worker/process_pv_restore_request.go | 2 +- 26 files changed, 79 insertions(+), 289 deletions(-) diff --git a/docs/rest_api.md b/docs/rest_api.md index c047a38ad0..b42d2bd2ca 100644 --- a/docs/rest_api.md +++ b/docs/rest_api.md @@ -103,8 +103,8 @@ For other part, we are using the GraphQL API. Check the [GraphQL API Documentati --- -### Upload File for Restore API -**POST** /persistent-volume/restore//upload +### Upload File and Start Restore Job API +**POST** /persistent-volume/:id/restore **Form Data** @@ -117,7 +117,7 @@ For other part, we are using the GraphQL API. Check the [GraphQL API Documentati **200 OK** ```json { - "message": "file uploaded successfully, you can now start the restore process", + "message": "Restore job has been enqueued. You can check the status of the restore job in restore panel" } ``` diff --git a/swiftwave_service/core/pv_restore.operations.go b/swiftwave_service/core/pv_restore.operations.go index fc0c610625..091c964067 100644 --- a/swiftwave_service/core/pv_restore.operations.go +++ b/swiftwave_service/core/pv_restore.operations.go @@ -37,7 +37,7 @@ func (persistentVolumeRestore *PersistentVolumeRestore) Update(ctx context.Conte return err } // delete the file - if persistentVolumeRestore.File != "" && persistentVolumeRestore.Status != RestoreUploaded { + if persistentVolumeRestore.File != "" && persistentVolumeRestore.Status != RestorePending { err = os.Remove(filepath.Join(dataDir, persistentVolumeRestore.File)) if err != nil { log.Println("error deleting restore file: ", err) diff --git a/swiftwave_service/core/types.go b/swiftwave_service/core/types.go index f439815d6d..0e88bb06d2 100644 --- a/swiftwave_service/core/types.go +++ b/swiftwave_service/core/types.go @@ -147,8 +147,7 @@ const ( type RestoreStatus string const ( - RestorePending RestoreStatus = "pending" - RestoreUploaded RestoreStatus = "uploaded" - RestoreFailed RestoreStatus = "failed" - RestoreSuccess RestoreStatus = "success" + RestorePending RestoreStatus = "pending" + RestoreFailed RestoreStatus = "failed" + RestoreSuccess RestoreStatus = "success" ) diff --git a/swiftwave_service/graphql/application.resolvers.go b/swiftwave_service/graphql/application.resolvers.go index 5f59e65cdc..7a450bd6bb 100644 --- a/swiftwave_service/graphql/application.resolvers.go +++ b/swiftwave_service/graphql/application.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/base.resolvers.go b/swiftwave_service/graphql/base.resolvers.go index f232e01b9f..dee22a4884 100644 --- a/swiftwave_service/graphql/base.resolvers.go +++ b/swiftwave_service/graphql/base.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 // Mutation returns MutationResolver implementation. func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} } diff --git a/swiftwave_service/graphql/deployment.resolvers.go b/swiftwave_service/graphql/deployment.resolvers.go index e940624c01..32421d329b 100644 --- a/swiftwave_service/graphql/deployment.resolvers.go +++ b/swiftwave_service/graphql/deployment.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/deployment_log.resolvers.go b/swiftwave_service/graphql/deployment_log.resolvers.go index 57a8364477..3cf6847eed 100644 --- a/swiftwave_service/graphql/deployment_log.resolvers.go +++ b/swiftwave_service/graphql/deployment_log.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/docker_config_generator.resolvers.go b/swiftwave_service/graphql/docker_config_generator.resolvers.go index dbe80cfa5d..8be279c98f 100644 --- a/swiftwave_service/graphql/docker_config_generator.resolvers.go +++ b/swiftwave_service/graphql/docker_config_generator.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/domain.resolvers.go b/swiftwave_service/graphql/domain.resolvers.go index 30084ea09c..4578227c0a 100644 --- a/swiftwave_service/graphql/domain.resolvers.go +++ b/swiftwave_service/graphql/domain.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/generated.go b/swiftwave_service/graphql/generated.go index 58fdb411a8..e48e8ddcc6 100644 --- a/swiftwave_service/graphql/generated.go +++ b/swiftwave_service/graphql/generated.go @@ -203,13 +203,11 @@ type ComplexityRoot struct { DeletePersistentVolumeRestoresByPersistentVolumeID func(childComplexity int, persistentVolumeID uint) int DeleteRedirectRule func(childComplexity int, id uint) int DeleteUser func(childComplexity int, id uint) int - InitiatePersistentVolumeRestore func(childComplexity int, input model.PersistentVolumeRestoreInput) int IssueSsl func(childComplexity int, id uint) int RebuildApplication func(childComplexity int, id string) int RegenerateWebhookToken func(childComplexity int, id string) int RemoveDomain func(childComplexity int, id uint) int RestartApplication func(childComplexity int, id string) int - StartPersistentVolumeRestore func(childComplexity int, id uint) int UpdateApplication func(childComplexity int, id string, input model.ApplicationInput) int UpdateGitCredential func(childComplexity int, id uint, input model.GitCredentialInput) int UpdateImageRegistryCredential func(childComplexity int, id uint, input model.ImageRegistryCredentialInput) int @@ -368,8 +366,6 @@ type MutationResolver interface { BackupPersistentVolume(ctx context.Context, input model.PersistentVolumeBackupInput) (*model.PersistentVolumeBackup, error) DeletePersistentVolumeBackup(ctx context.Context, id uint) (bool, error) DeletePersistentVolumeBackupsByPersistentVolumeID(ctx context.Context, persistentVolumeID uint) (bool, error) - InitiatePersistentVolumeRestore(ctx context.Context, input model.PersistentVolumeRestoreInput) (*model.PersistentVolumeRestore, error) - StartPersistentVolumeRestore(ctx context.Context, id uint) (bool, error) DeletePersistentVolumeRestore(ctx context.Context, id uint) (bool, error) DeletePersistentVolumeRestoresByPersistentVolumeID(ctx context.Context, persistentVolumeID uint) (bool, error) CreateRedirectRule(ctx context.Context, input model.RedirectRuleInput) (*model.RedirectRule, error) @@ -1305,18 +1301,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Mutation.DeleteUser(childComplexity, args["id"].(uint)), true - case "Mutation.initiatePersistentVolumeRestore": - if e.complexity.Mutation.InitiatePersistentVolumeRestore == nil { - break - } - - args, err := ec.field_Mutation_initiatePersistentVolumeRestore_args(context.TODO(), rawArgs) - if err != nil { - return 0, false - } - - return e.complexity.Mutation.InitiatePersistentVolumeRestore(childComplexity, args["input"].(model.PersistentVolumeRestoreInput)), true - case "Mutation.issueSSL": if e.complexity.Mutation.IssueSsl == nil { break @@ -1377,18 +1361,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Mutation.RestartApplication(childComplexity, args["id"].(string)), true - case "Mutation.startPersistentVolumeRestore": - if e.complexity.Mutation.StartPersistentVolumeRestore == nil { - break - } - - args, err := ec.field_Mutation_startPersistentVolumeRestore_args(context.TODO(), rawArgs) - if err != nil { - return 0, false - } - - return e.complexity.Mutation.StartPersistentVolumeRestore(childComplexity, args["id"].(uint)), true - case "Mutation.updateApplication": if e.complexity.Mutation.UpdateApplication == nil { break @@ -2487,21 +2459,6 @@ func (ec *executionContext) field_Mutation_deleteUser_args(ctx context.Context, return args, nil } -func (ec *executionContext) field_Mutation_initiatePersistentVolumeRestore_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { - var err error - args := map[string]interface{}{} - var arg0 model.PersistentVolumeRestoreInput - if tmp, ok := rawArgs["input"]; ok { - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("input")) - arg0, err = ec.unmarshalNPersistentVolumeRestoreInput2githubᚗcomᚋswiftwaveᚑorgᚋswiftwaveᚋswiftwave_serviceᚋgraphqlᚋmodelᚐPersistentVolumeRestoreInput(ctx, tmp) - if err != nil { - return nil, err - } - } - args["input"] = arg0 - return args, nil -} - func (ec *executionContext) field_Mutation_issueSSL_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} @@ -2577,21 +2534,6 @@ func (ec *executionContext) field_Mutation_restartApplication_args(ctx context.C return args, nil } -func (ec *executionContext) field_Mutation_startPersistentVolumeRestore_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { - var err error - args := map[string]interface{}{} - var arg0 uint - if tmp, ok := rawArgs["id"]; ok { - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) - arg0, err = ec.unmarshalNUint2uint(ctx, tmp) - if err != nil { - return nil, err - } - } - args["id"] = arg0 - return args, nil -} - func (ec *executionContext) field_Mutation_updateApplication_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} @@ -8589,125 +8531,6 @@ func (ec *executionContext) fieldContext_Mutation_deletePersistentVolumeBackupsB return fc, nil } -func (ec *executionContext) _Mutation_initiatePersistentVolumeRestore(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_Mutation_initiatePersistentVolumeRestore(ctx, field) - if err != nil { - return graphql.Null - } - ctx = graphql.WithFieldContext(ctx, fc) - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return ec.resolvers.Mutation().InitiatePersistentVolumeRestore(rctx, fc.Args["input"].(model.PersistentVolumeRestoreInput)) - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - return graphql.Null - } - res := resTmp.(*model.PersistentVolumeRestore) - fc.Result = res - return ec.marshalOPersistentVolumeRestore2ᚖgithubᚗcomᚋswiftwaveᚑorgᚋswiftwaveᚋswiftwave_serviceᚋgraphqlᚋmodelᚐPersistentVolumeRestore(ctx, field.Selections, res) -} - -func (ec *executionContext) fieldContext_Mutation_initiatePersistentVolumeRestore(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { - fc = &graphql.FieldContext{ - Object: "Mutation", - Field: field, - IsMethod: true, - IsResolver: true, - Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - switch field.Name { - case "id": - return ec.fieldContext_PersistentVolumeRestore_id(ctx, field) - case "type": - return ec.fieldContext_PersistentVolumeRestore_type(ctx, field) - case "status": - return ec.fieldContext_PersistentVolumeRestore_status(ctx, field) - case "createdAt": - return ec.fieldContext_PersistentVolumeRestore_createdAt(ctx, field) - case "completedAt": - return ec.fieldContext_PersistentVolumeRestore_completedAt(ctx, field) - } - return nil, fmt.Errorf("no field named %q was found under type PersistentVolumeRestore", field.Name) - }, - } - defer func() { - if r := recover(); r != nil { - err = ec.Recover(ctx, r) - ec.Error(ctx, err) - } - }() - ctx = graphql.WithFieldContext(ctx, fc) - if fc.Args, err = ec.field_Mutation_initiatePersistentVolumeRestore_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { - ec.Error(ctx, err) - return fc, err - } - return fc, nil -} - -func (ec *executionContext) _Mutation_startPersistentVolumeRestore(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_Mutation_startPersistentVolumeRestore(ctx, field) - if err != nil { - return graphql.Null - } - ctx = graphql.WithFieldContext(ctx, fc) - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return ec.resolvers.Mutation().StartPersistentVolumeRestore(rctx, fc.Args["id"].(uint)) - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - if !graphql.HasFieldError(ctx, fc) { - ec.Errorf(ctx, "must not be null") - } - return graphql.Null - } - res := resTmp.(bool) - fc.Result = res - return ec.marshalNBoolean2bool(ctx, field.Selections, res) -} - -func (ec *executionContext) fieldContext_Mutation_startPersistentVolumeRestore(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { - fc = &graphql.FieldContext{ - Object: "Mutation", - Field: field, - IsMethod: true, - IsResolver: true, - Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Boolean does not have child fields") - }, - } - defer func() { - if r := recover(); r != nil { - err = ec.Recover(ctx, r) - ec.Error(ctx, err) - } - }() - ctx = graphql.WithFieldContext(ctx, fc) - if fc.Args, err = ec.field_Mutation_startPersistentVolumeRestore_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { - ec.Error(ctx, err) - return fc, err - } - return fc, nil -} - func (ec *executionContext) _Mutation_deletePersistentVolumeRestore(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Mutation_deletePersistentVolumeRestore(ctx, field) if err != nil { @@ -16779,17 +16602,6 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet) if out.Values[i] == graphql.Null { out.Invalids++ } - case "initiatePersistentVolumeRestore": - out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { - return ec._Mutation_initiatePersistentVolumeRestore(ctx, field) - }) - case "startPersistentVolumeRestore": - out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { - return ec._Mutation_startPersistentVolumeRestore(ctx, field) - }) - if out.Values[i] == graphql.Null { - out.Invalids++ - } case "deletePersistentVolumeRestore": out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { return ec._Mutation_deletePersistentVolumeRestore(ctx, field) @@ -19322,11 +19134,6 @@ func (ec *executionContext) marshalNPersistentVolumeRestore2ᚖgithubᚗcomᚋsw return ec._PersistentVolumeRestore(ctx, sel, v) } -func (ec *executionContext) unmarshalNPersistentVolumeRestoreInput2githubᚗcomᚋswiftwaveᚑorgᚋswiftwaveᚋswiftwave_serviceᚋgraphqlᚋmodelᚐPersistentVolumeRestoreInput(ctx context.Context, v interface{}) (model.PersistentVolumeRestoreInput, error) { - res, err := ec.unmarshalInputPersistentVolumeRestoreInput(ctx, v) - return res, graphql.ErrorOnPath(ctx, err) -} - func (ec *executionContext) unmarshalNPersistentVolumeRestoreStatus2githubᚗcomᚋswiftwaveᚑorgᚋswiftwaveᚋswiftwave_serviceᚋgraphqlᚋmodelᚐPersistentVolumeRestoreStatus(ctx context.Context, v interface{}) (model.PersistentVolumeRestoreStatus, error) { var res model.PersistentVolumeRestoreStatus err := res.UnmarshalGQL(v) @@ -20017,13 +19824,6 @@ func (ec *executionContext) marshalOPersistentVolumeBackup2ᚖgithubᚗcomᚋswi return ec._PersistentVolumeBackup(ctx, sel, v) } -func (ec *executionContext) marshalOPersistentVolumeRestore2ᚖgithubᚗcomᚋswiftwaveᚑorgᚋswiftwaveᚋswiftwave_serviceᚋgraphqlᚋmodelᚐPersistentVolumeRestore(ctx context.Context, sel ast.SelectionSet, v *model.PersistentVolumeRestore) graphql.Marshaler { - if v == nil { - return graphql.Null - } - return ec._PersistentVolumeRestore(ctx, sel, v) -} - func (ec *executionContext) unmarshalOString2ᚖstring(ctx context.Context, v interface{}) (*string, error) { if v == nil { return nil, nil diff --git a/swiftwave_service/graphql/git_credential.resolvers.go b/swiftwave_service/graphql/git_credential.resolvers.go index ba4f965142..404813f70c 100644 --- a/swiftwave_service/graphql/git_credential.resolvers.go +++ b/swiftwave_service/graphql/git_credential.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/image_registry_credential.resolvers.go b/swiftwave_service/graphql/image_registry_credential.resolvers.go index c57e1e42cd..da3e14d948 100644 --- a/swiftwave_service/graphql/image_registry_credential.resolvers.go +++ b/swiftwave_service/graphql/image_registry_credential.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/ingress_rule.resolvers.go b/swiftwave_service/graphql/ingress_rule.resolvers.go index f0eb14aed6..72d5f1a3ea 100644 --- a/swiftwave_service/graphql/ingress_rule.resolvers.go +++ b/swiftwave_service/graphql/ingress_rule.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/model/models_gen.go b/swiftwave_service/graphql/model/models_gen.go index 3ba3af8725..6b4378e5f0 100644 --- a/swiftwave_service/graphql/model/models_gen.go +++ b/swiftwave_service/graphql/model/models_gen.go @@ -670,22 +670,20 @@ func (e PersistentVolumeBackupType) MarshalGQL(w io.Writer) { type PersistentVolumeRestoreStatus string const ( - PersistentVolumeRestoreStatusPending PersistentVolumeRestoreStatus = "pending" - PersistentVolumeRestoreStatusUploaded PersistentVolumeRestoreStatus = "uploaded" - PersistentVolumeRestoreStatusFailed PersistentVolumeRestoreStatus = "failed" - PersistentVolumeRestoreStatusSuccess PersistentVolumeRestoreStatus = "success" + PersistentVolumeRestoreStatusPending PersistentVolumeRestoreStatus = "pending" + PersistentVolumeRestoreStatusFailed PersistentVolumeRestoreStatus = "failed" + PersistentVolumeRestoreStatusSuccess PersistentVolumeRestoreStatus = "success" ) var AllPersistentVolumeRestoreStatus = []PersistentVolumeRestoreStatus{ PersistentVolumeRestoreStatusPending, - PersistentVolumeRestoreStatusUploaded, PersistentVolumeRestoreStatusFailed, PersistentVolumeRestoreStatusSuccess, } func (e PersistentVolumeRestoreStatus) IsValid() bool { switch e { - case PersistentVolumeRestoreStatusPending, PersistentVolumeRestoreStatusUploaded, PersistentVolumeRestoreStatusFailed, PersistentVolumeRestoreStatusSuccess: + case PersistentVolumeRestoreStatusPending, PersistentVolumeRestoreStatusFailed, PersistentVolumeRestoreStatusSuccess: return true } return false diff --git a/swiftwave_service/graphql/persistent_volume.resolvers.go b/swiftwave_service/graphql/persistent_volume.resolvers.go index fbb79dfcc7..4701044407 100644 --- a/swiftwave_service/graphql/persistent_volume.resolvers.go +++ b/swiftwave_service/graphql/persistent_volume.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/persistent_volume_backup.resolvers.go b/swiftwave_service/graphql/persistent_volume_backup.resolvers.go index 3195a23342..9b07db79c1 100644 --- a/swiftwave_service/graphql/persistent_volume_backup.resolvers.go +++ b/swiftwave_service/graphql/persistent_volume_backup.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/persistent_volume_binding.resolvers.go b/swiftwave_service/graphql/persistent_volume_binding.resolvers.go index 7548a380ac..c5cd7e0bf2 100644 --- a/swiftwave_service/graphql/persistent_volume_binding.resolvers.go +++ b/swiftwave_service/graphql/persistent_volume_binding.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/persistent_volume_restore.resolvers.go b/swiftwave_service/graphql/persistent_volume_restore.resolvers.go index e642def26a..4c4ff8a418 100644 --- a/swiftwave_service/graphql/persistent_volume_restore.resolvers.go +++ b/swiftwave_service/graphql/persistent_volume_restore.resolvers.go @@ -2,56 +2,14 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" - "errors" - "fmt" - "log" "github.com/swiftwave-org/swiftwave/swiftwave_service/core" - "github.com/swiftwave-org/swiftwave/swiftwave_service/graphql/model" ) -// InitiatePersistentVolumeRestore is the resolver for the initiatePersistentVolumeRestore field. -func (r *mutationResolver) InitiatePersistentVolumeRestore(ctx context.Context, input model.PersistentVolumeRestoreInput) (*model.PersistentVolumeRestore, error) { - // fetch the persistent volume - var persistentVolume core.PersistentVolume - err := persistentVolume.FindById(ctx, r.ServiceManager.DbClient, input.PersistentVolumeID) - if err != nil { - return nil, err - } - // create a new persistent volume restore - record := persistentVolumeRestoreInputToDatabaseObject(&input) - err = record.Create(ctx, r.ServiceManager.DbClient) - if err != nil { - return nil, err - } - // return the created persistent volume restore - return persistentVolumeRestoreToGraphqlObject(record), nil -} - -// StartPersistentVolumeRestore is the resolver for the startPersistentVolumeRestore field. -func (r *mutationResolver) StartPersistentVolumeRestore(ctx context.Context, id uint) (bool, error) { - // fetch the persistent volume restore - var record core.PersistentVolumeRestore - err := record.FindById(ctx, r.ServiceManager.DbClient, id) - if err != nil { - return false, err - } - if record.Status != core.RestoreUploaded { - return false, fmt.Errorf("restoration file has not been uploaded") - } - // enqueue the restore job - err = r.WorkerManager.EnqueuePersistentVolumeRestoreRequest(record.ID) - if err != nil { - log.Println(err) - return false, errors.New("failed to enqueue restore job ! try again") - } - return true, nil -} - // DeletePersistentVolumeRestore is the resolver for the deletePersistentVolumeRestore field. func (r *mutationResolver) DeletePersistentVolumeRestore(ctx context.Context, id uint) (bool, error) { // fetch the persistent volume restore diff --git a/swiftwave_service/graphql/redirect_rule.resolvers.go b/swiftwave_service/graphql/redirect_rule.resolvers.go index 069085ca89..036f431a1e 100644 --- a/swiftwave_service/graphql/redirect_rule.resolvers.go +++ b/swiftwave_service/graphql/redirect_rule.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/graphql/runtime_log.resolvers.go b/swiftwave_service/graphql/runtime_log.resolvers.go index 5853b98b46..e22a69b34b 100644 --- a/swiftwave_service/graphql/runtime_log.resolvers.go +++ b/swiftwave_service/graphql/runtime_log.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "bufio" diff --git a/swiftwave_service/graphql/schema/persistent_volume_restore.graphqls b/swiftwave_service/graphql/schema/persistent_volume_restore.graphqls index f200778d20..2021de519d 100644 --- a/swiftwave_service/graphql/schema/persistent_volume_restore.graphqls +++ b/swiftwave_service/graphql/schema/persistent_volume_restore.graphqls @@ -4,7 +4,6 @@ enum PersistentVolumeRestoreType { enum PersistentVolumeRestoreStatus { pending - uploaded failed success } @@ -23,8 +22,6 @@ type PersistentVolumeRestore { } extend type Mutation { - initiatePersistentVolumeRestore(input: PersistentVolumeRestoreInput!): PersistentVolumeRestore - startPersistentVolumeRestore(id: Uint!): Boolean! deletePersistentVolumeRestore(id: Uint!): Boolean! deletePersistentVolumeRestoresByPersistentVolumeId(persistentVolumeId: Uint!): Boolean! } \ No newline at end of file diff --git a/swiftwave_service/graphql/user.graphqls.resolvers.go b/swiftwave_service/graphql/user.graphqls.resolvers.go index 383842044c..810156c6df 100644 --- a/swiftwave_service/graphql/user.graphqls.resolvers.go +++ b/swiftwave_service/graphql/user.graphqls.resolvers.go @@ -2,7 +2,7 @@ package graphql // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.43 +// Code generated by github.com/99designs/gqlgen version v0.17.44 import ( "context" diff --git a/swiftwave_service/rest/persistent_volume.go b/swiftwave_service/rest/persistent_volume.go index b20790215f..164ede908a 100644 --- a/swiftwave_service/rest/persistent_volume.go +++ b/swiftwave_service/rest/persistent_volume.go @@ -15,6 +15,7 @@ import ( "path/filepath" "strconv" "strings" + "time" ) // GET /persistent-volume/backup/:id/download @@ -98,35 +99,52 @@ func (server *Server) getPersistentVolumeBackupFileName(c echo.Context) error { return c.String(200, persistentVolumeBackup.File) } -// POST /persistent-volume/restore/:id/upload +// POST /persistent-volume/:id/restore func (server *Server) uploadPersistentVolumeRestoreFile(c echo.Context) error { + dbTx := server.ServiceManager.DbClient.Begin() + defer func() { + if r := recover(); r != nil { + dbTx.Rollback() + } + }() file, err := c.FormFile("file") if err != nil { return c.JSON(400, map[string]string{ "message": "file not found", }) } - // fetch persistent volume restore - idStr := c.Param("id") + persistentVolumeIdStr := c.Param("id") // convert id to uint - id, err := strconv.Atoi(idStr) + persistentVolumeId, err := strconv.Atoi(persistentVolumeIdStr) if err != nil { return c.JSON(400, map[string]string{ "message": "Invalid id", }) } - var persistentVolumeRestore core.PersistentVolumeRestore - err = persistentVolumeRestore.FindById(c.Request().Context(), server.ServiceManager.DbClient, uint(id)) + // fetch persistent volume + var persistentVolume core.PersistentVolume + err = persistentVolume.FindById(c.Request().Context(), *dbTx, uint(persistentVolumeId)) if err != nil { return c.JSON(500, map[string]string{ "message": "Internal server error", }) } - if persistentVolumeRestore.Status != core.RestorePending { - return c.JSON(400, map[string]string{ - "message": "Sorry, you can't upload file for this restore anymore", + // create a new persistent volume restore + persistentVolumeRestore := core.PersistentVolumeRestore{ + Type: core.LocalRestore, + Status: core.RestorePending, + PersistentVolumeID: persistentVolume.ID, + File: "", + CreatedAt: time.Now(), + CompletedAt: time.Now(), + } + err = persistentVolumeRestore.Create(c.Request().Context(), *dbTx) + if err != nil { + return c.JSON(500, map[string]string{ + "message": "Internal server error", }) } + // open file src, err := file.Open() if err != nil { return c.JSON(400, map[string]string{ @@ -170,14 +188,32 @@ func (server *Server) uploadPersistentVolumeRestoreFile(c echo.Context) error { } // update persistent volume restore persistentVolumeRestore.File = fileName - persistentVolumeRestore.Status = core.RestoreUploaded - err = persistentVolumeRestore.Update(c.Request().Context(), server.ServiceManager.DbClient, server.SystemConfig.ServiceConfig.DataDir) + err = persistentVolumeRestore.Update(c.Request().Context(), *dbTx, server.SystemConfig.ServiceConfig.DataDir) if err != nil { return c.JSON(500, map[string]string{ "message": "failed to update restore", }) } + // commit + err = dbTx.Commit().Error + if err != nil { + return c.JSON(500, map[string]string{ + "message": "failed to create restore", + }) + } + err = server.WorkerManager.EnqueuePersistentVolumeRestoreRequest(persistentVolumeRestore.ID) + if err != nil { + // mark restore as failed + persistentVolumeRestore.Status = core.RestoreFailed + err = persistentVolumeRestore.Update(c.Request().Context(), server.ServiceManager.DbClient, server.SystemConfig.ServiceConfig.DataDir) + if err != nil { + log.Println(err) + } + return c.JSON(500, map[string]string{ + "message": "failed to enqueue restore job", + }) + } return c.JSON(200, map[string]string{ - "message": "file uploaded successfully, you can now start the restore process", + "message": "Restore job has been enqueued. You can check the status of the restore job in restore panel", }) } diff --git a/swiftwave_service/rest/server.go b/swiftwave_service/rest/server.go index 3a227b09c7..ebe04ab580 100644 --- a/swiftwave_service/rest/server.go +++ b/swiftwave_service/rest/server.go @@ -21,7 +21,7 @@ func (server *Server) initiateProjectRoutes(e *echo.Echo) { // Initiating Routes for PersistentVolume e.GET("/persistent-volume/backup/:id/download", server.downloadPersistentVolumeBackup) e.GET("/persistent-volume/backup/:id/filename", server.getPersistentVolumeBackupFileName) - e.POST("/persistent-volume/restore/:id/upload", server.uploadPersistentVolumeRestoreFile) + e.POST("/persistent-volume/:id/restore", server.uploadPersistentVolumeRestoreFile) // Initiating Routes for Webhook e.Any("/webhook/redeploy-app/:app-id/:webhook-token", server.redeployApp) } diff --git a/swiftwave_service/worker/cancel_pending_tasks_local_queue.go b/swiftwave_service/worker/cancel_pending_tasks_local_queue.go index 33ebe5ead7..af9ce21b6e 100644 --- a/swiftwave_service/worker/cancel_pending_tasks_local_queue.go +++ b/swiftwave_service/worker/cancel_pending_tasks_local_queue.go @@ -22,6 +22,8 @@ func CancelPendingTasksLocalQueue(config system_config.Config, manager core.Serv tx.Model(&core.RedirectRule{}).Where("status = ? OR status = ?", core.RedirectRuleStatusPending, core.RedirectRuleStatusDeleting).Update("status", core.RedirectRuleStatusFailed) // Mark all PersistentVolumeBackup > pending tasks as failed tx.Model(&core.PersistentVolumeBackup{}).Where("status = ?", core.BackupPending).Update("status", core.BackupFailed) + // Mark all PersistentVolumeRestore > uploaded tasks as failed + tx.Model(&core.PersistentVolumeRestore{}).Where("status = ?", core.RestorePending).Update("status", core.RestoreFailed) // commit err := tx.Commit().Error if err != nil { diff --git a/swiftwave_service/worker/process_pv_restore_request.go b/swiftwave_service/worker/process_pv_restore_request.go index aa10b0fb93..d4d6ed5ca7 100644 --- a/swiftwave_service/worker/process_pv_restore_request.go +++ b/swiftwave_service/worker/process_pv_restore_request.go @@ -16,7 +16,7 @@ func (m Manager) PersistentVolumeRestore(request PersistentVolumeRestoreRequest, return nil } // check status should be uploaded - if persistentVolumeRestore.Status != core.RestoreUploaded { + if persistentVolumeRestore.Status != core.RestorePending { return nil } // fetch persistent volume