OpenstackStorageManager.py 1.53 KB
Newer Older
adamtorok committed
1
import StorageManager
adamtorok committed
2 3 4
from openstack.exceptions import ResourceNotFound

from interface.storage.Volume import Volume
5 6


adamtorok committed
7
class OpenstackStorageManager(StorageManager.StorageManager):
8

adamtorok committed
9
    def __init__(self, openstack) -> None:
10
        super().__init__()
adamtorok committed
11 12

        self.openstack = openstack
13 14

    @staticmethod
adamtorok committed
15
    def os_volume_to_rc_volume(os_volume):
adamtorok committed
16
        return Volume(
adamtorok committed
17 18
            os_volume.id,
            os_volume.size,
adamtorok committed
19 20 21
            os_volume.is_bootable,
            os_volume.status,
            os_volume.created_at
22 23
        )

adamtorok committed
24
    def create(self, size, bootable):
adamtorok committed
25 26 27 28 29 30 31 32 33
        os_volume = self.openstack.block_storage.create_volume(
            size=size,
            bootable=bootable
        )

        return self.os_volume_to_rc_volume(os_volume)

    def create_from_snapshot(self, id):
        os_volume = self.openstack.block_storage.create_volume(snapshot_id=id)
34

adamtorok committed
35
        return self.os_volume_to_rc_volume(os_volume)
36 37

    def get(self, id):
adamtorok committed
38 39 40 41
        try:
            os_volume = self.openstack.block_storage.get_volume(id)
        except ResourceNotFound:
            return None
adamtorok committed
42 43

        return self.os_volume_to_rc_volume(os_volume)
44

adamtorok committed
45
    def delete(self, id):
adamtorok committed
46 47 48 49 50 51
        try:
            self.openstack.block_storage.delete_volume(id)
        except ResourceNotFound:
            return False

        return True
52 53

    def list(self):
adamtorok committed
54
        volumes = []
55

adamtorok committed
56 57
        for os_volume in self.openstack.block_storage.volumes():
            volumes.append(self.os_volume_to_rc_volume(os_volume))
58

adamtorok committed
59
        return volumes
60

adamtorok committed
61 62
    def resize(self, id):
        pass