Merge "Cleanups for create-flavor"

This commit is contained in:
Jenkins 2013-05-14 23:04:53 +00:00 committed by Gerrit Code Review
commit f124d50024
4 changed files with 35 additions and 45 deletions

View File

@ -58,18 +58,20 @@ class FlavorManageController(wsgi.Controller):
vals = body['flavor']
name = vals['name']
flavorid = vals.get('id')
memory_mb = vals.get('ram')
memory = vals.get('ram')
vcpus = vals.get('vcpus')
root_gb = vals.get('disk')
ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral')
swap = vals.get('swap')
rxtx_factor = vals.get('rxtx_factor')
ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral', 0)
swap = vals.get('swap', 0)
rxtx_factor = vals.get('rxtx_factor', 1.0)
is_public = vals.get('os-flavor-access:is_public', True)
try:
flavor = flavors.create(name, memory_mb, vcpus,
root_gb, ephemeral_gb, flavorid,
swap, rxtx_factor, is_public)
flavor = flavors.create(name, memory, vcpus, root_gb,
ephemeral_gb=ephemeral_gb,
flavorid=flavorid, swap=swap,
rxtx_factor=rxtx_factor,
is_public=is_public)
req.cache_db_flavor(flavor)
except (exception.InstanceTypeExists,
exception.InstanceTypeIdExists) as err:

View File

@ -887,8 +887,9 @@ class InstanceTypeCommands(object):
"""Creates instance types / flavors."""
try:
flavors.create(name, memory, vcpus, root_gb,
ephemeral_gb, flavorid, swap, rxtx_factor,
is_public)
ephemeral_gb=ephemeral_gb, flavorid=flavorid,
swap=swap, rxtx_factor=rxtx_factor,
is_public=is_public)
except exception.InvalidInput as e:
print _("Must supply valid parameters to create instance_type")
print e

View File

@ -65,18 +65,11 @@ system_metadata_instance_type_props = {
}
def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None,
swap=None, rxtx_factor=None, is_public=True):
def create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None,
swap=0, rxtx_factor=1.0, is_public=True):
"""Creates instance types."""
if flavorid is None or flavorid == '':
if not flavorid:
flavorid = uuid.uuid4()
if swap is None:
swap = 0
if rxtx_factor is None:
rxtx_factor = 1.0
if ephemeral_gb is None:
ephemeral_gb = 0
kwargs = {
'memory_mb': memory,
@ -96,13 +89,23 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None,
msg = _("names can only contain [a-zA-Z0-9_.- ]")
raise exception.InvalidInput(reason=msg)
# ensure some attributes are integers and greater than or equal to 0
for option in ['memory_mb', 'vcpus', 'root_gb', 'ephemeral_gb', 'swap']:
# Some attributes are positive ( > 0) integers
for option in ['memory_mb', 'vcpus']:
try:
kwargs[option] = int(kwargs[option])
assert kwargs[option] > 0
except (ValueError, AssertionError):
msg = _("'%s' argument must be greater than 0") % option
raise exception.InvalidInput(reason=msg)
# Some attributes are non-negative ( >= 0) integers
for option in ['root_gb', 'ephemeral_gb', 'swap']:
try:
kwargs[option] = int(kwargs[option])
assert kwargs[option] >= 0
except (ValueError, AssertionError):
msg = _("'%s' argument must be a positive integer") % option
msg = _("'%s' argument must be greater than or equal"
" to 0") % option
raise exception.InvalidInput(reason=msg)
# rxtx_factor should be a positive float
@ -113,14 +116,6 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None,
msg = _("'rxtx_factor' argument must be a positive float")
raise exception.InvalidInput(reason=msg)
# some value are required to be nonzero, not just positive
for option in ['memory_mb', 'vcpus']:
try:
assert kwargs[option] > 0
except AssertionError:
msg = _("'%s' argument must be greater than 0") % option
raise exception.InvalidInput(reason=msg)
kwargs['name'] = name
# NOTE(vish): Internally, flavorid is stored as a string but it comes
# in through json as an integer, so we convert it here.

View File

@ -6050,11 +6050,8 @@ class ComputeAPITestCase(BaseTestCase):
name = 'test_resize_new_flavor'
flavorid = 11
memory_mb = 128
root_gb = 0
vcpus = 1
flavors.create(name, memory_mb, vcpus, root_gb, 0,
flavorid, 0, 1.0, True)
flavors.create(name, 128, 1, 0, ephemeral_gb=0, flavorid=flavorid,
swap=0, rxtx_factor=1.0, is_public=True)
flavors.destroy(name)
self.assertRaises(exception.FlavorNotFound, self.compute_api.resize,
self.context, instance, flavorid)
@ -6082,11 +6079,8 @@ class ComputeAPITestCase(BaseTestCase):
name = 'test_resize_with_big_mem'
flavorid = 11
memory_mb = 102400
root_gb = 0
vcpus = 1
flavors.create(name, memory_mb, vcpus, root_gb, 0,
flavorid, 0, 1.0, True)
flavors.create(name, 102400, 1, 0, ephemeral_gb=0, flavorid=flavorid,
swap=0, rxtx_factor=1.0, is_public=True)
self.assertRaises(exception.TooManyInstances, self.compute_api.resize,
self.context, instance, flavorid)
@ -6096,11 +6090,9 @@ class ComputeAPITestCase(BaseTestCase):
def test_resize_revert_deleted_flavor_fails(self):
orig_name = 'test_resize_revert_orig_flavor'
orig_flavorid = 11
memory_mb = 128
root_gb = 0
vcpus = 1
flavors.create(orig_name, memory_mb, vcpus, root_gb, 0,
orig_flavorid, 0, 1.0, True)
flavors.create(orig_name, 128, 1, 0, ephemeral_gb=0,
flavorid=orig_flavorid, swap=0, rxtx_factor=1.0,
is_public=True)
instance = self._create_fake_instance(type_name=orig_name)
instance = db.instance_get_by_uuid(self.context, instance['uuid'])