def get_remote_image_service(context, image_href): """Create an image_service and parse the id from the given image_href. The image_href param can be an href of the form 'http://example.com:9292/v1/images/b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3', or just an id such as 'b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3'. If the image_href is a standalone id, then the default image service is returned. :param image_href: href that describes the location of an image :returns: a tuple of the form (image_service, image_id) """ #NOTE(bcwaldon): If image_href doesn't look like a URI, assume its a # standalone image ID if '/' not in str(image_href): image_service = get_default_image_service() return image_service, image_href try: (image_id, glance_host, glance_port, use_ssl) = \ _parse_image_ref(image_href) glance_client = GlanceClientWrapper(context=context, host=glance_host, port=glance_port, use_ssl=use_ssl) except ValueError: raise exception.InvalidImageRef(image_href=image_href) image_service = GlanceImageService(client=glance_client) return image_service, image_id
def get_remote_image_service(context, image_href): """Create an image_service and parse the id from the given image_href. The image_href param can be an href of the form 'http://example.com:9292/v1/images/b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3', or just an id such as 'b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3'. If the image_href is a standalone id, then the default image service is returned. :param image_href: href that describes the location of an image :returns: a tuple of the form (image_service, image_id) """ # NOTE(bcwaldon): If image_href doesn't look like a URI, assume its a # standalone image ID if '/' not in str(image_href): image_service = get_default_image_service() return image_service, image_href try: (image_id, endpoint) = _endpoint_from_image_ref(image_href) glance_client = GlanceClientWrapper(context=context, endpoint=endpoint) except ValueError: raise exception.InvalidImageRef(image_href=image_href) # TODO(sbiswas7): Remove this check once we move to glance V2 # completely. if CONF.glance.use_glance_v1: image_service = GlanceImageService(client=glance_client) else: image_service = GlanceImageServiceV2(client=glance_client) return image_service, image_id
def get_glance_client(context, image_href): """Get the correct glance client and id for the given image_href. The image_href param can be an href of the form http://myglanceserver:9292/images/42, or just an int such as 42. If the image_href is an int, then flags are used to create the default glance client. :param image_href: image ref/id for an image :returns: a tuple of the form (glance_client, image_id) """ glance_host, glance_port = pick_glance_api_server() # check if this is an id if '/' not in str(image_href): glance_client = _create_glance_client(context, glance_host, glance_port) return (glance_client, image_href) else: try: (image_id, host, port) = _parse_image_ref(image_href) except ValueError: raise exception.InvalidImageRef(image_href=image_href) glance_client = _create_glance_client(context, glance_host, glance_port) return (glance_client, image_id)
def get_glance_client(image_href): """Get the correct glance client and id for the given image_href. The image_href param can be an href of the form http://myglanceserver:9292/images/42, or just an int such as 42. If the image_href is an int, then flags are used to create the default glance client. :param image_href: image ref/id for an image :returns: a tuple of the form (glance_client, image_id) """ image_href = image_href or 0 if str(image_href).isdigit(): glance_host, glance_port = \ glance_image_service.pick_glance_api_server() glance_client = GlanceClient(glance_host, glance_port) return (glance_client, int(image_href)) try: (image_id, host, port) = _parse_image_ref(image_href) except ValueError: raise exception.InvalidImageRef(image_href=image_href) glance_client = GlanceClient(host, port) return (glance_client, image_id)
def test_get_cached_image_with_fetch_exception(self, mock_fetch): (expected_path, expected_vhd_path) = self._prepare_get_cached_image(False, False) # path doesn't exist until fetched. self.imagecache._pathutils.exists.side_effect = [False, False, True] mock_fetch.side_effect = exception.InvalidImageRef( image_href=self.FAKE_IMAGE_REF) self.assertRaises(exception.InvalidImageRef, self.imagecache.get_cached_image, self.context, self.instance) self.imagecache._pathutils.remove.assert_called_once_with( expected_path)