OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forms.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2016 Letv Cloud Computing
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from django.core.urlresolvers import reverse
  16. from django.utils.translation import ugettext_lazy as _
  17. from horizon import exceptions
  18. from horizon import forms
  19. from horizon import messages
  20. from openstack_dashboard import api
  21. class AdminFloatingIpAllocate(forms.SelfHandlingForm):
  22. pool = forms.ThemableChoiceField(label=_("Pool"))
  23. tenant = forms.ThemableChoiceField(label=_("Project"))
  24. floating_ip_address = forms.IPField(
  25. label=_("Floating IP Address (optional)"),
  26. required=False,
  27. initial="",
  28. help_text=_("The IP address of the new floating IP (e.g. 202.2.3.4). "
  29. "You need to specify an explicit address which is under "
  30. "the public network CIDR (e.g. 202.2.3.0/24)."),
  31. mask=False)
  32. def __init__(self, *args, **kwargs):
  33. super(AdminFloatingIpAllocate, self).__init__(*args, **kwargs)
  34. floating_pool_list = kwargs.get('initial', {}).get('pool_list', [])
  35. self.fields['pool'].choices = floating_pool_list
  36. tenant_list = kwargs.get('initial', {}).get('tenant_list', [])
  37. self.fields['tenant'].choices = tenant_list
  38. def handle(self, request, data):
  39. try:
  40. # Admin ignore quota
  41. param = {}
  42. if data['floating_ip_address']:
  43. param['floating_ip_address'] = data['floating_ip_address']
  44. subnet = api.neutron.subnet_get(request, data['pool'])
  45. param['subnet_id'] = subnet.id
  46. fip = api.neutron.tenant_floating_ip_allocate(
  47. request,
  48. pool=subnet.network_id,
  49. tenant_id=data['tenant'],
  50. **param)
  51. messages.success(
  52. request,
  53. _('Allocated floating IP %(ip)s.') % {"ip": fip.ip})
  54. return fip
  55. except Exception:
  56. redirect = reverse('horizon:admin:floating_ips:index')
  57. msg = _('Unable to allocate floating IP.')
  58. exceptions.handle(request, msg, redirect=redirect)