neutron plugin 开发

作者 Billy 日期 2017-09-06
neutron plugin 开发

neutron plugin 开发

根据ml2 plugin继承关系,

基础接口:neutron_plugin_base_v2.NeutronPluginBaseV2

数据库接口:neutron.db.db_base_plugin_v2.NeutronDbPluginV2

首先,Neutron对最基本的三个资源:Network, Port 和 Subnet 的基本调用都已经定义好了API接口。如果你的插件也需要用到这些资源,最好直接实现它们的接口。API接口的定义可以再 neutron/neutron_plugin_base_v2.py 这个文件中找到,其中每个参数的作用也有比较详细的介绍。对于用不着的资源,直接放任不管就好了,最多下次不小心调用了会发出“该接口没有被实现”的错误,不会有其他影响。这里是一个 Network API 实现的范例,其实它什么也没有做,但是确实是一个有效的接口实现:

from neutron import neutron_plugin_base_v2
class MyPlugin(neutron_plugin_base_v2.NeutronPluginBaseV2):
    def __init__(self):
        pass

    def create_network(self, context, network):
        # Create a network by using data from network dictionary
        # Send back a dictionary to display created network's info
        return network

    def update_network(self, context, id, network):
        # Update a created network matched by id with
        # data in the network dictionary. Send back a
        # dictionary to display the network's updated info
        return network

    def get_network(self, context, id, fields=None):
        network = {}
        # List information of a specific network matched by id
        # and return it in a form of dictionary
        return network

    def get_networks(self, context, filters=None, fields=None):
        network = {}
        # List all networks that are active
        return network

    def delete_network(self, context, id):
        # Delete a specific network matched by id
        # return back the id of the network.
        return id

如果在具体实现这些接口的过程中,需要参考的话,有两个地方非常值得参考:一个是 neutron/db/db_base_plugin_v2.py,这个是neutron官方给出的一个基于数据库的实现。它只是操作数据库中的内容,模拟各个资源的创建、修改、删除等操作,但没有在物理机器上做任何改变。第二个地方就是 neutron/plugins 里面收纳的各个公司的插件实现,我们可以从中学习到其他公司是怎么写插件的。