~eday/nova/pep8-fixes-api

« back to all changes in this revision

Viewing changes to nova/image/services/glance/__init__.py

  • Committer: Vishvananda Ishaya
  • Date: 2010-10-20 20:54:53 UTC
  • mto: This revision was merged to the branch mainline in revision 372.
  • Revision ID: vishvananda@yahoo.com-20101020205453-p5rd8ikdcr32vyri
Check the pid to make sure it refers to the correct dnsmasq process

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2010 OpenStack LLC.
4
 
# All Rights Reserved.
5
 
#
6
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 
#    not use this file except in compliance with the License. You may obtain
8
 
#    a copy of the License at
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
#    Unless required by applicable law or agreed to in writing, software
13
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 
#    License for the specific language governing permissions and limitations
16
 
#    under the License.
17
 
 
18
 
"""Implementation of an image service that uses Glance as the backend"""
19
 
 
20
 
import httplib
21
 
import json
22
 
import urlparse
23
 
 
24
 
import webob.exc
25
 
 
26
 
from nova import utils
27
 
from nova import flags
28
 
from nova import exception
29
 
import nova.image.service
30
 
 
31
 
FLAGS = flags.FLAGS
32
 
 
33
 
class TellerClient(object):
34
 
 
35
 
    def __init__(self):
36
 
        self.address = FLAGS.glance_teller_address
37
 
        self.port = FLAGS.glance_teller_port
38
 
        url = urlparse.urlparse(self.address)
39
 
        self.netloc = url.netloc
40
 
        self.connection_type = {'http': httplib.HTTPConnection,
41
 
                                'https': httplib.HTTPSConnection}[url.scheme]
42
 
 
43
 
 
44
 
class ParallaxClient(object):
45
 
 
46
 
    def __init__(self):
47
 
        self.address = FLAGS.glance_parallax_address
48
 
        self.port = FLAGS.glance_parallax_port
49
 
        url = urlparse.urlparse(self.address)
50
 
        self.netloc = url.netloc
51
 
        self.connection_type = {'http': httplib.HTTPConnection,
52
 
                                'https': httplib.HTTPSConnection}[url.scheme]
53
 
 
54
 
    def get_image_index(self):
55
 
        """
56
 
        Returns a list of image id/name mappings from Parallax
57
 
        """
58
 
        try:
59
 
            c = self.connection_type(self.netloc, self.port)
60
 
            c.request("GET", "images")
61
 
            res = c.getresponse()
62
 
            if res.status == 200:
63
 
                # Parallax returns a JSONified dict(images=image_list)
64
 
                data = json.loads(res.read())['images']
65
 
                return data
66
 
            else:
67
 
                logging.warn("Parallax returned HTTP error %d from "
68
 
                             "request for /images", res.status_int)
69
 
                return []
70
 
        finally:
71
 
            c.close()
72
 
 
73
 
    def get_image_details(self):
74
 
        """
75
 
        Returns a list of detailed image data mappings from Parallax
76
 
        """
77
 
        try:
78
 
            c = self.connection_type(self.netloc, self.port)
79
 
            c.request("GET", "images/detail")
80
 
            res = c.getresponse()
81
 
            if res.status == 200:
82
 
                # Parallax returns a JSONified dict(images=image_list)
83
 
                data = json.loads(res.read())['images']
84
 
                return data
85
 
            else:
86
 
                logging.warn("Parallax returned HTTP error %d from "
87
 
                             "request for /images/detail", res.status_int)
88
 
                return []
89
 
        finally:
90
 
            c.close()
91
 
 
92
 
    def get_image_metadata(self, image_id):
93
 
        """
94
 
        Returns a mapping of image metadata from Parallax
95
 
        """
96
 
        try:
97
 
            c = self.connection_type(self.netloc, self.port)
98
 
            c.request("GET", "images/%s" % image_id)
99
 
            res = c.getresponse()
