how to read openstack code: request extension
We have learned resource extension and action extension. This post we will write a request extension
First see two API call
curl -X POST http://liberty-controller01:9696/v2.0/networks.json -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net3", "admin_state_up": true}}'
{"network": {"status": "ACTIVE", "subnets": [], "name": "net3", "provider:physical_network": "physnet1", "admin_state_up": true, "tenant_id": "8c5f13ee6a404759839e48537bdf69ac", "mtu": 0, "router:external": false, "shared": false, "port_security_enabled": true, "provider:network_type": "vlan", "id": "95c955ac-c963-4f53-ab4a-d721fa0cda51", "provider:segmentation_id": 490}}
It run successful. Nothing to say. Then this one
curl -X POST http://liberty-controller01:9696/v2.0/networks.json -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net3", "admin_state_up": true, "some_attr":"some_value"}}'
{"NeutronError": {"message": "Unrecognized attribute(s) 'some_attr'", "type": "HTTPBadRequest", "detail": ""}}[root@liberty-controller01 myPluginPKG]#
This one is bad request. But the only difference is the POST body. Neutron think the some_attr is not a attribute of network and it is right. Because this is a core resource and the attribute map of this resource do not have some_attr
To solve this we need an request extension which actually update the resource attribute map of network. Below are the code
from neutron.api import extensions
EXTENDED_ATTRIBUTES_2_0 = {
'networks': {
'some_attr': {'allow_post': True,
'allow_put': False,
'is_visible': True,
'default': ''}
}
}
class Myreq(extensions.ExtensionDescriptor):
@classmethod
def get_name(cls):
return "myreq"
@classmethod
def get_alias(cls):
return 'myreq'
@classmethod
def get_description(cls):
return "myreq"
@classmethod
def get_updated(cls):
return "2017-02-08T10:00:00-00:00"
def get_extended_resources(self, *args, **kwargs):
return EXTENDED_ATTRIBUTES_2_0
You can see we defined a dict in the extension and return it with method get_extended_resources.
So to implement an request extension is very easy. Define a method called get_extended_resources and return some attributes that you want to added to the original reosurce
最新文章
- Data对象
- C#各种文件操作的代码与注释
- 计算机视觉入门 Intorduction To Computer Vision
- CPU与外设传送数据方式
- BZOJ 1006 神奇的国度
- 彻底解决TAP(点透)提升移动端点击响应速度
- php 极简框架ES发布(代码总和不到 400 行)
- Linux netfilter 学习笔记
- Docker技术应用场景(转载)
- 使用bat脚本部署hexo到coding和github
- 集腋成裘-06-angularJS -angular_02
- Jmeter(二十八)_Docker+Jmeter+Gitlab+Jenkins+Ant(容器化的接口自动化持续集成平台)
- Android - 序列化与反序列化
- 【Jmeter】Jmeter 5.0新特性
- [Java学习] 再谈Java包
- fast协议解读
- c语言中static、extern、void的重载
- python-redis集合模式
- #运算符、不同的指针类型、数组和指针、指针运算、堆、栈、静态区、只读区、下标VS指针
- Linux远程管理器xshell和xftp使用教程,以及遇到关闭Xshell后项目也停止的解决方法