Adding record-editor fip dropdown

This change adds an option drop down of floating ip addresses to use
for record creation / updates.  This simplifies the record setup process.

Change-Id: I042956268b254ec909a8a3f922836df5c1b8f981
Closes-bug: #1462022
This commit is contained in:
eric 2015-06-18 15:16:01 -06:00
parent 483c442d26
commit 5d43bcce2a
4 changed files with 89 additions and 5 deletions

View File

@ -39,3 +39,8 @@ Howto
'dns': 'designate_policy.json',
4. (Optional) Within your horizon settings file(s) (either the local settings or the other settings.py), add
the line below. This will make it so the record create/update screen uses a drop down of your floating ip
addresses instead of a free form text field::
DESIGNATE = { 'records_use_fips': True }

View File

@ -24,6 +24,8 @@ from horizon import forms
from horizon import messages
from designatedashboard import api
from designatedashboard.dashboards.project.dns_domains.utils\
import limit_records_to_fips
LOG = logging.getLogger(__name__)
@ -257,6 +259,16 @@ class RecordForm(forms.SelfHandlingForm):
}),
)
ip_addr = forms.ChoiceField(
required=False,
widget=forms.Select(attrs={
'class': 'switched',
'data-switch-on': 'record_type',
'data-record_type-a': _('IP Address'),
'data-record_type-aaaa': _('IP Address'),
}),
)
txt = forms.CharField(
label=_('TXT'),
required=False,
@ -309,6 +321,42 @@ class RecordForm(forms.SelfHandlingForm):
widget=forms.Textarea(),
)
def __init__(self, request, *args, **kwargs):
super(RecordForm, self).__init__(request, *args, **kwargs)
initial = kwargs.get('initial', {})
if limit_records_to_fips():
del self.fields['data'].widget.attrs['data-record_type-a']
del self.fields['data'].widget.attrs['data-record_type-aaaa']
self.fields['ip_addr'].choices = \
self.populate_ip_addr_choices(request,
initial)
else:
del self.fields['ip_addr']
def _generate_fip_list(self, fips, instances):
instance_dict = {instance.id: instance for instance in instances}
for fip in fips:
instance_name = _("Unknown instance name")
if getattr(fip, "instance_id", "None") in instance_dict:
instance_name = instance_dict[getattr(fip, "instance_id")].name
yield (fip.ip, "%s (%s)" % (fip.ip, instance_name))
def populate_ip_addr_choices(self, request, initial):
results = [(None, _('Select an IP')), ]
if (initial.get('ip_addr') and
initial['ip_addr'] not in [fip.ip for fip in initial['fips']]):
"""The record is currently using an ip not in the list
of fips - this can happen when instance goes away or in
multi region setups
"""
results.append((initial['ip_addr'], initial['ip_addr']))
results.extend(self._generate_fip_list(initial['fips'],
initial['instances']))
if len(results) == 1:
messages.warning(request, _("There are no floating IP addresses "
"currently in use to select from."))
return results
def clean_type(self):
'''Type value needs to be uppercased before it is sent to the API.'''
return self.cleaned_data['type'].upper()
@ -324,6 +372,10 @@ class RecordForm(forms.SelfHandlingForm):
cleaned_data = super(RecordForm, self).clean()
record_type = cleaned_data['type']
domain_name = cleaned_data['domain_name']
if limit_records_to_fips():
ip_addr = cleaned_data.pop('ip_addr')
if (record_type in ['AAAA', 'A'] and limit_records_to_fips()):
cleaned_data['data'] = ip_addr
# Name field
if self._is_field_blank(cleaned_data, 'name'):

View File

@ -0,0 +1,20 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.conf import settings
def limit_records_to_fips():
# This method checks the settings to determine if the
# record creation / update screen should limit the ip input
# to be a dropdown of floating ips
return getattr(settings, "DESIGNATE",
{}).get("records_use_fips", False)

View File

@ -19,6 +19,9 @@ from horizon import forms
from horizon import tables
from horizon.views import HorizonTemplateView # noqa
from openstack_dashboard.api.network import tenant_floating_ip_list
from openstack_dashboard.api.nova import server_list
from designatedashboard import api
from .forms import DomainCreate # noqa
@ -27,6 +30,7 @@ from .forms import RecordCreate # noqa
from .forms import RecordUpdate # noqa
from .tables import DomainsTable # noqa
from .tables import RecordsTable # noqa
from .utils import limit_records_to_fips # noqa
class IndexView(tables.DataTableView):
@ -144,11 +148,12 @@ class BaseRecordFormView(forms.ModalFormView):
def get_initial(self):
self.domain = self.get_domain()
return {
'domain_id': self.domain.id,
'domain_name': self.domain.name,
}
results = {'domain_id': self.domain.id,
'domain_name': self.domain.name, }
if limit_records_to_fips():
results.update({'fips': tenant_floating_ip_list(self.request),
'instances': server_list(self.request)[0]})
return results
def get_context_data(self, **kwargs):
context = super(BaseRecordFormView, self).get_context_data(**kwargs)
@ -215,6 +220,8 @@ class UpdateRecordView(BaseRecordFormView):
'type': self.record.type.lower(),
'description': self.record.description,
})
if limit_records_to_fips():
initial.update({'ip_addr': self.record.data})
return initial