分享

给horizon添加分配指定floating IP的功能

pig2 发表于 2014-1-3 02:34:24 [显示全部楼层] 只看大图 回帖奖励 阅读模式 关闭右栏 0 20567
本帖最后由 pig2 于 2014-1-3 02:58 编辑

适应版本:E

使用方法:

如下图所示,若要分配指定的floatingIP,可在里输入指定IP;若不要分配指定的floating IP,“IP地址”栏为空,直接点“分配IP”按钮。

20131029184237296.jpg


修改过程


1./usr/share/pyshared/horizon/dashboards/nova/access_and_security/floating_ips/forms.py:FloatingIpAllocate中添加一个变量address,address从horizon的页面上接受用户输入的floating ip的值;并在api函数中添加该变量。

  1. class FloatingIpAllocate(forms.SelfHandlingForm):
  2.     tenant_name = forms.CharField(widget=forms.HiddenInput())
  3.     pool = forms.ChoiceField(label=_("Pool"))
  4.     address = forms.CharField(required=False,label=_("IP Address"))
  5.     def __init__(self, *args, **kwargs):
  6.         super(FloatingIpAllocate, self).__init__(*args, **kwargs)
  7.         floating_pool_list = kwargs.get('initial', {}).get('pool_list', [])
  8.         self.fields['pool'].choices = floating_pool_list
  9.         #self.fields['address'].choices = None
  10.     def handle(self, request, data):
  11.         try:
  12.             fip = api.tenant_floating_ip_allocate(request,
  13.                                                   pool=data.get('pool', None),address=data.get('address',None))
  14.             LOG.info('Allocating Floating IP "%s" to project "%s"'
  15.                      % (fip.ip, data['tenant_name']))
  16.             messages.success(request,
  17.                              _('Successfully allocated Floating IP "%(ip)s" '
  18.                                'to project "%(project)s"')
  19.                              % {"ip": fip.ip, "project": data['tenant_name']})
  20.         except:
  21.             exceptions.handle(request, _('Unable to allocate Floating IP.'))
  22.         return shortcuts.redirect(
  23.                         'horizon:nova:access_and_security:index')
复制代码

