Flavor ID added to Flavor creation form

Flavor ID supported values are:
* integer
* uuid4
* auto

Using 'auto' generates a random uuid4.

Clean method from Edit Form removed as unnecessary.

Flavor tests updated.

Fixes: bug #1189877

Change-Id: I222836d7a84b66891d5380f323bf123122044d30
This commit is contained in:
Nikita Konovalov 2013-07-25 13:39:47 +04:00
parent 699926413c
commit 2c2e21d512
3 changed files with 37 additions and 27 deletions

View File

@ -366,9 +366,10 @@ def server_spice_console(request, instance_id, console_type='spice-html5'):
instance_id, console_type)['console'])
def flavor_create(request, name, memory, vcpu, disk, ephemeral=0, swap=0,
metadata=None):
def flavor_create(request, name, memory, vcpu, disk, flavorid='auto',
ephemeral=0, swap=0, metadata=None):
flavor = novaclient(request).flavors.create(name, memory, vcpu, disk,
flavorid=flavorid,
ephemeral=ephemeral,
swap=swap)
if (metadata):

View File

@ -33,12 +33,22 @@ LOG = logging.getLogger(__name__)
class CreateFlavor(forms.SelfHandlingForm):
_flavor_id_regex = (r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-'
r'[0-9a-fA-F]{4}-[0-9a-fA-F]{12}|[0-9]+|auto$')
_flavor_id_help_text = _("Flavor ID should be UUID4 or integer. "
"Leave this field blank or use 'auto' to set "
"a random UUID4.")
name = forms.RegexField(label=_("Name"),
max_length=25,
regex=r'^[\w\.\- ]+$',
error_messages={'invalid': _('Name may only '
'contain letters, numbers, underscores, '
'periods and hyphens.')})
flavor_id = forms.RegexField(label=_("ID"),
regex=_flavor_id_regex,
required=False,
initial='auto',
help_text=_flavor_id_help_text)
vcpus = forms.IntegerField(label=_("VCPUs"))
memory_mb = forms.IntegerField(label=_("RAM MB"))
disk_gb = forms.IntegerField(label=_("Root Disk GB"))
@ -63,6 +73,24 @@ class CreateFlavor(forms.SelfHandlingForm):
)
return name
def clean_flavor_id(self):
flavor_id = self.data.get('flavor_id')
try:
flavors = api.nova.flavor_list(self.request)
except:
flavors = []
msg = _('Unable to get flavor list')
exceptions.check_message(["Connection", "refused"], msg)
raise
if flavors is not None:
for flavor in flavors:
if flavor.id == flavor_id:
raise forms.ValidationError(
_('The ID "%s" is already used by another flavor.')
% flavor_id
)
return flavor_id
def handle(self, request, data):
try:
flavor = api.nova.flavor_create(request,
@ -70,6 +98,7 @@ class CreateFlavor(forms.SelfHandlingForm):
data['memory_mb'],
data['vcpus'],
data['disk_gb'],
flavorid=data["flavor_id"],
ephemeral=data['eph_gb'],
swap=data['swap_mb'])
msg = _('Created flavor "%s".') % data['name']
@ -82,29 +111,8 @@ class CreateFlavor(forms.SelfHandlingForm):
class EditFlavor(CreateFlavor):
flavor_id = forms.CharField(widget=forms.widgets.HiddenInput)
def clean_name(self):
return self.cleaned_data['name']
def clean(self):
cleaned_data = super(EditFlavor, self).clean()
name = cleaned_data.get('name')
flavor_id = cleaned_data.get('flavor_id')
try:
flavors = api.nova.flavor_list(self.request)
except:
flavors = []
msg = _('Unable to get flavor list')
exceptions.check_message(["Connection", "refused"], msg)
raise
# Check if there is no flavor with the same name
if flavors is not None:
for flavor in flavors:
if flavor.name == name and flavor.id != flavor_id:
raise forms.ValidationError(
_('The name "%s" is already used by another flavor.')
% name
)
return cleaned_data
def clean_flavor_id(self):
return self.data.get('flavor_id')
def handle(self, request, data):
try:

View File

@ -20,9 +20,11 @@ class FlavorsTests(test.BaseAdminViewTests):
flavor.ram,
flavor.vcpus,
flavor.disk,
flavorid=flavor.id,
swap=flavor.swap,
ephemeral=eph).AndReturn(flavor)
api.nova.flavor_list(IsA(http.HttpRequest))
api.nova.flavor_list(IsA(http.HttpRequest))
self.mox.ReplayAll()
url = reverse('horizon:admin:flavors:create')
@ -31,6 +33,7 @@ class FlavorsTests(test.BaseAdminViewTests):
self.assertTemplateUsed(resp, "admin/flavors/create.html")
data = {'name': flavor.name,
'flavor_id': flavor.id,
'vcpus': flavor.vcpus,
'memory_mb': flavor.ram,
'disk_gb': flavor.disk,
@ -172,8 +175,6 @@ class FlavorsTests(test.BaseAdminViewTests):
flavor_a.id).AndReturn(flavor_a)
# POST
api.nova.flavor_list(IsA(http.HttpRequest)) \
.AndReturn(self.flavors.list())
api.nova.flavor_get(IsA(http.HttpRequest),
flavor_a.id).AndReturn(flavor_a)
self.mox.ReplayAll()