From 2ff7a179c3826260c25357e84d9847e81fc1843f Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Thu, 14 Apr 2011 03:18:26 +0400 Subject: [PATCH] fixing after review --- glance/store/swift.py | 38 ++++++++++++++-------------------- tests/unit/test_swift_store.py | 28 +++++++++---------------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/glance/store/swift.py b/glance/store/swift.py index 3f07209d47..db1e5d14f6 100644 --- a/glance/store/swift.py +++ b/glance/store/swift.py @@ -75,6 +75,15 @@ class SwiftBackend(glance.store.Backend): return resp_body + @classmethod + def _option_get(cls, options, param): + result = options.get(param) + if not result: + msg = ("Could not find %s in configuration options." % param) + logger.error(msg) + raise glance.store.BackendException(msg) + return result + @classmethod def add(cls, id, data, options): """ @@ -101,36 +110,19 @@ class SwiftBackend(glance.store.Backend): from swift.common import client as swift_client container = options.get('swift_store_container', DEFAULT_SWIFT_CONTAINER) - auth_address = options.get('swift_store_auth_address') - user = options.get('swift_store_user') - key = options.get('swift_store_key') # TODO(jaypipes): This needs to be checked every time # because of the decision to make glance.store.Backend's # interface all @classmethods. This is inefficient. Backend # should be a stateful object with options parsed once in # a constructor. - if not auth_address: - msg = ("Could not find swift_store_auth_address in configuration " - "options.") - logger.error(msg) - raise glance.store.BackendException(msg) - else: - full_auth_address = auth_address - if not full_auth_address.startswith('http'): - full_auth_address = 'https://' + full_auth_address + auth_address = cls._option_get(options, 'swift_store_auth_address') + user = cls._option_get(options, 'swift_store_user') + key = cls._option_get(options, 'swift_store_key') - if not user: - msg = ("Could not find swift_store_user in configuration " - "options.") - logger.error(msg) - raise glance.store.BackendException(msg) - - if not key: - msg = ("Could not find swift_store_key in configuration " - "options.") - logger.error(msg) - raise glance.store.BackendException(msg) + full_auth_address = auth_address + if not full_auth_address.startswith('http'): + full_auth_address = 'https://' + full_auth_address swift_conn = swift_client.Connection( authurl=full_auth_address, user=user, key=key, snet=False) diff --git a/tests/unit/test_swift_store.py b/tests/unit/test_swift_store.py index b7fae7ccdb..62b1e81246 100644 --- a/tests/unit/test_swift_store.py +++ b/tests/unit/test_swift_store.py @@ -324,41 +324,33 @@ class TestSwiftBackend(unittest.TestCase): SwiftBackend.add, 2, image_swift, SWIFT_OPTIONS) + def _assertOptionRequiredForSwift(self, key): + image_swift = StringIO.StringIO("nevergonnamakeit") + options = SWIFT_OPTIONS.copy() + del options[key] + self.assertRaises(BackendException, SwiftBackend.add, + 2, image_swift, options) + def test_add_no_user(self): """ Tests that adding options without user raises an appropriate exception """ - image_swift = StringIO.StringIO("nevergonnamakeit") - options = SWIFT_OPTIONS.copy() - del options['swift_store_user'] - self.assertRaises(BackendException, - SwiftBackend.add, - 2, image_swift, options) + self._assertOptionRequiredForSwift('swift_store_user') def test_no_key(self): """ Tests that adding options without key raises an appropriate exception """ - image_swift = StringIO.StringIO("nevergonnamakeit") - options = SWIFT_OPTIONS.copy() - del options['swift_store_key'] - self.assertRaises(BackendException, - SwiftBackend.add, - 2, image_swift, options) + self._assertOptionRequiredForSwift('swift_store_key') def test_add_no_auth_address(self): """ Tests that adding options without auth address raises an appropriate exception """ - image_swift = StringIO.StringIO("nevergonnamakeit") - options = SWIFT_OPTIONS.copy() - del options['swift_store_auth_address'] - self.assertRaises(BackendException, - SwiftBackend.add, - 2, image_swift, options) + self._assertOptionRequiredForSwift('swift_store_auth_address') def test_delete(self): """