From 7555aca28a5ab1ba4dd1be04a91bf6347eaac84f Mon Sep 17 00:00:00 2001 From: Chris Behrens Date: Wed, 29 Jun 2011 15:22:56 -0700 Subject: [PATCH] fix issue of recurse_zones not being converted to bool properly add bool_from_str util call add test for bool_from_str slight rework of min/max_count check --- nova/api/openstack/create_instance_helper.py | 10 ++-------- nova/api/openstack/servers.py | 3 +-- nova/tests/test_utils.py | 13 +++++++++++++ nova/utils.py | 11 +++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/nova/api/openstack/create_instance_helper.py b/nova/api/openstack/create_instance_helper.py index 2cc38911a090..1066713a39aa 100644 --- a/nova/api/openstack/create_instance_helper.py +++ b/nova/api/openstack/create_instance_helper.py @@ -119,14 +119,8 @@ class CreateInstanceHelper(object): # min_count and max_count are optional. If they exist, they come # in as strings. We want to default 'min_count' to 1, and default # 'max_count' to be 'min_count'. - if not min_count: - min_count = 1 - else: - min_count = int(min_count) - if not max_count: - max_count = min_count - else: - max_count = int(max_count) + min_count = int(min_count) if min_count else 1 + max_count = int(max_count) if max_count else min_count if min_count > max_count: min_count = max_count diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 66ef97af09bc..fc1ab8d4687a 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -80,8 +80,7 @@ class Controller(object): reservation_id = query_str.get('reservation_id') project_id = query_str.get('project_id') fixed_ip = query_str.get('fixed_ip') - recurse_zones = query_str.get('recurse_zones') - recurse_zones = recurse_zones and True or False + recurse_zones = utils.bool_from_str(query_str.get('recurse_zones')) instance_list = self.compute_api.get_all( req.environ['nova.context'], reservation_id=reservation_id, diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index 3a3f914e4fe0..0c359e981fde 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -276,6 +276,19 @@ class GenericUtilsTestCase(test.TestCase): result = utils.parse_server_string('www.exa:mple.com:8443') self.assertEqual(('', ''), result) + def test_bool_from_str(self): + self.assertTrue(utils.bool_from_str('1')) + self.assertTrue(utils.bool_from_str('2')) + self.assertTrue(utils.bool_from_str('-1')) + self.assertTrue(utils.bool_from_str('true')) + self.assertTrue(utils.bool_from_str('True')) + self.assertTrue(utils.bool_from_str('tRuE')) + self.assertFalse(utils.bool_from_str('False')) + self.assertFalse(utils.bool_from_str('false')) + self.assertFalse(utils.bool_from_str('0')) + self.assertFalse(utils.bool_from_str(None)) + self.assertFalse(utils.bool_from_str('junk')) + class IsUUIDLikeTestCase(test.TestCase): def assertUUIDLike(self, val, expected): diff --git a/nova/utils.py b/nova/utils.py index 510cdd9f69d8..be26899ca42d 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -772,6 +772,17 @@ def is_uuid_like(val): return (len(val) == 36) and (val.count('-') == 4) +def bool_from_str(val): + """Convert a string representation of a bool into a bool value""" + + if not val: + return False + try: + return True if int(val) else False + except ValueError: + return val.lower() == 'true' + + class Bootstrapper(object): """Provides environment bootstrapping capabilities for entry points."""