feature(backups): init repo mutation

This commit is contained in:
Houkime 2023-05-29 15:34:26 +00:00
parent 0dc6f74754
commit 97e4c529f6
5 changed files with 89 additions and 12 deletions

View file

@ -29,6 +29,9 @@ class Backups:
Storage.store_testrepo_path(file_path) Storage.store_testrepo_path(file_path)
Storage.store_provider(provider) Storage.store_provider(provider)
def set_provider(provider: AbstractBackupProvider):
Storage.store_provider(provider)
@staticmethod @staticmethod
def get_last_backed_up(service: Service) -> Optional[datetime]: def get_last_backed_up(service: Service) -> Optional[datetime]:
"""Get a timezone-aware time of the last backup of a service""" """Get a timezone-aware time of the last backup of a service"""
@ -126,19 +129,21 @@ class Backups:
return Backups.lookup_provider() return Backups.lookup_provider()
@staticmethod @staticmethod
def set_provider(kind: str, login: str, key: str): def set_provider(kind: str, login: str, key: str, location: str, repo_id: str = ""):
provider = Backups.construct_provider(kind, login, key) provider = Backups.construct_provider(kind, login, key, location, id)
Storage.store_provider(provider) Storage.store_provider(provider)
@staticmethod @staticmethod
def construct_provider(kind: str, login: str, key: str): def construct_provider(
kind: str, login: str, key: str, location: str, repo_id: str = ""
):
provider_class = get_provider(BackupProvider[kind]) provider_class = get_provider(BackupProvider[kind])
if kind == "FILE": if kind == "FILE":
path = Storage.get_testrepo_path() path = Storage.get_testrepo_path()
return provider_class(path) return provider_class(path)
return provider_class(login=login, key=key) return provider_class(login=login, key=key, location=location, repo_id=repo_id)
@staticmethod @staticmethod
def reset(): def reset():
@ -169,17 +174,19 @@ class Backups:
if "backblaze" in user_data.keys(): if "backblaze" in user_data.keys():
account = user_data["backblaze"]["accountId"] account = user_data["backblaze"]["accountId"]
key = user_data["backblaze"]["accountKey"] key = user_data["backblaze"]["accountKey"]
location = user_data["backblaze"]["bucket"]
provider_string = "BACKBLAZE" provider_string = "BACKBLAZE"
return Backups.construct_provider( return Backups.construct_provider(
kind=provider_string, login=account, key=key kind=provider_string, login=account, key=key, location=location
) )
return None return None
account = user_data["backup"]["accountId"] account = user_data["backup"]["accountId"]
key = user_data["backup"]["accountKey"] key = user_data["backup"]["accountKey"]
provider_string = user_data["backup"]["provider"] provider_string = user_data["backup"]["provider"]
location = user_data["backup"]["bucket"]
return Backups.construct_provider( return Backups.construct_provider(
kind=provider_string, login=account, key=key kind=provider_string, login=account, key=key, location=location
) )
@staticmethod @staticmethod
@ -188,7 +195,11 @@ class Backups:
if provider_model is None: if provider_model is None:
return None return None
return Backups.construct_provider( return Backups.construct_provider(
provider_model.kind, provider_model.login, provider_model.key provider_model.kind,
provider_model.login,
provider_model.key,
provider_model.location,
provider_model.repo_id,
) )
@staticmethod @staticmethod
@ -214,9 +225,12 @@ class Backups:
Jobs.update(job, status=JobStatus.FINISHED) Jobs.update(job, status=JobStatus.FINISHED)
@staticmethod @staticmethod
def init_repo(service: Service): def init_repo(service: Optional[Service] = None):
if service is not None:
repo_name = service.get_id() repo_name = service.get_id()
Backups.provider().backuper.init(repo_name) Backups.provider().backuper.init(repo_name)
if service is not None:
Storage.mark_as_init(service) Storage.mark_as_init(service)
@staticmethod @staticmethod

View file

@ -12,7 +12,11 @@ class AbstractBackupProvider(ABC):
def backuper(self) -> AbstractBackuper: def backuper(self) -> AbstractBackuper:
raise NotImplementedError raise NotImplementedError
def __init__(self, login="", key="", location=""): def __init__(self, login="", key="", location="", repo_id=""):
self.backuper.set_creds(login, key, location) self.backuper.set_creds(login, key, location)
self.login = login self.login = login
self.key = key self.key = key
self.location = location
# We do not need to do anything with this one
# Just remember in case the app forgets
self.repo_id = repo_id

View file

@ -146,7 +146,11 @@ class Storage:
redis, redis,
REDIS_PROVIDER_KEY, REDIS_PROVIDER_KEY,
BackupProviderModel( BackupProviderModel(
kind=get_kind(provider), login=provider.login, key=provider.key kind=get_kind(provider),
login=provider.login,
key=provider.key,
location=provider.location,
repo_id=provider.repo_id,
), ),
) )

View file

@ -0,0 +1,53 @@
import datetime
import typing
import strawberry
from strawberry.types import Info
from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.graphql.mutations.mutation_interface import (
GenericMutationReturn,
MutationReturnInterface,
)
from selfprivacy_api.graphql.queries.backup import BackupConfiguration
from selfprivacy_api.graphql.queries.providers import BackupProvider
from selfprivacy_api.backup import Backups
@strawberry.input
class InitializeRepositoryInput:
"""Initialize repository input"""
provider: BackupProvider
# The following field may become optional for other providers?
# Backblaze takes bucket id and name
location_id: str
location_name: str
# Key ID and key for Backblaze
login: str
password: str
@strawberry.type
class GenericBackupConfigReturn(MutationReturnInterface):
"""Generic backup config return"""
configuration: typing.Optional[BackupConfiguration]
@strawberry.type
class BackupMutations:
@strawberry.mutation(permission_classes=[IsAuthenticated])
def initialize_repository(
self, repository: InitializeRepositoryInput
) -> GenericBackupConfigReturn:
"""Initialize a new repository"""
provider = Backups.construct_provider(
kind=repository.provider,
login=repository.login,
key=repository.password,
location=repository.location_name,
repo_id=repository.location_id,
)
Backups.set_provider(provider)
Backups.init_repo()

View file

@ -7,3 +7,5 @@ class BackupProviderModel(BaseModel):
kind: str kind: str
login: str login: str
key: str key: str
location: str
repo_id: str # for app usage, not for us