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.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. description = forms.CharField(max_length=255,
  33. label=_("Description"),
  34. required=False)
  35. def __init__(self, *args, **kwargs):
  36. super(AdminFloatingIpAllocate, self).__init__(*args, **kwargs)
  37. floating_pool_list = kwargs.get('initial', {}).get('pool_list', [])
  38. self.fields['pool'].choices = floating_pool_list
  39. tenant_list = kwargs.get('initial', {}).get('tenant_list', [])
  40. self.fields['tenant'].choices = tenant_list
  41. def handle(self, request, data):
  42. try:
  43. # Admin ignore quota
  44. param = {}
  45. if data['floating_ip_address']:
  46. param['floating_ip_address'] = data['floating_ip_address']
  47. if data['description']:
  48. param['description'] = data['description']
  49. subnet = api.neutron.subnet_get(request, data['pool'])
  50. param['subnet_id'] = subnet.id
  51. fip = api.neutron.tenant_floating_ip_allocate(
  52. request,
  53. pool=subnet.network_id,
  54. tenant_id=data['tenant'],
  55. **param)
  56. messages.success(
  57. request,
  58. _('Allocated floating IP %(ip)s.') % {"ip": fip.ip})
  59. return fip
  60. except Exception:
  61. redirect = reverse('horizon:admin:floating_ips:index')
  62. msg = _('Unable to allocate floating IP.')
  63. exceptions.handle(request, msg, redirect=redirect)