openstack_image_manager.py 1.5 KB
Newer Older
edems committed
1 2
from typing import Optional

adamtorok committed
3
from openstack.exceptions import ResourceNotFound
4

Bodor Máté committed
5 6
from interface_openstack.interface.image.image_manager import ImageManager
from interface_openstack.interface.image.image import Image
Chif Gergő committed
7
from interface_openstack.implementation.utils.connection import OpenStackConnection
8 9


Chif Gergő committed
10
class OpenstackImageManager(ImageManager, OpenStackConnection):
11 12

    @staticmethod
edems committed
13
    def os_image_to_rc_image(os_image) -> Image:
adamtorok committed
14
        return Image(
15 16
            os_image.id,
            os_image.name,
17 18
            os_image.disk_format,
            os_image.size
19 20
        )

edems committed
21
    def upload_file(self, name, path, format) -> Image:
adamtorok committed
22 23 24 25 26
        os_image = self.openstack.image.create_image(
            name=name,
            filename=path,
            disk_format=format
        )
27 28 29

        return self.os_image_to_rc_image(os_image)

edems committed
30
    def get(self, id) -> Optional[Image]:
adamtorok committed
31 32 33 34
        try:
            os_image = self.openstack.image.get_image(id)
        except ResourceNotFound:
            return None
35 36 37

        return self.os_image_to_rc_image(os_image)

38 39
    def download(self, id, path):
        return self.openstack.image.download_image(image=id, output=path)   
adamtorok committed
40

edems committed
41
    def delete(self, id) -> bool:
adamtorok committed
42 43 44 45 46 47
        try:
            self.openstack.image.delete_image(id)
        except ResourceNotFound:
            return False

        return True
48

edems committed
49
    def list(self) -> []:
50 51 52 53 54 55
        images = []

        for os_image in self.openstack.image.images():
            images.append(self.os_image_to_rc_image(os_image))

        return images