fix(backups): Handle orphaned snapshots

restic-rewrite-api
Inex Code 2023-06-14 02:52:10 +03:00
parent f4df1f6a62
commit d356fad534
4 changed files with 33 additions and 16 deletions

View File

@ -38,7 +38,7 @@ class LocalBackupSecret:
@staticmethod
def exists() -> bool:
return redis.exists(REDIS_KEY)
return redis.exists(REDIS_KEY) == 1
@staticmethod
def _generate() -> str:

View File

@ -1,9 +0,0 @@
import datetime
import strawberry
@strawberry.type
class SnapshotInfo:
id: str
service_name: str
created_at: datetime.datetime

View File

@ -3,7 +3,6 @@ import typing
import strawberry
import datetime
from selfprivacy_api.graphql.common_types.dns import DnsRecord
from selfprivacy_api.graphql.common_types.backup_snapshot import SnapshotInfo
from selfprivacy_api.services import get_service_by_id, get_services_by_location
from selfprivacy_api.services import Service as ServiceInterface
@ -104,14 +103,14 @@ class Service:
return get_storage_usage(self)
@strawberry.field
def backup_snapshots(self) -> typing.Optional[typing.List[SnapshotInfo]]:
def backup_snapshots(self) -> typing.Optional[typing.List["SnapshotInfo"]]:
return None
@strawberry.type
class SnapshotInfo:
id: str
service: "Service"
service: Service
created_at: datetime.datetime

View File

@ -7,13 +7,20 @@ import strawberry
from selfprivacy_api.backup import Backups
from selfprivacy_api.backup.local_secret import LocalBackupSecret
from selfprivacy_api.graphql.queries.providers import BackupProvider
from selfprivacy_api.graphql.common_types.service import SnapshotInfo
from selfprivacy_api.graphql.common_types.service import (
Service,
ServiceStatusEnum,
SnapshotInfo,
service_to_graphql_service,
)
from selfprivacy_api.services import get_service_by_id
@strawberry.type
class BackupConfiguration:
provider: BackupProvider
# When server is lost, the app should have the key to decrypt backups on a new server
# When server is lost, the app should have the key to decrypt backups
# on a new server
encryption_key: str
# False when repo is not initialized and not ready to be used
is_initialized: bool
@ -39,11 +46,31 @@ class Backup:
@strawberry.field
def all_snapshots(self) -> typing.List[SnapshotInfo]:
if not Backups.is_initted():
return []
result = []
snapshots = Backups.get_all_snapshots()
for snap in snapshots:
service = get_service_by_id(snap.service_name)
if service is None:
service = Service(
id=snap.service_name,
display_name=f"{snap.service_name} (Orphaned)",
description="",
svg_icon="",
is_movable=False,
is_required=False,
is_enabled=False,
status=ServiceStatusEnum.OFF,
url=None,
dns_records=None,
)
else:
service = service_to_graphql_service(service)
graphql_snap = SnapshotInfo(
id=snap.id, service=snap.service_name, created_at=snap.created_at
id=snap.id,
service=service,
created_at=snap.created_at,
)
result.append(graphql_snap)
return result