16
16
# under the License.
19
from urlparse import urlparse
22
from nova import exception
23
20
from nova import utils
24
21
from nova import flags
25
from nova.image import glance as glance_image_service
22
from nova.image import glance
27
24
FLAGS = flags.FLAGS
30
GlanceClient = utils.import_class('glance.client.Client')
33
def _parse_image_ref(image_href):
34
"""Parse an image href into composite parts.
36
:param image_href: href of an image
37
:returns: a tuple of the form (image_id, host, port)
41
o = urlparse(image_href)
43
host = o.netloc.split(':', 1)[0]
44
image_id = int(o.path.split('/')[-1])
45
return (image_id, host, port)
48
27
def get_default_image_service():
49
28
ImageService = utils.import_class(FLAGS.image_service)
50
29
return ImageService()
53
# FIXME(sirp): perhaps this should be moved to nova/images/glance so that we
54
# keep Glance specific code together for the most part
55
def get_glance_client(image_href):
56
"""Get the correct glance client and id for the given image_href.
58
The image_href param can be an href of the form
59
http://myglanceserver:9292/images/42, or just an int such as 42. If the
60
image_href is an int, then flags are used to create the default
63
:param image_href: image ref/id for an image
64
:returns: a tuple of the form (glance_client, image_id)
67
image_href = image_href or 0
68
if str(image_href).isdigit():
69
glance_host, glance_port = \
70
glance_image_service.pick_glance_api_server()
71
glance_client = GlanceClient(glance_host, glance_port)
72
return (glance_client, int(image_href))
75
(image_id, host, port) = _parse_image_ref(image_href)
77
raise exception.InvalidImageRef(image_href=image_href)
78
glance_client = GlanceClient(host, port)
79
return (glance_client, image_id)
82
def get_image_service(image_href):
32
def get_image_service(context, image_href):
83
33
"""Get the proper image_service and id for the given image_href.
85
35
The image_href param can be an href of the form
94
44
if str(image_href).isdigit():
95
45
return (get_default_image_service(), int(image_href))
97
(glance_client, image_id) = get_glance_client(image_href)
47
(glance_client, image_id) = glance.get_glance_client(context, image_href)
98
48
image_service = nova.image.glance.GlanceImageService(glance_client)
99
49
return (image_service, image_id)