Skip to content

Commit

Permalink
Validates port range and displayed non-field errors.
Browse files Browse the repository at this point in the history
Fixes bug 898374 and fixes bug 943004.

Change-Id: Ifffce054db78b23edcb6e1ae85e15d39afde5b26
  • Loading branch information
gabrielhurley committed Feb 29, 2012
1 parent b85d1aa commit 4b33334
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Expand Up @@ -23,6 +23,7 @@
from django import shortcuts
from django.contrib import messages
from django.core import validators
from django.forms import ValidationError
from django.utils.translation import ugettext as _
from novaclient import exceptions as novaclient_exceptions

Expand Down Expand Up @@ -84,10 +85,16 @@ class AddRule(forms.SelfHandlingForm):
security_group_id = forms.IntegerField(widget=forms.HiddenInput())
tenant_id = forms.CharField(widget=forms.HiddenInput())

def clean(self):
cleaned_data = super(AddRule, self).clean()
if cleaned_data["to_port"] < cleaned_data["from_port"]:
msg = _('The "to" port number must be greater than or equal to '
'the "from" port number.')
raise ValidationError(msg)
return cleaned_data

def handle(self, request, data):
try:
LOG.info('Add security_group_rule: "%s"' % data)

rule = api.security_group_rule_create(request,
data['security_group_id'],
data['ip_protocol'],
Expand Down
Expand Up @@ -130,6 +130,26 @@ def test_edit_rules_add_rule(self):
res = self.client.post(self.edit_url, formData)
self.assertRedirectsNoFollow(res, INDEX_URL)

def test_edit_rules_invalid_port_range(self):
sec_group = self.security_groups.first()
rule = self.security_group_rules.first()

self.mox.StubOutWithMock(api, 'security_group_get')
api.security_group_get(IsA(http.HttpRequest),
sec_group.id).AndReturn(sec_group)
self.mox.ReplayAll()

formData = {'method': 'AddRule',
'tenant_id': self.tenant.id,
'security_group_id': sec_group.id,
'from_port': rule.from_port,
'to_port': int(rule.from_port) - 1,
'ip_protocol': rule.ip_protocol,
'cidr': rule.ip_range['cidr']}
res = self.client.post(self.edit_url, formData)
self.assertNoMessages()
self.assertContains(res, "greater than or equal to")

def test_edit_rules_add_rule_exception(self):
sec_group = self.security_groups.first()
rule = self.security_group_rules.first()
Expand Down
Expand Up @@ -23,12 +23,11 @@
"""
import logging

from django.contrib import messages
from django import shortcuts
from django.utils.translation import ugettext as _
from novaclient import exceptions as novaclient_exceptions

from horizon import api
from horizon import exceptions
from horizon import forms
from horizon import tables
from .forms import CreateGroup, AddRule
Expand All @@ -49,12 +48,11 @@ def get_data(self):
security_group_id)
rules = [api.nova.SecurityGroupRule(rule) for
rule in self.object.rules]
except novaclient_exceptions.ClientException, e:
except:
self.object = None
rules = []
LOG.exception("ClientException in security_groups rules edit")
messages.error(self.request,
_('Error getting security_group: %s') % e)
exceptions.handle(self.request,
_('Unable to retrieve security group.'))
return rules

def handle_form(self):
Expand Down
5 changes: 5 additions & 0 deletions horizon/horizon/templates/horizon/common/_form_fields.html
@@ -1,6 +1,11 @@
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-message error">
{{ form.non_field_errors }}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-field clearfix{% if field.errors %} error{% endif %}">
{{ field.label_tag }}
Expand Down

0 comments on commit 4b33334

Please sign in to comment.