From 9edcc59af4df3098d95e7fe764557571cc9985a0 Mon Sep 17 00:00:00 2001 From: Andy Chong Date: Fri, 2 Mar 2012 18:47:16 +0800 Subject: [PATCH] Return proper error message when adding security group with no params * fixes bug 944469 Change-Id: I03ba89de135a2d7e2e9f6359ec652b069e55d089 --- AUTHORS | 1 + .../security_groups/forms.py | 17 ++++++++++++++--- horizon/utils/validators.py | 6 ++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index cfb4f29a3..13c4ce741 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,5 +1,6 @@ aababilov Andrews Medina +Andy Chong Anthony Young Arvind Somya Carlo Truijllo diff --git a/horizon/dashboards/nova/access_and_security/security_groups/forms.py b/horizon/dashboards/nova/access_and_security/security_groups/forms.py index 0eaf05a6f..22f42b09a 100644 --- a/horizon/dashboards/nova/access_and_security/security_groups/forms.py +++ b/horizon/dashboards/nova/access_and_security/security_groups/forms.py @@ -31,6 +31,7 @@ from horizon import api from horizon import exceptions from horizon import forms from horizon.utils.validators import validate_ipv4_cidr +from horizon.utils.validators import validate_port_range LOG = logging.getLogger(__name__) @@ -68,7 +69,8 @@ class AddRule(forms.SelfHandlingForm): "in the range (-1: 255)"), widget=forms.TextInput( attrs={'data': _('From port'), - 'data-icmp': _('Type')})) + 'data-icmp': _('Type')}), + validators=[validate_port_range]) to_port = forms.IntegerField(label=_("To port"), help_text=_("TCP/UDP: Enter integer value " "between 1 and 65535. ICMP: " @@ -76,7 +78,8 @@ class AddRule(forms.SelfHandlingForm): "in the range (-1: 255)"), widget=forms.TextInput( attrs={'data': _('To port'), - 'data-icmp': _('Code')})) + 'data-icmp': _('Code')}), + validators=[validate_port_range]) cidr = forms.CharField(label=_("CIDR"), help_text=_("Classless Inter-Domain Routing " "(i.e. 192.168.0.0/24"), @@ -89,7 +92,15 @@ class AddRule(forms.SelfHandlingForm): def clean(self): cleaned_data = super(AddRule, self).clean() - if cleaned_data["to_port"] < cleaned_data["from_port"]: + to_port = cleaned_data.get("to_port", None) + from_port = cleaned_data.get("from_port", None) + if to_port == None: + msg = _('The "to" port number must not be empty.') + raise ValidationError(msg) + if from_port == None: + msg = _('The "from" port number must not be empty.') + raise ValidationError(msg) + if to_port < from_port: msg = _('The "to" port number must be greater than or equal to ' 'the "from" port number.') raise ValidationError(msg) diff --git a/horizon/utils/validators.py b/horizon/utils/validators.py index 372a75865..fa2082b47 100644 --- a/horizon/utils/validators.py +++ b/horizon/utils/validators.py @@ -17,6 +17,7 @@ import re from django.core import validators +from django.core.exceptions import ValidationError ipv4_cidr_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)' # 0-255 @@ -25,3 +26,8 @@ ipv4_cidr_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)' # 0-255 validate_ipv4_cidr = validators.RegexValidator(ipv4_cidr_re) + + +def validate_port_range(port): + if port not in range(-1, 65535): + raise ValidationError(u'%d is not a valid port number' % port)