100
 
            if res.status == 200:
101
 
                # Parallax returns a JSONified dict(image=image_info)
102
 
                data = json.loads(res.read())['image']
103
 
                return data
104
 
            else:
105
 
                # TODO(jaypipes): log the error?
106
 
                return None
107
 
        finally:
108
 
            c.close()
109
 
 
110
 
    def add_image_metadata(self, image_metadata):
111
 
        """
112
 
        Tells parallax about an image's metadata
113
 
        """
114
 
        try:
115
 
            c = self.connection_type(self.netloc, self.port)
116
 
            body = json.dumps(image_metadata)
117
 
            c.request("POST", "images", body)
118
 
            res = c.getresponse()
119
 
            if res.status == 200:
120
 
                # Parallax returns a JSONified dict(image=image_info)
121
 
                data = json.loads(res.read())['image']
122
 
                return data['id']
123
 
            else:
124
 
                # TODO(jaypipes): log the error?
125
 
                return None
126
 
        finally:
127
 
            c.close()
128
 
 
129
 
    def update_image_metadata(self, image_id, image_metadata):
130
 
        """
131
 
        Updates Parallax's information about an image
132
 
        """
133
 
        try:
134
 
            c = self.connection_type(self.netloc, self.port)
135
 
            body = json.dumps(image_metadata)
136
 
            c.request("PUT", "images/%s" % image_id, body)
137
 
            res = c.getresponse()
138
 
            return res.status == 200
139
 
        finally:
140
 
            c.close()
141
 
 
142
 
    def delete_image_metadata(self, image_id):
143
 
        """
144
 
        Deletes Parallax's information about an image
145
 
        """
146
 
        try:
147
 
            c = self.connection_type(self.netloc, self.port)
148
 
            c.request("DELETE", "images/%s" % image_id)
149
 
            res = c.getresponse()
150
 
            return res.status == 200
151
 
        finally:
152
 
            c.close()
153
 
 
154
 
 
155
 
class GlanceImageService(nova.image.service.BaseImageService):
156
 
    
157
 
    """Provides storage and retrieval of disk image objects within Glance."""
158
 
 
159
 
    def __init__(self):
160
 
        self.teller = TellerClient()
161
 
        self.parallax = ParallaxClient()
162
 
 
163
 
    def index(self):
164
 
        """
165
 
        Calls out to Parallax for a list of images available
166
 
        """
167
 
        images = self.parallax.get_image_index()
168
 
        return images
169
 
 
170
 
    def detail(self):
171
 
        """
172
 
        Calls out to Parallax for a list of detailed image information
173
 
        """
174
 
        images = self.parallax.get_image_details()
175
 
        return images
176
 
 
177
 
    def show(self, id):
178
 
        """
179
 
        Returns a dict containing image data for the given opaque image id.
180
 
        """
181
 
        image = self.parallax.get_image_metadata(id)
182
 
        if image:
183
 
            return image
184
 
        raise exception.NotFound
185
 
 
186
 
    def create(self, data):
187
 
        """
188
 
        Store the image data and return the new image id.
189
 
 
190
 
        :raises AlreadyExists if the image already exist.
191
 
 
192
 
        """
193
 
        return self.parallax.add_image_metadata(data)
194
 
 
195
 
    def update(self, image_id, data):
196
 
        """Replace the contents of the given image with the new data.
197
 
 
198
 
        :raises NotFound if the image does not exist.
199
 
 
200
 
        """
201
 
        self.parallax.update_image_metadata(image_id, data)
202
 
 
203
 
    def delete(self, image_id):
204
 
        """
205
 
        Delete the given image. 
206
 
        
207
 
        :raises NotFound if the image does not exist.
208
 
        
209
 
        """
210
 
        self.parallax.delete_image_metadata(image_id)
211
 
 
212
 
    def delete_all(self):
213
 
        """
214
 
        Clears out all images
215
 
        """
216
 
        pass