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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2012 United States Government as represented by the
  2. # Administrator of the National Aeronautics and Space Administration.
  3. # All Rights Reserved.
  4. #
  5. # Copyright 2012 Nebula, Inc.
  6. # Copyright (c) 2012 X.commerce, a business unit of eBay Inc.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License. You may obtain
  10. # a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. from django.utils.translation import ugettext_lazy as _
  20. from horizon import exceptions
  21. from horizon import forms
  22. from horizon import messages
  23. from openstack_dashboard import api
  24. from openstack_dashboard.usage import quotas
  25. class FloatingIpAllocate(forms.SelfHandlingForm):
  26. pool = forms.ThemableChoiceField(label=_("Pool"))
  27. description = forms.CharField(max_length=255,
  28. label=_("Description"),
  29. required=False)
  30. def __init__(self, *args, **kwargs):
  31. super(FloatingIpAllocate, self).__init__(*args, **kwargs)
  32. floating_pool_list = kwargs.get('initial', {}).get('pool_list', [])
  33. self.fields['pool'].choices = floating_pool_list
  34. def handle(self, request, data):
  35. try:
  36. # Prevent allocating more IP than the quota allows
  37. usages = quotas.tenant_quota_usages(request,
  38. targets=('floating_ips', ))
  39. if usages['floating_ips']['available'] <= 0:
  40. error_message = _('You are already using all of your available'
  41. ' floating IPs.')
  42. self.api_error(error_message)
  43. return False
  44. param = {}
  45. if data['description']:
  46. param['description'] = data['description']
  47. fip = api.neutron.tenant_floating_ip_allocate(
  48. request,
  49. pool=data['pool'],
  50. **param)
  51. messages.success(request,
  52. _('Allocated Floating IP %(ip)s.')
  53. % {"ip": fip.ip})
  54. return fip
  55. except Exception:
  56. exceptions.handle(request, _('Unable to allocate Floating IP.'))