how to read openstack code: service plugin
We have learned core plugin, service plugin and extension in last post. Now let`s review:
Core Plugin
Core plugin manage core resources which are network, subnet, port and subnetpool.
Service Plugin
Service plugin manage higher services.
extension
Extensions are called API Extensions. There are three types of extension
- resource extension which define new resources
- action extension which define actions for resource
- request extension which can add more parameter to request
Normally a new feature will be implemented by extension first. When the feature is stable, the community will move it to official api and may implement it in plugin.
We write our core plugin in the previous post now we are going to write our service plugin.
What is the difference between core plugin and service plugin
Core plugin manage core resource in neutron. The code structure is different from service plugin. But the community are considering transfer core plugin into one kind of service plugin. You will see the trend in code
Design our service plugin
Our service plugin is called "ZOO". This plugin will manage some resource like "tiger". We are going to do API call like CREATE/UPDATE/DELETE/GET tiger with this service plugin.
Write our service plugin
Service plugin must be inherited from the class neutron.services.service_base.ServicePluginBase
Below are my service plugin
from neutron.services import service_base
class ZooPlugin(service_base.ServicePluginBase):
supported_extension_aliases = ["zoo"]
def __init__(self):
super(ZooPlugin, self).__init__()
def get_plugin_name(self):
return "ZOO"
def get_plugin_type(self):
# should be under neutron/plugins/common/constants.py
return "ZOO"
def get_plugin_description(self):
return ("ZOO")
def create_tiger(self, context, tiger):
return "tiger created"
def get_tigers(self, context, filters, fields):
return {}
supported_extension_aliases is necessary since we need an extension to generate the resource.
We only support get_tigers and create_tiger here for simplicity purpose.
Because the plugin is third-party code, so we have to register it under certain entry point so neutron can load it. So our code structure will be like :
[root@liberty-controller01 tmp]# tree zooServicePlugin
zooServicePlugin/
├── setup.py
└── zoo
├── __init__.py
├── zoo_plugin.py
The content of setup.py is like
from setuptools import setup, find_packages
setup(
name='zoo',
version='1.0',
packages=find_packages(),
entry_points={
'neutron.service_plugins': [
'zoo = zoo.zoo_plugin:ZooPlugin',
],
},
)
The key point here is to register the plugin under neutron.service_plugins namespace.
Write the extension
We have service plugin ready to manage the tiger resource. But we do not have the tiger resource yet. One option is to modify the neutron/api/v2/attribute.py which is not suggested. The recommended way is to generate the resource by extension like below
from neutron.api import extensions
from neutron.api.v2 import base
from neutron import manager
EXT_PREFIX = '/zoo'
RESOURCE_NAME = 'tiger'
COLLECTION_NAME = '%ss' % RESOURCE_NAME
RESOURCE_ATTRIBUTE_MAP = {
'tiger': {
'id': {'allow_post': False, 'allow_put': False,
'validate': {'type:uuid': None},
'is_visible': True,
'primary_key': True},
'name': {'allow_post': True,
'allow_put': False,
'is_visible': True,
'default': ''},
'tenant_id': {'allow_post': True, 'allow_put': False,
'required_by_policy': True,
'validate': {'type:string': None},
'is_visible': True}
}
}
class Zoo(extensions.ExtensionDescriptor):
@classmethod
def get_name(cls):
return "zoo"
@classmethod
def get_alias(cls):
return 'zoo'
@classmethod
def get_description(cls):
return "zoo"
@classmethod
def get_updated(cls):
return "2017-02-08T10:00:00-00:00"
@classmethod
def get_resources(cls):
# This method registers the URL and the dictionary of
# attributes on the neutron-server.
exts = list()
plugin = manager.NeutronManager.get_service_plugins()['ZOO']
resource_name = RESOURCE_NAME
collection_name = COLLECTION_NAME
params = RESOURCE_ATTRIBUTE_MAP.get(resource_name)
controller = base.create_resource(collection_name, resource_name,
plugin, params, allow_bulk=False)
ex = extensions.ResourceExtension(collection_name, controller, path_prefix=EXT_PREFIX)
exts.append(ex)
return exts
The RESOURCE_ATTRIBUTE_MAP is used for define resource tiger. The tenant_id attribute is necessary for auth.
An extension must inherited from extensions.ExtensionDescriptor
get_alias method is really important because plugin will use this value to find the extension. This value must be in the supported_extension_alias of plugin
The get_resources method is necessary for an extension who define new resource. We will see the detail in later post.
Config
Now we have our service plugin and extension we need to install our service plugin by python setup.py install and put the extension under neutron/extensions
Also config the /etc/neutron/neutron.conf
service_plugins = ...,zoo
Restart your neutron server and run below API
curl -g -i "http://liberty-controller01:9696/v2.0/zoo/tigers" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token:$token"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 14
X-Openstack-Request-Id: req-82ed8ccc-da9d-46d9-8fd9-beb01a24385b
Date: Wed, 08 Feb 2017 11:09:13 GMT
{"tigers": []}
It work
how to read openstack code: service plugin的更多相关文章
- how to read openstack code: Core plugin and resource extension
本章我们将写一个自己的core plugin 和一个resource extension来加深理解.(阅读本文的前提是你已经理解了restful以及stevedore等内容) 什么是 core plu ...
- Service Plugin / Agent - 每天5分钟玩转 OpenStack(73)
Core Plugin/Agent 负责管理核心实体:net, subnet 和 port.而对于更高级的网络服务,则由 Service Plugin/Agent 管理.Service Plugin ...
- jshint-eclipse: JavaScript Code Quality Plugin for Eclipse
https://blog.oio.de/2012/03/26/jshint-eclipse-javascript-code-quality-plugin-for-eclipse/ techscou ...
- how to read openstack code: loading process
之前我们了解了neutron的结构,plugin 和 extension等信息.这一章我们看一下neutron如何加载这些plugin和extension.也就是neutron的启动过程.本文涉及的代 ...
- how to read openstack code: action extension
之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...
- how to read openstack code: Neutron architecture
今天这一章节非常重要.我们知道neutron是一个非常复杂的系统,由很多组件构成.研究这样一个复杂的系统,正确的顺序应该是现在宏观上对其整体结构有所了解,然后再由针对性的对其组件进行深入了解.本章要做 ...
- how to read openstack code
本文的目的不是介绍openstack.我们这里假设你已经知道了openstack是什么,能够做什么.所以目的是介绍如何阅读openstack的代码.通过读代码来进一步学习openstack. 转载要求 ...
- OpenStack (1) - Keystone OpenStack Identity Service
echo deb http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main >> /etc ...
- eclipse启动报错 Problems occurred when invoking code from plug-in: "org.eclipse.jface"
eclipse在使用中可能会发生错误: Problems occurred when invoking code from plug-in: "org.eclipse.jface" ...
随机推荐
- 11G RAC环境数据库启动和关闭
一步启动Oracle (1) 启动整个集群 # ./crsctl start cluster -all -all选项启动整个集群. 不加-all选项只能启动本节点的服务. (2) 启动本节点集群 以下 ...
- 世平信息(T 面试)
1.跟我说下你们这个民主测评项目 在递归这一块扯了很久 2.遍历树结构,除了递归,还有什么方法? 3.如果数据库里面有2万条数据,你需要在前台用列表展示出来,有搜索功能.分页功能.总数:你觉得最需要优 ...
- mysql的sql语句练习的2个网址
sql语句练习: https://blog.csdn.net/mrbcy/article/details/68965271 完成. https://blog.csdn.net/flycat296/ar ...
- 北京化工大学2018年10月程序设计竞赛部分题解(A,C,E,H)
目录 北京化工大学2018年10月程序设计竞赛部分题解(A,C,E,H) 竞赛事件相关 竞赛链接 竞赛题目 总结 北京化工大学2018年10月程序设计竞赛部分题解(A,C,E,H) 竞赛事件相关 竞赛 ...
- layui使用小记(持续更新)
关于Select等Form表单元素,在使用的时候部分特性会失效 如select自带的Search功能: 其实在使用Form表单元素的时候,你如果需要layui自带的一些功能(搜索,验证等),请用< ...
- 360 Atlas中间件安装及使用
1.下载Atlas wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm ...
- wap学习记录
针对移动端浏览器: 大部分的浏览器内核都是webkit内核,对h5和c3的支持非常好 库 国内比较流行的框架 : 妹子UI bootstrap中解决ie8以及以下的h5标签和媒体查询兼容问题的两个库分 ...
- 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they're used)
其实 无论学何种语言 ,还是觉得要养成先声明后使用,先初始化再使用. 1.永远在使用对象之前先将其初始化. 内置类型: 必须手工完成. 内置类型以外的:使用构造函数完成.确保每一个构造函数都将对象的一 ...
- 转载 vue的基础使用
转载https://www.cnblogs.com/majj/p/9957597.html#top vue的介绍 前端框架和库的区别 nodejs的简单使用 vue的起步 指令系统 组件的使用 过滤器 ...
- Uva 12325 Zombie's Treasure Chest (贪心,分类讨论)
题意: 你有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号的整数.你的任务是最多能装多少价值的宝物? 分析: 分类枚举, ...