openstack_storage_manager.py 2.1 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.storage_manager import StorageManager
from interface_openstack.interface.storage.volume import Volume
Chif Gergő committed
7
from interface_openstack.implementation.utils.connection import OpenStackConnection
8 9


Chif Gergő committed
10
class OpenstackStorageManager(StorageManager, OpenStackConnection):
11 12

    @staticmethod
edems committed
13
    def os_volume_to_rc_volume(os_volume) -> Volume:
adamtorok committed
14
        return Volume(
adamtorok committed
15
            os_volume.id,
adamtorok committed
16
            os_volume.image_id,
adamtorok committed
17
            os_volume.size,
adamtorok committed
18 19 20
            os_volume.is_bootable,
            os_volume.status,
            os_volume.created_at
21 22
        )

edems committed
23
    def create(self, size) -> Volume:
adamtorok committed
24
        os_volume = self.openstack.block_storage.create_volume(
adamtorok committed
25 26 27 28 29
            size=size
        )

        return self.os_volume_to_rc_volume(os_volume)

edems committed
30
    def create_from_image(self, id, size, bootable) -> Volume:
adamtorok committed
31 32
        os_volume = self.openstack.block_storage.create_volume(
            image_id=id,
adamtorok committed
33 34 35 36 37 38
            size=size,
            bootable=bootable
        )

        return self.os_volume_to_rc_volume(os_volume)

edems committed
39
    def create_from_snapshot(self, id) -> Volume:
adamtorok committed
40 41 42
        os_volume = self.openstack.block_storage.create_volume(
            snapshot_id=id
        )
43

adamtorok committed
44
        return self.os_volume_to_rc_volume(os_volume)
45

edems committed
46
    def get(self, id) -> Optional[Volume]:
adamtorok committed
47 48 49 50
        try:
            os_volume = self.openstack.block_storage.get_volume(id)
        except ResourceNotFound:
            return None
adamtorok committed
51 52

        return self.os_volume_to_rc_volume(os_volume)
53

adamtorok committed
54
    def delete(self, id):
adamtorok committed
55 56 57 58 59 60
        try:
            self.openstack.block_storage.delete_volume(id)
        except ResourceNotFound:
            return False

        return True
61

edems committed
62
    def extend(self, id, size) -> bool:
edems committed
63 64 65 66 67 68 69
        try:
            self.openstack.block_storage.extend_volume(id, size)
        except ResourceNotFound:
            return False

        return True

edems committed
70
    def list(self) -> []:
adamtorok committed
71
        volumes = []
72

adamtorok committed
73 74
        for os_volume in self.openstack.block_storage.volumes():
            volumes.append(self.os_volume_to_rc_volume(os_volume))
75

adamtorok committed
76
        return volumes