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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. def __init__(self, *args, **kwargs):
  28. super(FloatingIpAllocate, self).__init__(*args, **kwargs)
  29. floating_pool_list = kwargs.get('initial', {}).get('pool_list', [])
  30. self.fields['pool'].choices = floating_pool_list
  31. def handle(self, request, data):
  32. try:
  33. # Prevent allocating more IP than the quota allows
  34. usages = quotas.tenant_quota_usages(request,
  35. targets=('floating_ips', ))
  36. if usages['floating_ips']['available'] <= 0:
  37. error_message = _('You are already using all of your available'
  38. ' floating IPs.')
  39. self.api_error(error_message)
  40. return False
  41. fip = api.neutron.tenant_floating_ip_allocate(request,
  42. pool=data['pool'])
  43. messages.success(request,
  44. _('Allocated Floating IP %(ip)s.')
  45. % {"ip": fip.ip})
  46. return fip
  47. except Exception:
  48. exceptions.handle(request, _('Unable to allocate Floating IP.'))