openstack_snapshot_manager.py 1.48 KB
Newer Older
edems committed
1 2
from typing import Optional

adamtorok committed
3 4
from openstack.exceptions import ResourceNotFound

Bodor Máté committed
5 6
from interface_openstack.interface.storage.snapshot import Snapshot
from interface_openstack.interface.storage.snapshot_manager import SnapshotManager
Chif Gergő committed
7
from interface_openstack.implementation.utils.connection import OpenStackConnection
adamtorok committed
8 9


Chif Gergő committed
10
class OpenstackSnapshotManager(SnapshotManager, OpenStackConnection):
adamtorok committed
11 12

    @staticmethod
edems committed
13
    def os_snapshot_to_rc_snapshot(os_snapshot) -> Snapshot:
adamtorok committed
14
        return Snapshot(
adamtorok committed
15
            os_snapshot.id,
adamtorok committed
16 17 18 19
            os_snapshot.volume_id,
            os_snapshot.size,
            os_snapshot.status,
            os_snapshot.created_at
adamtorok committed
20 21
        )

edems committed
22
    def create_from_volume(self, id) -> Snapshot:
adamtorok committed
23 24 25 26
        os_snapshot = self.openstack.block_storage.create_snapshot(volume_id=id)

        return self.os_snapshot_to_rc_snapshot(os_snapshot)

edems committed
27
    def get(self, id) -> Optional[Snapshot]:
adamtorok committed
28 29 30 31
        try:
            os_snapshot = self.openstack.block_storage.get_snapshot(id)
        except ResourceNotFound:
            return None
adamtorok committed
32 33 34

        return self.os_snapshot_to_rc_snapshot(os_snapshot)

edems committed
35
    def list(self) -> []:
adamtorok committed
36 37 38 39 40 41 42
        snapshots = []

        for os_snapshot in self.openstack.block_storage.snapshots():
            snapshots.append(self.os_snapshot_to_rc_snapshot(os_snapshot))

        return snapshots

edems committed
43
    def delete(self, id) -> bool:
adamtorok committed
44 45 46 47 48 49
        try:
            self.openstack.block_storage.delete_snapshot(id)
        except ResourceNotFound:
            return False

        return True