Overview:
All of the requests coming from neutron-client of dashboard will be tranfered to neutron-server using REST API. Those requests are passed to a URL defined in neutron-server. Neutron-server will read that URL, revoke the correspoding methods in plugin then map the corresponding actions like (show, list, delete, etc.) based on methods (POST, GET, DELTE, etc.) defined in URL.
If you have your own plugin and you want neutron-server read URL (e.g. /myextension) with your own plugin, all that you have to do is writing the new plugin inside the plugins folder of neutron. Neutron allows the third party vendors to customize their own methods specific to their plugins with the help of API extensions.
This example will introduce to you the basic steps about writing an API extension.
- In the folder plugins, i create a "myplugin" tree as below:
- In myextension.py, we need to define two things: a RESOURCE_ATTRIBUTE_MAP dictionary and a class called Myextension called. RESOURCE_ATTRIBUTE_MAP what we put inside this new extended attributes, myextension.py seems like below:
- After create the myextension file, we need to tell Neutron the path of extension file. In /etc/neutron/neutron.conf, inside the section DEFAULT:
api_extensions_path = /usr/lib/python2.7/dist-packages/neutron/plugins/myplugin/extensions
- Create database for extensions
models.py: Defines how those properties in database
import sqlalchemy as sa
from neutron.db import model_base
class MyExtension(model_base.BASEV2):
'''
A simple table to store the myextension properties
'''
__tablename__ = 'myextension'
name = sa.Column(sa.String(255),
primary_key=True, nullable=False)
id = sa.Column(sa.Integer)
db.py: Provides a number of methods to read, write properties of database such as READ, WRITE, UPDATE, GET, etc. Each method will be executed through the database session. The database session will be provided by neutron api request context.
For example:
def add_something(db_session, arg1*, arg2*):
with db_session.begin (subtransactions=True):
do_something
...
A simple sample of plugin.py can be written like below:
from neutron.db import db_base_plugin_v2
class MyPlugin(db_base_plugin_v2.NeutronDbPluginV2):
# We need to define a data member to tell Neutron Server about our plugin.
# Name of alias should be the same with get_alias() in myextension.py
_supported_extension_aliases = ['Test-Ex']
def __init__(self):
pass
def create_myextension(self, context, myextension):
return myextension
def update_myextension(self, context, id, myextension):
return myextension
def get_myextension(self, context, id):
myextension = {}
return myextension
def delete_myextension(self, context, id):
return id
Restart neutron server by running “service neutron-server restart”, check “/var/log/neutron/server.log” we will see the “Test-Ex” loaded “Loaded extension: Test-Ex”.
Reference: http://control-that-vm.blogspot.hu/2014/05/writing-api-extensions-in-neutron.html
22/08/2015
VietStack Team
Comments