diff --git a/bin/nova-manage b/bin/nova-manage index 7b3769629f66..c1426e260e69 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -851,7 +851,7 @@ class InstanceTypeCommands(object): help='rxtx_factor') @args('--is_public', dest="is_public", metavar='', help='Make flavor accessible to the public') - def create(self, name, memory, vcpus, root_gb, ephemeral_gb, flavorid, + def create(self, name, memory, vcpus, root_gb, ephemeral_gb, flavorid=None, swap=0, rxtx_factor=1, is_public=True): """Creates instance types / flavors""" try: diff --git a/nova/api/openstack/compute/contrib/flavormanage.py b/nova/api/openstack/compute/contrib/flavormanage.py index d6bd870056c9..a7e313c9ca2e 100644 --- a/nova/api/openstack/compute/contrib/flavormanage.py +++ b/nova/api/openstack/compute/contrib/flavormanage.py @@ -58,7 +58,7 @@ class FlavorManageController(wsgi.Controller): vals = body['flavor'] name = vals['name'] - flavorid = vals['id'] + flavorid = vals.get('id') memory_mb = vals.get('ram') vcpus = vals.get('vcpus') root_gb = vals.get('disk') diff --git a/nova/compute/instance_types.py b/nova/compute/instance_types.py index e3193324090c..eb8ac706d0c1 100644 --- a/nova/compute/instance_types.py +++ b/nova/compute/instance_types.py @@ -35,10 +35,12 @@ LOG = logging.getLogger(__name__) INVALID_NAME_REGEX = re.compile("[^\w\.\- ]") -def create(name, memory, vcpus, root_gb, ephemeral_gb, flavorid, swap=None, - rxtx_factor=None, is_public=True): +def create(name, memory, vcpus, root_gb, ephemeral_gb, flavorid=None, + swap=None, rxtx_factor=None, is_public=True): """Creates instance types.""" + if flavorid is None: + flavorid = utils.gen_uuid() if swap is None: swap = 0 if rxtx_factor is None: diff --git a/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py b/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py index 7406b0fd9bfd..feae4c47f754 100644 --- a/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py +++ b/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py @@ -58,6 +58,8 @@ def fake_destroy(flavorname): def fake_create(name, memory_mb, vcpus, root_gb, ephemeral_gb, flavorid, swap, rxtx_factor, is_public): + if flavorid is None: + flavorid = 1234 newflavor = fake_get_instance_type_by_flavor_id(flavorid) newflavor["name"] = name @@ -156,6 +158,30 @@ class FlavorManageTest(test.TestCase): for key in expected["flavor"]: self.assertEquals(body["flavor"][key], expected["flavor"][key]) + def test_create_without_flavorid(self): + expected = { + "flavor": { + "name": "test", + "ram": 512, + "vcpus": 2, + "disk": 1, + "OS-FLV-EXT-DATA:ephemeral": 1, + "swap": 512, + "rxtx_factor": 1, + "os-flavor-access:is_public": True, + } + } + + url = '/v2/fake/flavors' + req = webob.Request.blank(url) + req.headers['Content-Type'] = 'application/json' + req.method = 'POST' + req.body = jsonutils.dumps(expected) + res = req.get_response(fakes.wsgi_app()) + body = jsonutils.loads(res.body) + for key in expected["flavor"]: + self.assertEquals(body["flavor"][key], expected["flavor"][key]) + def test_instance_type_exists_exception_returns_409(self): expected = { "flavor": { diff --git a/nova/tests/test_instance_types.py b/nova/tests/test_instance_types.py index ed33a74b054d..d9d2669a38f4 100644 --- a/nova/tests/test_instance_types.py +++ b/nova/tests/test_instance_types.py @@ -88,6 +88,18 @@ class InstanceTypeTestCase(test.TestCase): new_list = instance_types.get_all_types() self.assertEqual(original_list, new_list) + def test_instance_type_create_without_flavorid(self): + name = 'Small Flavor' + inst_type = instance_types.create(name, 256, 1, 120, 100) + self.assertNotEqual(inst_type['flavorid'], None) + self.assertEqual(inst_type['name'], name) + self.assertEqual(inst_type['memory_mb'], 256) + self.assertEqual(inst_type['vcpus'], 1) + self.assertEqual(inst_type['root_gb'], 120) + self.assertEqual(inst_type['ephemeral_gb'], 100) + self.assertEqual(inst_type['swap'], 0) + self.assertEqual(inst_type['rxtx_factor'], 1) + def test_instance_type_create_with_special_characters(self): """Ensure instance types raises InvalidInput for invalid characters""" name = "foo.bar!@#$%^-test_name"