Skip to content

Commit

Permalink
Generate a flavorid if needed at flavor creation
Browse files Browse the repository at this point in the history
When creating a new flavor, it's now possible to omit flavorid. In this
case it will be automatically generated using utils.gen_uuid().
nova-manage and OS API updated accordingly.

Fixes: bug #1043410
Change-Id: Ibf9229599dac953177fbf1ffac5242ed716142db
  • Loading branch information
Adrien Cunin committed Sep 6, 2012
1 parent 76d094e commit daf0681
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/nova-manage
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ class InstanceTypeCommands(object):
help='rxtx_factor')
@args('--is_public', dest="is_public", metavar='<is_public>',
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:
Expand Down
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/flavormanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _create(self, req, body):

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')
Expand Down
6 changes: 4 additions & 2 deletions nova/compute/instance_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
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:
Expand Down
26 changes: 26 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_flavor_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -156,6 +158,30 @@ def test_create_public_default(self):
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": {
Expand Down
12 changes: 12 additions & 0 deletions nova/tests/test_instance_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ def test_instance_type_create_then_delete(self):
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"
Expand Down

0 comments on commit daf0681

Please sign in to comment.