[docs]@staticmethoddeffrom_glance_image(glance_image)->"Image":"""Convert a glance image object to an Image object. Args: glance_image: The glance image object. Returns: Image: The Image object. """if"build-repo"inglance_image:returnImage(uuid=glance_image.id,created_at=glance_image.created_at,is_chameleon_supported=(glance_image["build-repo"]=="https://github.com/ChameleonCloud/cc-images"),name=glance_image.name,)else:returnImage(uuid=glance_image.id,created_at=glance_image.created_at,is_chameleon_supported=False,name=glance_image.name,)
[docs]deflist_images(is_chameleon_supported:Optional[bool]=False)->List[Image]:"""List all images available at the current site, filtered by support status. Args: is_chameleon_supported (bool, optional): Filter images by Chameleon support. Defaults to True. Returns: List[Image]: A list of Image objects. """ifis_chameleon_supported:glance_images=glance().images.list(filters={"build-repo":"https://github.com/ChameleonCloud/cc-images"})else:glance_images=glance().images.list()return[Image.from_glance_image(image)forimageinglance_images]
[docs]defget_image(name:str)->Image:"""Get an image by its name. Args: name (str): The name of the image. Returns: Image: The Image object matching the name. Raises: CHIValueError: If no image is found with the given name. ResourceError: If multiple images are found with the same name. """ifVersion(context.version)>=Version("1.0"):glance_images=list(glance().images.list(filters={"name":name}))ifnotglance_images:raiseCHIValueError(f'No images found matching name "{name}"')eliflen(glance_images)>1:raiseResourceError(f'Multiple images found matching name "{name}"')returnImage.from_glance_image(glance_images[0])try:returnglance().images.get(name)exceptNotFound:returnglance().images.get(get_image_id(name))
[docs]defget_image_name(id:str)->str:"""Look up an image's name from its ID. Args: id (str): The ID of the image. Returns: str: The name of the found image. Raises: CHIValueError: If the image could not be found. """try:image=glance().images.get(id)returnimage.nameexceptNotFound:raiseCHIValueError(f'No image found with ID "{id}"')
[docs]defget_image_id(name):"""Look up an image's ID from its name. Args: name (str): The name of the image. Returns: The ID of the found image. Raises: ValueError: If the image could not be found, or if multiple images matched the name. """images=list(glance().images.list(filters={"name":name}))ifnotimages:raiseCHIValueError(f'No images found matching name "{name}"')returnimages[0].id