2. /usr/share/pyshared/horizon/api/nova.py添加address变量

  1. def tenant_floating_ip_allocate(request, pool=None, address=None):
  2.     """Allocates a floating ip to tenant. Optionally you may provide a pool
  3.     for which you would like the IP.
  4.     """
  5.     """
  6. return novaclient(request).floating_ips.create(pool=pool,address=address)
复制代码
3./usr/share/pyshared/novaclient/v1_1/floating_ips.py:FloatingIPManager.create()

添加address变量

  1.     def create(self, pool=None, address=None):
  2.         """
  3.         Create (allocate) a  floating ip for a tenant
  4.         """
  5.         return self._create("/os-floating-ips", {'pool': pool,'address':address}, "floating_ip")
复制代码

4./usr/share/pyshared/nova/api/openstack/compute/contrib/floating_ips.py:FloatIPController.create()添加address变量

  1. def create(self, req, body=None):
  2.         context = req.environ['nova.context']
  3.         authorize(context)
  4.         pool = None
  5.         address_your= None
  6.         if body and 'pool' in body:
  7.             pool = body['pool']
  8.         if body and 'address' in body:
  9.             address_your = body['address']
  10.         LOG.debug(_('ozg.debug xxxxxxxxxxxxxxxxx address = %s'), address_your)
  11.         try:
  12.             address = self.network_api.allocate_floating_ip(context, pool, address_your)
  13.             ip = self.network_api.get_floating_ip_by_address(context, address)
  14.         except rpc_common.RemoteError as ex:
  15.             # NOTE(tr3buchet) - why does this block exist?
  16.             if ex.exc_type == 'NoMoreFloatingIps':
  17.                 if pool:
  18.                     msg = _("No more floating ips in pool %s.") % pool
  19.                 else:
  20.                     msg = _("No more floating ips available.")
  21.                 raise webob.exc.HTTPBadRequest(explanation=msg)
  22.             else:
  23.                 raise
  24.         return _translate_floating_ip_view(ip)
复制代码

5. /usr/share/pyshared/nova/network/api.py.allocate_floating_ip()添加address变量


  1.     def allocate_floating_ip(self, context, pool=None, address_your=None):
  2.         """Adds a floating ip to a project from a pool. (allocates)"""
  3.         # NOTE(vish): We don't know which network host should get the ip
  4.         #             when we allocate, so just send it to any one.  This
  5.         #             will probably need to move into a network supervisor
  6.         #             at some point.
  7.         # ozg
  8.         LOG.debug(_("ozg.debug xxxxxxx rpc.call!!!"))
  9.         return rpc.call(context,
  10.                         FLAGS.network_topic,
  11.                         {'method': 'allocate_floating_ip',
  12.                          'args': {'project_id': context.project_id,
  13.                                   'pool': pool,
  14.                                   'address_your': address_your}})
复制代码



6. /usr/share/pyshared/nova/network/manager.py.allocate_floating_ip()添加address变量


  1. @wrap_check_policy
  2.     def allocate_floating_ip(self, context, project_id, pool=None, address_your=None):
  3.         """Gets a floating ip from the pool."""
  4.         # NOTE(tr3buchet): all network hosts in zone now use the same pool
  5.         LOG.debug("QUOTA: %s" % quota.allowed_floating_ips(context, 1))
  6.         if quota.allowed_floating_ips(context, 1) < 1:
  7.             LOG.warn(_('Quota exceeded for %s, tried to allocate address'),
  8.                      context.project_id)
  9.             raise exception.QuotaError(code='AddressLimitExceeded')
  10.         pool = pool or FLAGS.default_floating_pool
  11.         LOG.debug(_('ozg.debug network manager accept the messages xxxxxxxxxxxxx, address_your = %s'), address_your)
  12.         return self.db.floating_ip_allocate_address(context,
  13.                                                     project_id,
  14.                                                     pool,
  15.                                                     address_your)
复制代码




7. /usr/share/pyshared/nova/db/api.py. floating_ip_allocate_address()添加address变量

  1. def floating_ip_allocate_address (context, project_id, pool, address_your=None):
  2.     """Allocate free floating ip from specified pool and return the address.
  3.     Raises if one is not available.
  4.     ozg add address_your
  5.     """
  6. return IMPL.floating_ip_allocate_address(context, project_id, pool, address_your)
复制代码




8./usr/share/pyshared/nova/db/sqlalchemy/api.py

将floating_ip_allocate_address函数改为如下:

  1. @require_context
  2. def floating_ip_allocate_address(context, project_id, pool, address_your):
  3.     authorize_project_context(context, project_id)
  4.     session = get_session()
  5.     LOG.debug(_("ozg.debug xxxxxxx db.sql... address_your = %s"), address_your)
  6.     with session.begin():
  7.         if not address_your:
  8.             floating_ip_ref = model_query(context, models.FloatingIp,
  9.                                           session=session, read_deleted="no").\
  10.                                       filter_by(fixed_ip_id=None).\
  11.                                       filter_by(project_id=None).\
  12.                                       filter_by(pool=pool).\
  13.                                       with_lockmode('update').\
  14.                                       first()
  15.         else:
  16.             floating_ip_ref = model_query(context, models.FloatingIp,
  17.                                           session=session, read_deleted="no").\
  18.                                       filter_by(fixed_ip_id=None).\
  19.                                       filter_by(project_id=None).\
  20.                                       filter_by(pool=pool).\
  21.                                       filter_by(address=address_your).\
  22.                                       with_lockmode('update').\
  23.                                       first()
  24.         # NOTE(vish): if with_lockmode isn't supported, as in sqlite,
  25.         #             then this has concurrency issues
  26.         if not floating_ip_ref:
  27.             raise exception.NoMoreFloatingIps()
  28.         floating_ip_ref['project_id'] = project_id
  29.         session.add(floating_ip_ref)
  30.     return floating_ip_ref['address']
复制代码

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条