前两篇文章讨论了怎么写一个 Neutron 的插件。但是最基本的插件只包括 Network, Port,和 Subnet 三种资源。如果需要引入新的资源,比如一个二层的 gateway 的话,就需要在插件的基础上再写一个 extension, 也就是扩展。

Neutron 已经预定义了很多扩展,可以参看 neutron/extensions 下面的文件,我在这里就不一一列举了。如果正好有一种是你需要的,那直接拿过来用就好了。如果需要自己从头搭起的话,可以现在 自己的 plugin 文件夹下面创建一个 extensions 文件夹,然后在这个文件夹下面创建两个文件: __init__.py 和 myextension.py:

- neutron/

  - plugins/

    - myplugin/

      - __init__.py

      - plugin.py

      - extensions/

        - __init__.py

        - myextension.py

__init__.py 是空的就行了。在 myextension.py 中需要定义两个东西:一个叫RESOURCE_ATTRIBUTE_MAP 的词典和一个叫 Myextension 的类。RESOURCE_ATTRIBUTE_MAP里面放的就是你的这个新扩展的属性,例如:

RESOURCE_ATTRIBUTE_MAP = {
'myextensions': {
'id': {'allow_post': False, 'allow_put': False,
'is_visible': True},
'name': {'allow_post': True, 'allow_put': True,
'is_visible': True},
'tenant_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:string': None},
'required_by_policy': True,
'is_visible': True}
}
}

需要注意的是在词典中,第一层的 key ‘myextensions’ 就是文件名 ’myextension‘ 加上一个 ‘s'。第二层的 keys ’id‘, ’name‘, ’tenant_id‘ 就是这个扩展的三个属性。第三层的 keys 在 neutron/api/v2/attributes.py 中有比较详细的解释,我把它搬到这里来了:

# The following is a short reference for understanding attribute info:
# default: default value of the attribute (if missing, the attribute
# becomes mandatory.
# allow_post: the attribute can be used on POST requests.
# allow_put: the attribute can be used on PUT requests.
# validate: specifies rules for validating data in the attribute.
# convert_to: transformation to apply to the value before it is returned
# is_visible: the attribute is returned in GET responses.
# required_by_policy: the attribute is required by the policy engine and
# should therefore be filled by the API layer even if not present in
# request body.
# enforce_policy: the attribute is actively part of the policy enforcing
# mechanism, ie: there might be rules which refer to this attribute.

定义新类的时候需要注意一点,这个类的名字与包含这个类的文件名的唯一区别必须是一个首字母大写,另一个首字母小写。也就是说把MyExtension当做类的名字可能就会导致出错。具体原因可以参考 neutron/api/extensions.py 中 ExtensionManager 的_load_all_extensions_from_path 方法的实现。Myextension 这个类可以继承 neutron/api/extensions.py 这个文件中的一个类:ExtensionDescriptor,也可以自己定义。下面给出一个继承了该类的定义:

from neutron.api import extensions
from neutron import manager
from neutron.api.v2 import base
class Myextension(extensions.ExtensionDescriptor):
# The name of this class should be the same as the file name
# The first letter must be changed from lower case to upper case
# There are a couple of methods and their properties defined in the
# parent class of this class, ExtensionDescriptor you can check them @classmethod
def get_name(cls):
# You can coin a name for this extension
return "My Extension" @classmethod
def get_alias(cls):
# This alias will be used by your core_plugin class to load
# the extension
return "my-extensions" @classmethod
def get_description(cls):
# A small description about this extension
return "An extension defined by myself. Haha!" @classmethod
def get_namespace(cls):
# The XML namespace for this extension
return "http://docs.openstack.org/ext/myextension/api/v1.0" @classmethod
def get_updated(cls):
# Specify when was this extension last updated,
# good for management when there are changes in the design
return "2014-08-07T00:00:00-00:00" @classmethod
def get_resources(cls):
# This method registers the URL and the dictionary of
# attributes on the neutron-server.
exts = []
plugin = manager.NeutronManager.get_plugin()
resource_name = 'myextension'
collection_name = '%ss' % resource_name
params = RESOURCE_ATTRIBUTE_MAP.get(collection_name, dict())
controller = base.create_resource(collection_name, resource_name,
plugin, params, allow_bulk=False)
ex = extensions.ResourceExtension(collection_name, controller)
exts.append(ex)
return exts

到这一步为止,这个 myextension.py 文件基本就算是大功告成了,接下来需要去配置 /etc/neutron/neutron.conf 文件,告诉 Neutron 去哪里找到这个扩展。那么在该文件的[DEFAULT]下面,我们可以找到一个选项叫做api_extensions_path,并且把刚刚创建的 extensions 文件夹的位置赋给它,例如:

api_extensions_path = /usr/lib/python2.7/dist-packages/neutron/plugins/myplugin/extensions

然后在自己的 MyPlugin 类的定义中还要加一句话,告诉 Neutron 我的插件支持这个扩展。需要注意的是这里的方括号中的名字应该与上面 get_alias() 方法获得的名字一致。

