~0x44/nova/extdoc

« back to all changes in this revision

Viewing changes to nova/image/__init__.py

Update GlanceClient, GlanceImageService, and Glance Xen plugin to work with Glance keystone.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    under the License.
17
17
 
18
18
 
19
 
from urlparse import urlparse
20
 
 
21
19
import nova
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
26
23
 
27
24
FLAGS = flags.FLAGS
28
25
 
29
26
 
30
 
GlanceClient = utils.import_class('glance.client.Client')
31
 
 
32
 
 
33
 
def _parse_image_ref(image_href):
34
 
    """Parse an image href into composite parts.
35
 
 
36
 
    :param image_href: href of an image
37
 
    :returns: a tuple of the form (image_id, host, port)
38
 
    :raises ValueError
39
 
 
40
 
    """
41
 
    o = urlparse(image_href)
42
 
    port = o.port or 80
43
 
    host = o.netloc.split(':', 1)[0]
44
 
    image_id = int(o.path.split('/')[-1])
45
 
    return (image_id, host, port)
46
 
 
47
 
 
48
27
def get_default_image_service():
49
28
    ImageService = utils.import_class(FLAGS.image_service)
50
29
    return ImageService()
51
30
 
52
31
 
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.
57
 
 
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
61
 
    glance client.
62
 
 
63
 
    :param image_href: image ref/id for an image
64
 
    :returns: a tuple of the form (glance_client, image_id)
65
 
 
66
 
    """
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))
73
 
 
74
 
    try:
75
 
        (image_id, host, port) = _parse_image_ref(image_href)
76
 
    except ValueError:
77
 
        raise exception.InvalidImageRef(image_href=image_href)
78
 
    glance_client = GlanceClient(host, port)
79
 
    return (glance_client, image_id)
80
 
 
81
 
 
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.
84
34
 
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))
96
46
 
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)