怎樣寫 OpenStack Neutron 的 Extension (四)

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   ar   div   

上文說到需要在 /neutronclient/v2_0/myextension/extension.py 中分別定義五個 class:List/Show/Create/Delete/UpdateExtension。具體形式如下:

import argparseimport loggingfrom neutronclient.neutron import v2_0 as neutronV20from neutronclient.openstack.common.gettextutils import _
RESOURCE = ‘myextension‘
class ListExtension(neutronV20.ListCommand): """List extensions""" resource = RESOURCE log = logging.getLogger(__name__ + ‘.ListExtension‘) list_columns = [‘id‘, ‘name‘] class ShowExtension(neutronV20.ShowCommand): """Show information of a given extension.""" resource = RESOURCE log = logging.getLogger(__name__ + ‘.ShowExtension‘) class CreatePhysicalGateway(neutronV20.CreateCommand): """Create an extension.""" resource = RESOURCE log = logging.getLogger(__name__ + ‘.CreateExtension‘) def add_known_arguments(self, parser): parser.add_argument( ‘name‘, metavar=‘NAME‘, help=_(‘Name of extension to create‘)) def args2body(self, parsed_args): body = {self.resource: { ‘name‘: parsed_args.name}} return body

class UpdateExtension(neutronV20.UpdateCommand):    """update a given extension."""    resource = RESOURCE    log = logging.getLogger(__name__ + ‘.UpdateExtension‘)      
class DeleteExtension(neutronV20.DeleteCommand):    """Delete a given extension."""    resource = RESOURCE    log = logging.getLogger(__name__ + ‘.DeleteExtension‘)       

這些 class 處在接受 CLI 命令的第一線,負責將命令轉化成 API call。需要特別注意的是 CreateExtension 這個類,它有兩個方法 add_known_arguments 和 args2body。前者定義了 CLI 命令接受哪些參數,後者規定如何將收到的參數打包起來。

這些參數打包之後就會發給 neutron 後台中我們自己定義的 plugin controller,但是如何發送這些參數還需要我們去 /neutronclient/v2_0/client.py 的 Client 類中設定:

首先是 uri 路徑:

    myextensions_path = "/myextensions"    myextension_path = "/myextensions/%s"

然後是每個操作所對應的傳遞方法:

   @APIParamsCall    def list_myextensions(self, retrieve_all=True, **_params):        """Fetches a list of all myextensions for a tenant."""        return self.list(‘myextensions‘, self.myextensions_path, retrieve_all,                         **_params)                             @APIParamsCall    def show_myextension(self, myextension, **_params):        """Fetches information of a certain entry in myextension."""        return self.get(self.myextension_path % (myextension), params=_params)            @APIParamsCall    def create_myextension(self, body=None):        """Creates a new myextension entry."""        return self.post(self.myextensions_path, body=body)            @APIParamsCall    def delete_myextension(self, myextension):        """Deletes the specified myextension."""        return self.delete(self.myextension_path % (myextension))   @APIParamsCall    def update_myextension(self, myextension, body=None):        """Updates a myextension."""        return self.put(self.myextension_path % (myextension), body=body)

如此一來,我們自己實現的 neutron plugin 就能夠收到 CLI 發送過來的命令啦。

下一章再來說說怎麼修改 OpenStack 的 Dashboard 來顯示我們在 extension 中添加的新元素。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.