Filter current flavor in resize instance/database
Removed the validation that checks if the new flavor selected is the same as the old flavor, this case now will never happen since the current flavor is removed from the choices. Change-Id: I1c41a48fc9805b1b730dec5f13a43b73989b349b Closes-Bug: #1395777
This commit is contained in:
parent
3a492dd1ea
commit
3bad59c234
@ -68,22 +68,17 @@ class ResizeInstanceForm(forms.SelfHandlingForm):
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
super(ResizeInstanceForm, self).__init__(request, *args, **kwargs)
|
||||
|
||||
old_flavor_id = kwargs.get('initial', {}).get('old_flavor_id')
|
||||
choices = kwargs.get('initial', {}).get('flavors')
|
||||
# Remove current flavor from the list of flavor choices
|
||||
choices = [(flavor_id, name) for (flavor_id, name) in choices
|
||||
if flavor_id != old_flavor_id]
|
||||
if choices:
|
||||
choices.insert(0, ("", _("Select a new flavor")))
|
||||
else:
|
||||
choices.insert(0, ("", _("No flavors available")))
|
||||
self.fields['new_flavor'].choices = choices
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(ResizeInstanceForm, self).clean()
|
||||
flavor = cleaned_data.get('new_flavor', None)
|
||||
|
||||
if flavor is None or flavor == self.initial['old_flavor_id']:
|
||||
raise forms.ValidationError(_('Please choose a new flavor that '
|
||||
'is not the same as the old one.'))
|
||||
return cleaned_data
|
||||
|
||||
def handle(self, request, data):
|
||||
instance = data.get('instance_id')
|
||||
flavor = data.get('new_flavor')
|
||||
|
@ -388,6 +388,31 @@ class DatabaseTests(test.TestCase):
|
||||
self.assertContains(
|
||||
res, "New size for volume must be greater than current size.")
|
||||
|
||||
@test.create_stubs(
|
||||
{api.trove: ('instance_get',
|
||||
'flavor_list')})
|
||||
def test_resize_instance_get(self):
|
||||
database = self.databases.first()
|
||||
|
||||
# views.py: DetailView.get_data
|
||||
api.trove.instance_get(IsA(http.HttpRequest), database.id)\
|
||||
.AndReturn(database)
|
||||
api.trove.flavor_list(IsA(http.HttpRequest)).\
|
||||
AndReturn(self.database_flavors.list())
|
||||
|
||||
self.mox.ReplayAll()
|
||||
url = reverse('horizon:project:databases:resize_instance',
|
||||
args=[database.id])
|
||||
|
||||
res = self.client.get(url)
|
||||
self.assertTemplateUsed(res, 'project/databases/resize_instance.html')
|
||||
option = '<option value="%s">%s</option>'
|
||||
for flavor in self.database_flavors.list():
|
||||
if flavor.id == database.flavor['id']:
|
||||
self.assertNotContains(res, option % (flavor.id, flavor.name))
|
||||
else:
|
||||
self.assertContains(res, option % (flavor.id, flavor.name))
|
||||
|
||||
@test.create_stubs(
|
||||
{api.trove: ('instance_get',
|
||||
'flavor_list',
|
||||
@ -420,29 +445,3 @@ class DatabaseTests(test.TestCase):
|
||||
res = self.client.post(url, post)
|
||||
self.assertNoFormErrors(res)
|
||||
self.assertRedirectsNoFollow(res, INDEX_URL)
|
||||
|
||||
@test.create_stubs(
|
||||
{api.trove: ('instance_get', 'flavor_list')})
|
||||
def test_resize_instance_bad_value(self):
|
||||
database = self.databases.first()
|
||||
|
||||
api.trove.instance_get(IsA(http.HttpRequest),
|
||||
database.id).AndReturn(database)
|
||||
api.trove.flavor_list(IsA(http.HttpRequest)).\
|
||||
AndReturn(self.database_flavors.list())
|
||||
|
||||
old_flavor = self.database_flavors.list()[0]
|
||||
|
||||
self.mox.ReplayAll()
|
||||
url = reverse('horizon:project:databases:resize_instance',
|
||||
args=[database.id])
|
||||
post = {
|
||||
'instance_id': database.id,
|
||||
'old_flavor_name': old_flavor.name,
|
||||
'old_flavor_id': old_flavor.id,
|
||||
'new_flavor': old_flavor.id
|
||||
}
|
||||
res = self.client.post(url, post)
|
||||
self.assertContains(res,
|
||||
"Please choose a new flavor that is "
|
||||
"not the same as the old one.")
|
||||
|
@ -3474,6 +3474,13 @@ class InstanceTests(helpers.TestCase):
|
||||
config_drive_field_label = 'Configuration Drive'
|
||||
self.assertNotContains(res, config_drive_field_label)
|
||||
|
||||
option = '<option value="%s">%s</option>'
|
||||
for flavor in self.flavors.list():
|
||||
if flavor.id == server.flavor['id']:
|
||||
self.assertNotContains(res, option % (flavor.id, flavor.name))
|
||||
else:
|
||||
self.assertContains(res, option % (flavor.id, flavor.name))
|
||||
|
||||
@helpers.create_stubs({api.nova: ('server_get',
|
||||
'flavor_list',)})
|
||||
def test_instance_resize_get_server_get_exception(self):
|
||||
@ -3552,7 +3559,9 @@ class InstanceTests(helpers.TestCase):
|
||||
@helpers.create_stubs(instance_resize_post_stubs)
|
||||
def test_instance_resize_post(self):
|
||||
server = self.servers.first()
|
||||
flavor = self.flavors.first()
|
||||
flavors = [flavor for flavor in self.flavors.list()
|
||||
if flavor.id != server.flavor['id']]
|
||||
flavor = flavors[0]
|
||||
|
||||
api.nova.server_get(IsA(http.HttpRequest), server.id) \
|
||||
.AndReturn(server)
|
||||
@ -3573,7 +3582,9 @@ class InstanceTests(helpers.TestCase):
|
||||
@helpers.create_stubs(instance_resize_post_stubs)
|
||||
def test_instance_resize_post_api_exception(self):
|
||||
server = self.servers.first()
|
||||
flavor = self.flavors.first()
|
||||
flavors = [flavor for flavor in self.flavors.list()
|
||||
if flavor.id != server.flavor['id']]
|
||||
flavor = flavors[0]
|
||||
|
||||
api.nova.server_get(IsA(http.HttpRequest), server.id) \
|
||||
.AndReturn(server)
|
||||
|
@ -44,17 +44,12 @@ class SetFlavorChoiceAction(workflows.Action):
|
||||
help_text_template = ("project/instances/"
|
||||
"_flavors_and_quotas.html")
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(SetFlavorChoiceAction, self).clean()
|
||||
flavor = cleaned_data.get('flavor', None)
|
||||
|
||||
if flavor is None or flavor == cleaned_data['old_flavor_id']:
|
||||
raise forms.ValidationError(_('Please choose a new flavor that '
|
||||
'is not the same as the old one.'))
|
||||
return cleaned_data
|
||||
|
||||
def populate_flavor_choices(self, request, context):
|
||||
old_flavor_id = context.get('old_flavor_id')
|
||||
flavors = context.get('flavors').values()
|
||||
|
||||
# Remove current flavor from the list of flavor choices
|
||||
flavors = [flavor for flavor in flavors if flavor.id != old_flavor_id]
|
||||
if len(flavors) > 1:
|
||||
flavors = instance_utils.sort_flavor_list(request, flavors)
|
||||
if flavors:
|
||||
|
Loading…
Reference in New Issue
Block a user