|
1 | 1 | # This file is part of pycloudlib. See LICENSE file for license information.
|
2 | 2 | """Base class for all other clouds to provide consistent set of functions."""
|
3 | 3 |
|
| 4 | +import dataclasses |
4 | 5 | import enum
|
5 | 6 | import getpass
|
6 | 7 | import io
|
@@ -38,6 +39,32 @@ class ImageType(enum.Enum):
|
38 | 39 | PRO_FIPS = "Pro FIPS"
|
39 | 40 |
|
40 | 41 |
|
| 42 | +@dataclasses.dataclass |
| 43 | +class ImageInfo: |
| 44 | + """Dataclass that represents an image on any given cloud.""" |
| 45 | + |
| 46 | + id: str |
| 47 | + name: str |
| 48 | + |
| 49 | + def __str__(self): |
| 50 | + """Return a human readable string representation of the image.""" |
| 51 | + return f"{self.name} [id: {self.id}]" |
| 52 | + |
| 53 | + def __repr__(self): |
| 54 | + """Return a string representation of the image.""" |
| 55 | + return f"ImageInfo(id={self.id}, name={self.name})" |
| 56 | + |
| 57 | + def __eq__(self, other): |
| 58 | + """Check if two ImageInfo objects are equal.""" |
| 59 | + if not isinstance(other, ImageInfo): |
| 60 | + return False |
| 61 | + return self.id == other.id |
| 62 | + |
| 63 | + def __dict__(self): |
| 64 | + """Return a dictionary representation of the image.""" |
| 65 | + return {"id": self.id, "name": self.name} |
| 66 | + |
| 67 | + |
41 | 68 | class BaseCloud(ABC):
|
42 | 69 | """Base Cloud Class."""
|
43 | 70 |
|
@@ -371,3 +398,67 @@ def _get_ssh_keys(
|
371 | 398 | private_key_path=private_key_path,
|
372 | 399 | name=name,
|
373 | 400 | )
|
| 401 | + |
| 402 | + # all actual "Clouds" and not just substrates like LXD and QEMU should support this method |
| 403 | + def upload_local_file_to_cloud_storage( |
| 404 | + self, |
| 405 | + *, |
| 406 | + local_file_path: str, |
| 407 | + storage_name: str, |
| 408 | + remote_file_name: Optional[str] = None, |
| 409 | + ) -> str: |
| 410 | + """ |
| 411 | + Upload a file to a storage destination on the Cloud. |
| 412 | +
|
| 413 | + Args: |
| 414 | + local_file_path: The local file path of the image to upload. |
| 415 | + storage_name: The name of the storage destination on the Cloud to upload the file to. |
| 416 | + remote_file_name: The name of the file in the storage destination. If not provided, |
| 417 | + the base name of the local file path will be used. |
| 418 | +
|
| 419 | + Returns: |
| 420 | + str: URL of the uploaded file in the storage destination. |
| 421 | + """ |
| 422 | + raise NotImplementedError |
| 423 | + |
| 424 | + # most clouds except for like lxd should support this method |
| 425 | + def create_image_from_local_file( |
| 426 | + self, |
| 427 | + *, |
| 428 | + local_file_path: str, |
| 429 | + image_name: str, |
| 430 | + intermediary_storage_name: str, |
| 431 | + ): |
| 432 | + """ |
| 433 | + Upload local image file to storage on the Cloud and then create a custom image from it. |
| 434 | +
|
| 435 | + Args: |
| 436 | + local_file_path: The local file path of the image to upload. |
| 437 | + image_name: The name to upload the image as and to register. |
| 438 | + intermediary_storage_name: The intermediary storage destination on the Cloud to upload |
| 439 | + the file to before creating the image. |
| 440 | +
|
| 441 | + Returns: |
| 442 | + ImageInfo: Information about the created image. |
| 443 | + """ |
| 444 | + raise NotImplementedError |
| 445 | + |
| 446 | + # not all clouds will support this method - depends on how image creation is handled |
| 447 | + def _create_image_from_cloud_storage( |
| 448 | + self, |
| 449 | + *, |
| 450 | + image_name: str, |
| 451 | + remote_image_file_url: str, |
| 452 | + image_description: Optional[str] = None, |
| 453 | + ) -> ImageInfo: |
| 454 | + """ |
| 455 | + Register a custom image in the Cloud from a file in Cloud storage using its url. |
| 456 | +
|
| 457 | + Ideally, this url would be returned from the upload_local_file_to_cloud_storage method. |
| 458 | +
|
| 459 | + Args: |
| 460 | + image_name: The name the image will be created with. |
| 461 | + remote_image_file_url: The URL of the image file in the Cloud storage. |
| 462 | + image_description: (Optional) A description of the image. |
| 463 | + """ |
| 464 | + raise NotImplementedError |
0 commit comments