summaryrefslogtreecommitdiffstats
path: root/nova/image
diff options
context:
space:
mode:
authorBrian Waldon <bcwaldon@gmail.com>2012-06-25 14:06:08 -0700
committerBrian Waldon <bcwaldon@gmail.com>2012-06-27 14:31:35 -0700
commit3aaa0b103447d56f8d3b259c693cd9a3a8dcbe36 (patch)
tree00a15f76716fd9e43ce7c24dfc8b45e38a1b4866 /nova/image
parent7e658bd45db6c14b4cd7e3a976caf769873d4ca1 (diff)
downloadnova-3aaa0b103447d56f8d3b259c693cd9a3a8dcbe36.tar.gz
nova-3aaa0b103447d56f8d3b259c693cd9a3a8dcbe36.tar.xz
nova-3aaa0b103447d56f8d3b259c693cd9a3a8dcbe36.zip
Cleanup of image service code
* Remove image_service flag * Move nova.image.fake to nova.tests.image.fake * Move nova.image.get_default_image_service to nova.image.glance.get_default_image_service * Move nova.image.get_image_service to nova.image.glance.get_remote_image_service * Related to bp integrate-python-glanceclient Change-Id: Iea6db7898328a9060fb88586e042efbc0a4351fc
Diffstat (limited to 'nova/image')
-rw-r--r--nova/image/__init__.py51
-rw-r--r--nova/image/fake.py260
-rw-r--r--nova/image/glance.py28
-rw-r--r--nova/image/s3.py4
4 files changed, 30 insertions, 313 deletions
diff --git a/nova/image/__init__.py b/nova/image/__init__.py
index 18dcc2fa2..e69de29bb 100644
--- a/nova/image/__init__.py
+++ b/nova/image/__init__.py
@@ -1,51 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-#
-# Copyright 2011 OpenStack LLC.
-# All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-
-import nova
-from nova import flags
-from nova.image import glance
-from nova.openstack.common import importutils
-
-FLAGS = flags.FLAGS
-
-
-def get_default_image_service():
- ImageService = importutils.import_class(FLAGS.image_service)
- return ImageService()
-
-
-def get_image_service(context, image_href):
- """Get the proper image_service 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 the default image service is returned.
-
- :param image_href: image ref/id for an image
- :returns: a tuple of the form (image_service, image_id)
-
- """
- # check if this is not a uri
- if '/' not in str(image_href):
- return (get_default_image_service(), image_href)
-
- else:
- (glance_client, image_id) = glance._get_glance_client(context,
- image_href)
- image_service = nova.image.glance.GlanceImageService(glance_client)
- return (image_service, image_id)
diff --git a/nova/image/fake.py b/nova/image/fake.py
deleted file mode 100644
index 996459081..000000000
--- a/nova/image/fake.py
+++ /dev/null
@@ -1,260 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-
-# Copyright 2011 Justin Santa Barbara
-# All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-"""Implementation of a fake image service"""
-
-import copy
-import datetime
-
-from nova import exception
-from nova import flags
-from nova import log as logging
-from nova import utils
-
-
-LOG = logging.getLogger(__name__)
-
-
-FLAGS = flags.FLAGS
-
-
-class _FakeImageService(object):
- """Mock (fake) image service for unit testing."""
-
- def __init__(self):
- self.images = {}
- # NOTE(justinsb): The OpenStack API can't upload an image?
- # So, make sure we've got one..
- timestamp = datetime.datetime(2011, 01, 01, 01, 02, 03)
-
- # NOTE(bcwaldon): was image '123456'
- image1 = {'id': '155d900f-4e14-4e4c-a73d-069cbf4541e6',
- 'name': 'fakeimage123456',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': False,
- 'container_format': 'raw',
- 'disk_format': 'raw',
- 'properties': {'kernel_id': FLAGS.null_kernel,
- 'ramdisk_id': FLAGS.null_kernel,
- 'architecture': 'x86_64'}}
-
- # NOTE(bcwaldon): was image 'fake'
- image2 = {'id': 'a2459075-d96c-40d5-893e-577ff92e721c',
- 'name': 'fakeimage123456',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': True,
- 'container_format': 'ami',
- 'disk_format': 'ami',
- 'properties': {'kernel_id': FLAGS.null_kernel,
- 'ramdisk_id': FLAGS.null_kernel}}
-
- # NOTE(bcwaldon): was image '2'
- image3 = {'id': '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6',
- 'name': 'fakeimage123456',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': True,
- 'container_format': None,
- 'disk_format': None,
- 'properties': {'kernel_id': FLAGS.null_kernel,
- 'ramdisk_id': FLAGS.null_kernel}}
-
- # NOTE(bcwaldon): was image '1'
- image4 = {'id': 'cedef40a-ed67-4d10-800e-17455edce175',
- 'name': 'fakeimage123456',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': True,
- 'container_format': 'ami',
- 'disk_format': 'ami',
- 'properties': {'kernel_id': FLAGS.null_kernel,
- 'ramdisk_id': FLAGS.null_kernel}}
-
- # NOTE(bcwaldon): was image '3'
- image5 = {'id': 'c905cedb-7281-47e4-8a62-f26bc5fc4c77',
- 'name': 'fakeimage123456',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': True,
- 'container_format': 'ami',
- 'disk_format': 'ami',
- 'properties': {'kernel_id':
- '155d900f-4e14-4e4c-a73d-069cbf4541e6',
- 'ramdisk_id': None}}
-
- # NOTE(sirp): was image '6'
- image6 = {'id': 'a440c04b-79fa-479c-bed1-0b816eaec379',
- 'name': 'fakeimage6',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': False,
- 'container_format': 'ova',
- 'disk_format': 'vhd',
- 'properties': {'kernel_id': FLAGS.null_kernel,
- 'ramdisk_id': FLAGS.null_kernel,
- 'architecture': 'x86_64',
- 'auto_disk_config': 'False'}}
-
- # NOTE(sirp): was image '7'
- image7 = {'id': '70a599e0-31e7-49b7-b260-868f441e862b',
- 'name': 'fakeimage7',
- 'created_at': timestamp,
- 'updated_at': timestamp,
- 'deleted_at': None,
- 'deleted': False,
- 'status': 'active',
- 'is_public': False,
- 'container_format': 'ova',
- 'disk_format': 'vhd',
- 'properties': {'kernel_id': FLAGS.null_kernel,
- 'ramdisk_id': FLAGS.null_kernel,
- 'architecture': 'x86_64',
- 'auto_disk_config': 'True'}}
-
- self.create(None, image1)
- self.create(None, image2)
- self.create(None, image3)
- self.create(None, image4)
- self.create(None, image5)
- self.create(None, image6)
- self.create(None, image7)
- self._imagedata = {}
- super(_FakeImageService, self).__init__()
-
- #TODO(bcwaldon): implement optional kwargs such as limit, sort_dir
- def index(self, context, **kwargs):
- """Returns list of images."""
- retval = []
- for img in self.images.values():
- retval += [dict([(k, v) for k, v in img.iteritems()
- if k in ['id', 'name']])]
- return retval
-
- #TODO(bcwaldon): implement optional kwargs such as limit, sort_dir
- def detail(self, context, **kwargs):
- """Return list of detailed image information."""
- return copy.deepcopy(self.images.values())
-
- def get(self, context, image_id, data):
- metadata = self.show(context, image_id)
- data.write(self._imagedata.get(image_id, ''))
- return metadata
-
- def show(self, context, image_id):
- """Get data about specified image.
-
- Returns a dict containing image data for the given opaque image id.
-
- """
- image = self.images.get(str(image_id))
- if image:
- return copy.deepcopy(image)
- LOG.warn('Unable to find image id %s. Have images: %s',
- image_id, self.images)
- raise exception.ImageNotFound(image_id=image_id)
-
- def show_by_name(self, context, name):
- """Returns a dict containing image data for the given name."""
- images = copy.deepcopy(self.images.values())
- for image in images:
- if name == image.get('name'):
- return image
- raise exception.ImageNotFound(image_id=name)
-
- def create(self, context, metadata, data=None):
- """Store the image data and return the new image id.
-
- :raises: Duplicate if the image already exist.
-
- """
- image_id = str(metadata.get('id', utils.gen_uuid()))
- metadata['id'] = image_id
- if image_id in self.images:
- raise exception.Duplicate()
- self.images[image_id] = copy.deepcopy(metadata)
- if data:
- self._imagedata[image_id] = data.read()
- return self.images[image_id]
-
- def update(self, context, image_id, metadata, data=None,
- headers=None):
- """Replace the contents of the given image with the new data.
-
- :raises: ImageNotFound if the image does not exist.
-
- """
- if not self.images.get(image_id):
- raise exception.ImageNotFound(image_id=image_id)
- try:
- purge = headers['x-glance-registry-purge-props']
- except Exception:
- purge = True
- if purge:
- self.images[image_id] = copy.deepcopy(metadata)
- else:
- image = self.images[image_id]
- try:
- image['properties'].update(metadata.pop('properties'))
- except Exception:
- pass
- image.update(metadata)
- return self.images[image_id]
-
- def delete(self, context, image_id):
- """Delete the given image.
-
- :raises: ImageNotFound if the image does not exist.
-
- """
- removed = self.images.pop(image_id, None)
- if not removed:
- raise exception.ImageNotFound(image_id=image_id)
-
- def delete_all(self):
- """Clears out all images."""
- self.images.clear()
-
-_fakeImageService = _FakeImageService()
-
-
-def FakeImageService():
- return _fakeImageService
-
-
-def FakeImageService_reset():
- global _fakeImageService
- _fakeImageService = _FakeImageService()
diff --git a/nova/image/glance.py b/nova/image/glance.py
index bdd30ae91..9ad6da528 100644
--- a/nova/image/glance.py
+++ b/nova/image/glance.py
@@ -511,3 +511,31 @@ def _translate_plain_exception(exc_type, exc_value):
if exc_type is glance_exception.Invalid:
return exception.Invalid(exc_value)
return exc_value
+
+
+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()
+ image_id = image_href
+ else:
+ (glance_client, image_id) = _get_glance_client(context, image_href)
+ image_service = GlanceImageService(glance_client)
+
+ return (image_service, image_id)
+
+
+def get_default_image_service():
+ return GlanceImageService()
diff --git a/nova/image/s3.py b/nova/image/s3.py
index 9fcfd5c89..b5a94cb3f 100644
--- a/nova/image/s3.py
+++ b/nova/image/s3.py
@@ -33,7 +33,7 @@ from nova.api.ec2 import ec2utils
import nova.cert.rpcapi
from nova import exception
from nova import flags
-from nova import image
+from nova.image import glance
from nova import log as logging
from nova.openstack.common import cfg
from nova import utils
@@ -69,7 +69,7 @@ class S3ImageService(object):
def __init__(self, service=None, *args, **kwargs):
self.cert_rpcapi = nova.cert.rpcapi.CertAPI()
- self.service = service or image.get_default_image_service()
+ self.service = service or glance.get_default_image_service()
self.service.__init__(*args, **kwargs)
def _translate_uuids_to_ids(self, context, images):