Use MultiIPField for access form to avoid need for custom validation

Change-Id: If3314e94319056fe45499e83ff1bb15c4d19c64c
This commit is contained in:
Sam Morrison 2021-02-23 16:18:43 +11:00 committed by Lingxian Kong
parent e8d81f17d3
commit 42d6638d3b
2 changed files with 7 additions and 19 deletions

View File

@ -7,4 +7,3 @@ oslo.log>=3.30.0 # Apache-2.0
python-swiftclient>=2.2.0
python-troveclient>=1.2.0
horizon>=17.1.0 # Apache-2.0
netaddr>=0.7.18 # BSD

View File

@ -15,7 +15,6 @@
from django.conf import settings
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
import netaddr
from horizon import exceptions
from horizon import forms
@ -293,29 +292,19 @@ class AddAccessAction(workflows.Action):
"""
is_public = forms.BooleanField(label=_("Is Public"),
required=False)
allowed_cidrs = forms.CharField(label=_("Allowed CIDRs"),
required=False,
help_text=_("Comma-separated CIDRs "
"to connect through."))
allowed_cidrs = forms.MultiIPField(label=_("Allowed CIDRs"),
required=False,
version=forms.IPv4 | forms.IPv6,
mask=True,
widget=forms.TextInput(),
help_text=_("Comma-separated CIDRs "
"to connect through."))
class Meta(object):
name = _("Database Access")
permissions = TROVE_ADD_PERMS
help_text_template = "project/databases/_launch_access_help.html"
def clean(self):
cleaned_data = super(AddAccessAction, self).clean()
if cleaned_data.get('allowed_cidrs'):
cidrs = cleaned_data.get('allowed_cidrs').split(',')
for cidr in cidrs:
try:
netaddr.IPNetwork(cidr)
except netaddr.AddrFormatError:
msg = _('Invalid Allowed CIDR provided.')
self._errors["allowed_cidrs"] = self.error_class([msg])
return cleaned_data
class DatabaseAccess(workflows.Step):
action_class = AddAccessAction