class MyPlugin(db_base_plugin_v2.NeutronDbPluginV2):
  ...
  supported_extension_aliases = ['my-extensions']
  
  def __init__(self):
    ...
  ...

最后重启一下 neutron server, “service neutron-server restart”, 如果看到 /var/log/neutron/server.log 里面有 Loaded extension: my-extensions 的字样就说明成功了。

在接下来的一些文章中,我会继续讨论一下如何实现一个扩展的不同操作,如何在 CLI 中加入对自定义扩展的命令支持等内容。

怎样写 OpenStack Neutron 的 Extension (一)的更多相关文章

  1. 怎样写 OpenStack Neutron 的 Extension (四)

    上文说到需要在 /neutronclient/v2_0/myextension/extension.py 中分别定义五个 class:List/Show/Create/Delete/UpdateExt ...

  2. 怎样写 OpenStack Neutron 的 Extension (三)

    通过上几章的介绍,我们现在的 myplugin 文件夹看上去应该是这样的: - neutron/ - plugins/ - myplugin/ - __init__.py - plugin.py - ...

  3. 怎样写 OpenStack Neutron 的 Extension (二)

    接着之前一篇文章,再来谈谈 Extension 的具体实现问题.我使用的是本地数据库加远程API调用的方法,所以先要定义一下数据库中 myextension 如何存储.首先,我们可以在自己的 plug ...

  4. 怎样写 OpenStack Neutron 的 Plugin (二)

    其实上一篇博文中的内容已经涵盖了大部分写Neutron插件的技术问题,这里主要还遗留了一些有关插件的具体实现的问题. 首先,Neutron对最基本的三个资源:Network, Port 和 Subne ...

  5. 怎样写 OpenStack Neutron 的 Plugin (一)

    鉴于不知道Neutron的人也不会看这篇文章,而知道的人也不用我再啰嗦Neutron是什么东西,我决定跳过Neutron简介,直接爆料. 首先要介绍一下我的开发环境.我没有使用DevStack,而是直 ...

  6. OpenStack Neutron 之 Load Balance

    OpenStack Neutron 之 Load Balance 负载均衡(Load Balance)是 OpenStack Neutron 支持的功能之一.负载均衡能够将网络请求分发到多个实际处理请 ...

  7. how to read openstack code: action extension

    之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...

  8. [转]OpenStack Neutron运行机制解析概要

    转载自:http://panpei.net.cn/2013/12/04/openstack-neutron-mechanism-introduce/ 自从开学以来,玩OpenStack也已经3个月了, ...

  9. [转]OpenStack Neutron解析

    1.为什么还需要linux bridge的部署方式? 2.哪一个网桥起着交换机的作用? 3.neutron如何实现私有网络的隔离 =================================== ...

随机推荐

  1. 6天的巴厘岛旅行 I love Bali

    6天的巴厘岛旅游今天结束了,从第一天的踏进异国之域的新奇,到最后一天的将且回程的恋恋不舍,要记下的.不愿忘记的东西太多太多. 1.下午5点半抵达巴厘岛登巴萨国际机场,7点半才出机场,让Ricky导游和 ...

  2. 编译流程,C开发常见文件类型名

    编译流程 我们常说的编译是一个整体的概念,是指从源程序到可执行程序的整个过程,实际上,C语言编译的过程可以进一步细分为预编译->编译->汇编->链接 预编译是把include关键字所 ...

  3. 动手学习TCP:TCP连接建立与终止

    TCP是一个面向连接的协议,任何一方在发送数据之前,都必须先在双方之间建立一条连接.所以,本文就主要看看TCP连接的建立和终止. 在开始介绍TCP连接之前,先来看看TCP数据包的首部,首部里面有很多重 ...

  4. 怎么运用好ZBrush中Magnify膨胀笔刷

    Magnify膨胀笔刷是ZBrush笔刷中经常使用的,利用该笔刷可绘制中心向四周膨胀的效果.本文内容向大家介绍ZBrush®中膨胀笔刷以便大家熟悉它的用法和特性. 查看更多内容请直接前往:http:/ ...

  5. 在Android Studio中使用shareSDK进行社会化分享(图文教程)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  6. JavaWeb学习----JSP简介及入门(含Eclipse for Java EE及Tomcat的配置)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. SQL Server 2005 安装图解教程(Windows)

    因工作需要,好久未安装SQL Server2005,今天安装了一下,特此写下安装步骤留下笔记. 安装前准备: 先安装IIS,再安装SQL Server2005 一.安装 点击安装,如下图: 选择操作系 ...

  8. 较多java书籍的网站 tools138.com

    http://www.tools138.com/front/resource/java_book.jsp

  9. Eclipse 分屏显示同一个文件

    场景 : 某个类很大,可能有数千行.当你想要将类开头部分与中间或者靠后的部分进行对比时,请follow如下步骤: Window -> Editor -> Toggle Split Edit ...

  10. Ubuntu安装JDK与配置环境变量

    Ubuntu14.04安装JDK与配置环境变量 工具/原料   Ubuntu14.04系统 方法/步骤     先从Oracle官网下载JDK.先选择同意按钮,然后根据自己的系统下载相应版本.我的系统 ...