Validate that boolean parameters are boolean

Ensure that values which are supposed to be boolean look like correct
user input, instead of assuming that any non true-looking input is
False.

Also, update the flavor manager to raise a CommandError if is_public is
not a boolean value.

Fixes bug 1059414

Change-Id: I3275e4bba103b14081becf91f723f1be060391e5
This commit is contained in:
Russell Cloran 2012-10-29 23:09:26 -07:00
parent 05bbe0fa0e
commit 7bf93a52f8
3 changed files with 15 additions and 2 deletions

View File

@ -48,9 +48,13 @@ def bool_from_str(val):
if not val:
return False
try:
return True if bool(int(val)) else False
return bool(int(val))
except ValueError:
return val.lower() in ['true', 'yes', 'y']
if val.lower() in ['true', 'yes', 'y']:
return True
if val.lower() in ['false', 'no', 'n']:
return False
raise
def add_resource_manager_extra_kwargs_hook(f, hook):

View File

@ -5,6 +5,7 @@ Flavor interface.
from novaclient import base
from novaclient import exceptions
from novaclient import utils
class Flavor(base.Resource):
@ -153,6 +154,11 @@ class FlavorManager(base.ManagerWithFind):
except:
raise exceptions.CommandError("rxtx_factor must be an integer.")
try:
is_public = utils.bool_from_str(is_public)
except:
raise exceptions.CommandError("is_public must be a boolean.")
body = {
"flavor": {
"name": name,

View File

@ -110,6 +110,9 @@ class FlavorsTest(utils.TestCase):
self.assertRaises(exceptions.CommandError, cs.flavors.create,
"flavorcreate", 512, 1, 10, 1234, swap=0,
ephemeral=0, rxtx_factor="invalid", is_public=True)
self.assertRaises(exceptions.CommandError, cs.flavors.create,
"flavorcreate", 512, 1, 10, 1234, swap=0,
ephemeral=0, rxtx_factor=1, is_public='invalid')
def test_delete(self):
cs.flavors.delete("flavordelete")