-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add HTTP API endpoints for online restore
- Loading branch information
Showing
4 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"mime/multipart" | ||
|
||
"github.com/swaggest/usecase" | ||
"github.com/swaggest/usecase/status" | ||
|
||
"link-society.com/flowg/internal/models" | ||
apiUtils "link-society.com/flowg/internal/utils/api" | ||
|
||
"link-society.com/flowg/internal/storage/auth" | ||
) | ||
|
||
type RestoreAuthRequest struct { | ||
Backup multipart.File `formData:"backup"` | ||
} | ||
|
||
type RestoreAuthResponse struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
func RestoreAuthUsecase(authStorage *auth.Storage) usecase.Interactor { | ||
u := usecase.NewInteractor( | ||
apiUtils.RequireScopeApiDecorator( | ||
authStorage, | ||
models.SCOPE_WRITE_ACLS, | ||
func( | ||
ctx context.Context, | ||
req RestoreAuthRequest, | ||
resp *RestoreAuthResponse, | ||
) error { | ||
defer req.Backup.Close() | ||
|
||
err := authStorage.Restore(ctx, req.Backup) | ||
if err != nil { | ||
slog.ErrorContext( | ||
ctx, | ||
"Failed to restore authentication database", | ||
slog.String("channel", "api"), | ||
slog.String("error", err.Error()), | ||
) | ||
|
||
return status.Wrap(err, status.Internal) | ||
} | ||
|
||
resp.Success = true | ||
|
||
return nil | ||
}, | ||
), | ||
) | ||
|
||
u.SetName("restore_auth") | ||
u.SetTitle("Restore Authentication Database") | ||
u.SetDescription("Upload a full snapshot of the authentication database.") | ||
u.SetTags("backup") | ||
|
||
u.SetExpectedErrors(status.Unauthenticated, status.PermissionDenied, status.Internal) | ||
|
||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"mime/multipart" | ||
|
||
"github.com/swaggest/usecase" | ||
"github.com/swaggest/usecase/status" | ||
|
||
"link-society.com/flowg/internal/models" | ||
apiUtils "link-society.com/flowg/internal/utils/api" | ||
|
||
"link-society.com/flowg/internal/storage/auth" | ||
"link-society.com/flowg/internal/storage/config" | ||
) | ||
|
||
type RestoreConfigRequest struct { | ||
Backup multipart.File `formData:"backup"` | ||
} | ||
|
||
type RestoreConfigResponse struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
func RestoreConfigUsecase( | ||
authStorage *auth.Storage, | ||
configStorage *config.Storage, | ||
) usecase.Interactor { | ||
u := usecase.NewInteractor( | ||
apiUtils.RequireScopesApiDecorator( | ||
authStorage, | ||
[]models.Scope{ | ||
models.SCOPE_WRITE_PIPELINES, | ||
models.SCOPE_WRITE_TRANSFORMERS, | ||
models.SCOPE_WRITE_ALERTS, | ||
}, | ||
func( | ||
ctx context.Context, | ||
req RestoreConfigRequest, | ||
resp *RestoreConfigResponse, | ||
) error { | ||
defer req.Backup.Close() | ||
|
||
err := configStorage.Restore(ctx, req.Backup) | ||
if err != nil { | ||
slog.ErrorContext( | ||
ctx, | ||
"Failed to restore configuration database", | ||
slog.String("channel", "api"), | ||
slog.String("error", err.Error()), | ||
) | ||
|
||
return status.Wrap(err, status.Internal) | ||
} | ||
|
||
resp.Success = true | ||
|
||
return nil | ||
}, | ||
), | ||
) | ||
|
||
u.SetName("restore_config") | ||
u.SetTitle("Restore Configuration") | ||
u.SetDescription("Upload a full snapshot of the configuration database.") | ||
u.SetTags("backup") | ||
|
||
u.SetExpectedErrors(status.Unauthenticated, status.PermissionDenied, status.Internal) | ||
|
||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"mime/multipart" | ||
|
||
"github.com/swaggest/usecase" | ||
"github.com/swaggest/usecase/status" | ||
|
||
"link-society.com/flowg/internal/models" | ||
apiUtils "link-society.com/flowg/internal/utils/api" | ||
|
||
"link-society.com/flowg/internal/storage/auth" | ||
"link-society.com/flowg/internal/storage/log" | ||
) | ||
|
||
type RestoreLogsRequest struct { | ||
Backup multipart.File `json:"backup"` | ||
} | ||
|
||
type RestoreLogsResponse struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
func RestoreLogsUsecase( | ||
authStorage *auth.Storage, | ||
logStorage *log.Storage, | ||
) usecase.Interactor { | ||
u := usecase.NewInteractor( | ||
apiUtils.RequireScopeApiDecorator( | ||
authStorage, | ||
models.SCOPE_WRITE_STREAMS, | ||
func( | ||
ctx context.Context, | ||
req RestoreLogsRequest, | ||
resp *RestoreLogsResponse, | ||
) error { | ||
defer req.Backup.Close() | ||
|
||
err := logStorage.Restore(ctx, req.Backup) | ||
if err != nil { | ||
slog.ErrorContext( | ||
ctx, | ||
"Failed to restore logs database", | ||
slog.String("channel", "api"), | ||
slog.String("error", err.Error()), | ||
) | ||
|
||
return status.Wrap(err, status.Internal) | ||
} | ||
|
||
return nil | ||
}, | ||
), | ||
) | ||
|
||
u.SetName("restore_logs") | ||
u.SetTitle("Restore Logs Database") | ||
u.SetDescription("Upload a full snapshot of the logs database.") | ||
u.SetTags("backup") | ||
|
||
u.SetExpectedErrors(status.Unauthenticated, status.PermissionDenied, status.Internal) | ||
|
||
return u | ||
} |