How to Write extension of openstack neutron (1)

Source: Internet
Author: User

The first two articles discussed how to write a neutron plug-in. However, the most basic plug-ins only include network, port, and subnet resources. If you need to introduce new resources, such as a layer-2 gateway, you need to write another extension, that is, extension, based on the plug-in.

Neutron has predefined many extensions. For more information, see the files under neutron/extensions. I will not list them here. If there is one that you need, you just need to use it. If you need to start from scratch, you can create an extensions folder under your plug-in folder, and then create two files under this folder: _ init _. py and myextension. py:

-Neutron/

-Plugins/

-Myplugin/

-_ Init _. py

-Plugin. py

-Extensions/

-_ Init _. py

-Myextension. py

 

_ Init _. py is empty. In myextension. py, You need to define two things: a dictionary named resource_attribute_map and a class named myextension. Resource_attribute_map contains your new extended attributes, for example:

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}        }    }

 

Note that in the dictionary, the first key 'myextension' is the file name 'myextension' plus an's '. The second layer's keys 'id', 'name', and 'tenant _ id' are the three attributes of this extension. The third-level keys have detailed explanations in neutron/API/v2/attributes. py. I moved it here:

# 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.

 

When defining a new class, note that the only difference between the class name and the file name containing the class must be an uppercase letter and a lowercase letter. That is to say, using myextension as the class name may cause errors. For details, refer to the implementation of the _ load_all_extensions_from_path method of extensionmanager in neutron/API/extensions. py. The myextension class can inherit a class in the neutron/API/extensions. py file: extensiondescriptor, or you can define it yourself. The following is a definition that inherits the class:

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

 

So far, this myextension. py file is almost accomplished. Next, you need to configure the/etc/neutron/Neutron. conf file to tell neutron where to find the extension. Under the [Default] of the file, we can find an option named api_extensions_path and assign the location of the created extensions folder to it. For example:

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

 

Then, add a sentence in the definition of your myplugin class to tell neutron that my plug-in supports this extension. Note that the name in square brackets must be the same as the name obtained by the get_alias () method above.

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

 

Restart the neutron server and "service neutron-server restart". If/var/log/neutron/server is displayed. if the log contains the words loaded Extension: My-extensions, the operation is successful.

In the following articles, I will continue to discuss how to implement different operations for an extension and how to add command support for custom extension to CLI.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.