From ef4e9314eb880238642e3122ae699f553b98db0e Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Wed, 24 Oct 2012 14:03:17 -0700 Subject: [PATCH] Use UUIDs for flavor create/edit. Now that Nova accepts strings as flavor IDs we can just use UUIDs on our end and get rid of the horrors of trying to guess the next integer ID. This fixes all the instability in flavor editing. Fixes bug 1057799. Change-Id: I55eb1d7408655fd27e81cc1196f15ceb3659a7b4 --- .../dashboards/admin/flavors/forms.py | 21 +++---------------- .../dashboards/admin/flavors/tests.py | 11 ++++------ 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/openstack_dashboard/dashboards/admin/flavors/forms.py b/openstack_dashboard/dashboards/admin/flavors/forms.py index 208d6f5ee3..0f68ee7c54 100644 --- a/openstack_dashboard/dashboards/admin/flavors/forms.py +++ b/openstack_dashboard/dashboards/admin/flavors/forms.py @@ -19,6 +19,7 @@ # under the License. import logging +import uuid from django.utils.translation import ugettext_lazy as _ @@ -39,22 +40,6 @@ class CreateFlavor(forms.SelfHandlingForm): disk_gb = forms.IntegerField(label=_("Root Disk GB")) eph_gb = forms.IntegerField(label=_("Ephemeral Disk GB")) - def _get_new_flavor_id(self): - # TODO(gabriel): Get rid of this hack after flavor - # id handling is improved in Nova's API. - flavors = [] - try: - flavors = api.nova.flavor_list(self.request) - except: - exceptions.handle(self.request, - _("Unable to get unique ID for new flavor.")) - if flavors: - largest_id = max(flavors, key=lambda f: f.id).id - flavor_id = int(largest_id) + 1 - else: - flavor_id = 1 - return flavor_id - def handle(self, request, data): try: flavor = api.nova.flavor_create(request, @@ -62,7 +47,7 @@ class CreateFlavor(forms.SelfHandlingForm): data['memory_mb'], data['vcpus'], data['disk_gb'], - self._get_new_flavor_id(), + uuid.uuid4(), ephemeral=data['eph_gb']) msg = _('Created flavor "%s".') % data['name'] messages.success(request, msg) @@ -87,7 +72,7 @@ class EditFlavor(CreateFlavor): data['memory_mb'], data['vcpus'], data['disk_gb'], - self._get_new_flavor_id(), + uuid.uuid4(), ephemeral=data['eph_gb']) msg = _('Updated flavor "%s".') % data['name'] messages.success(request, msg) diff --git a/openstack_dashboard/dashboards/admin/flavors/tests.py b/openstack_dashboard/dashboards/admin/flavors/tests.py index b6a01ac5d3..0b9ac4353b 100644 --- a/openstack_dashboard/dashboards/admin/flavors/tests.py +++ b/openstack_dashboard/dashboards/admin/flavors/tests.py @@ -1,3 +1,5 @@ +import uuid + from django import http from django.core.urlresolvers import reverse from mox import IsA @@ -14,13 +16,12 @@ class FlavorsTests(test.BaseAdminViewTests): self.mox.StubOutWithMock(api.nova, 'flavor_create') # no pre-existing flavors - api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn([]) api.nova.flavor_create(IsA(http.HttpRequest), flavor.name, flavor.ram, flavor.vcpus, flavor.disk, - 1, # Flavor id 1 because there are no others. + IsA(uuid.uuid4()), ephemeral=eph).AndReturn(flavor) self.mox.ReplayAll() @@ -39,9 +40,7 @@ class FlavorsTests(test.BaseAdminViewTests): reverse("horizon:admin:flavors:index")) def test_edit_flavor(self): - flavors = self.flavors.list() flavor = self.flavors.first() - next_id = int(max(flavors, key=lambda f: f.id).id) + 1 eph = getattr(flavor, 'OS-FLV-EXT-DATA:ephemeral') self.mox.StubOutWithMock(api.nova, 'flavor_list') self.mox.StubOutWithMock(api.nova, 'flavor_get') @@ -54,14 +53,12 @@ class FlavorsTests(test.BaseAdminViewTests): # POST api.nova.flavor_get(IsA(http.HttpRequest), flavor.id).AndReturn(flavor) api.nova.flavor_delete(IsA(http.HttpRequest), int(flavor.id)) - api.nova.flavor_list(IsA(http.HttpRequest)) \ - .AndReturn(flavors) api.nova.flavor_create(IsA(http.HttpRequest), flavor.name, flavor.ram, flavor.vcpus + 1, flavor.disk, - next_id, + IsA(uuid.uuid4()), ephemeral=eph).AndReturn(flavor) self.mox.ReplayAll()