fixing after review

This commit is contained in:
Eldar Nugaev 2011-04-14 03:18:26 +04:00
parent 3dcfd4a3b4
commit 2ff7a179c3
2 changed files with 25 additions and 41 deletions

View File

@ -75,6 +75,15 @@ class SwiftBackend(glance.store.Backend):
return resp_body 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 @classmethod
def add(cls, id, data, options): def add(cls, id, data, options):
""" """
@ -101,36 +110,19 @@ class SwiftBackend(glance.store.Backend):
from swift.common import client as swift_client from swift.common import client as swift_client
container = options.get('swift_store_container', container = options.get('swift_store_container',
DEFAULT_SWIFT_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 # TODO(jaypipes): This needs to be checked every time
# because of the decision to make glance.store.Backend's # because of the decision to make glance.store.Backend's
# interface all @classmethods. This is inefficient. Backend # interface all @classmethods. This is inefficient. Backend
# should be a stateful object with options parsed once in # should be a stateful object with options parsed once in
# a constructor. # a constructor.
if not auth_address: auth_address = cls._option_get(options, 'swift_store_auth_address')
msg = ("Could not find swift_store_auth_address in configuration " user = cls._option_get(options, 'swift_store_user')
"options.") key = cls._option_get(options, 'swift_store_key')
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
if not user: full_auth_address = auth_address
msg = ("Could not find swift_store_user in configuration " if not full_auth_address.startswith('http'):
"options.") full_auth_address = 'https://' + full_auth_address
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)
swift_conn = swift_client.Connection( swift_conn = swift_client.Connection(
authurl=full_auth_address, user=user, key=key, snet=False) authurl=full_auth_address, user=user, key=key, snet=False)

View File

@ -324,41 +324,33 @@ class TestSwiftBackend(unittest.TestCase):
SwiftBackend.add, SwiftBackend.add,
2, image_swift, SWIFT_OPTIONS) 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): def test_add_no_user(self):
""" """
Tests that adding options without user raises Tests that adding options without user raises
an appropriate exception an appropriate exception
""" """
image_swift = StringIO.StringIO("nevergonnamakeit") self._assertOptionRequiredForSwift('swift_store_user')
options = SWIFT_OPTIONS.copy()
del options['swift_store_user']
self.assertRaises(BackendException,
SwiftBackend.add,
2, image_swift, options)
def test_no_key(self): def test_no_key(self):
""" """
Tests that adding options without key raises Tests that adding options without key raises
an appropriate exception an appropriate exception
""" """
image_swift = StringIO.StringIO("nevergonnamakeit") self._assertOptionRequiredForSwift('swift_store_key')
options = SWIFT_OPTIONS.copy()
del options['swift_store_key']
self.assertRaises(BackendException,
SwiftBackend.add,
2, image_swift, options)
def test_add_no_auth_address(self): def test_add_no_auth_address(self):
""" """
Tests that adding options without auth address raises Tests that adding options without auth address raises
an appropriate exception an appropriate exception
""" """
image_swift = StringIO.StringIO("nevergonnamakeit") self._assertOptionRequiredForSwift('swift_store_auth_address')
options = SWIFT_OPTIONS.copy()
del options['swift_store_auth_address']
self.assertRaises(BackendException,
SwiftBackend.add,
2, image_swift, options)
def test_delete(self): def test_delete(self):
""" """