zoukankan      html  css  js  c++  java
  • 自定义 openstack REST API (newton)

    外部访问 openstack  都是通过 REST API 的方式,比如 获取一个image 的详细信息 /v2/images/{image_id} ,有关 API 的信息参考 api文档 .

    我是基于devstack newton 版本进行编写的,步骤如下

    1.创建一个python文件叫documents.py

    2.documents.py 中有一个类 继承 extensions.ExtensionDescriptor 这个类,并且覆盖name,alias,namespace,update这个四个属性,并且实现get_resources这个方法

    1 class Documents(extensions.ExtensionDescriptor):
    2     name = "documents"
    3     alias = "os-documents"
    4     namespace = "www.doc.com"
    5     update = "2017-8-23T00:00:00+00:00"
    6     
    7     def get_resources(self):
    8         resources = [extensions.ResourceExtension("os-documents",DocumentsController())]
    9         return resources

    3.创建一个实现你请求的类,比如你想获取一个image的详细信息,实现的具体方法在这里实现。大家注意  Documents 类中实现的get_resources(self) 方法,这个方法会引用现在创建的类,从而获取想要 的数据。这里为了方便,不去数据库中查询数据,而用一个集合代替

        3.1声明一个集合,里边存储documents 的数据

          

    documents={"documents":[{"id":"1001","name":"docs1"},
                            {"id":"1002","name":"docs2"},
                            {"id":"1003","name":"docs3"}
                            ]}

        3.2创建一个类,名字叫做DocumentsController

        3.3 在DocumentsController创建一个index方法,这个方法就会自动处理获取所有信息的请求 

     

        def index(self,req):
            '''
            get v2/{tenant_id}/os-documents
            '''
            return  documents

      3.4在DocumentsController创建一个show方法,这个方法就会自动处理获取指定条目请求

        def show(self,req,id):
            '''
             get v2/{tenant_id}/os-documents/{id}
            '''
            document = None
            for docu in documents["documents"]:
                if docu["id"] ==id:
                    document = docu
            if docu == None:
                raise webob.exc.HTTPNotFound(explanation="documents not found")
            else:
                return document

      3.5在DocumentsController创建一个create方法,这个处理创建一个条目的请求

     

        def create(self,req,body):
            '''
            post v2/{tenant_id}/os-documents
            '''
            try:
                documents["documents"].append(body["document"])
            except:
                raise webob.exc.HTTPBadRequest(explanation="document invaild")
            return body["document"]
      

      3.6在DocumentsController创建一个update方法,这个处理更新一个条目的请求

        def update(self,req,body,id):
            '''
            put v2/{tenant_id}/os-documents/{id}
            
            '''
            document = None
            for docu in documents["documents"]:
                if docu["id"] == id:
                    documents["documents"].remove(docu)
                    documents["documents"].append(body["document"])
                    document = body["document"]
            
            if document == None:
                webob.exc.HTTPNotFound(explanation="document not found")
            
            else:
                return document

      3.7 在DocumentsController创建一个delete方法,这个处理删除一个条目的请求

        def delete(self,req,id):
            '''
            delete v2/{tenant_id}/os-documents/{id}
            '''
            document = None
            for docu in documents["documents"]:
                if docu["id"] == id:
                    document = docu
                    documents["documents"].remove(docu)
                    return webob.Response(status_int = 202)
                
            if document == None:
                raise webob.exc.HTTPNotFound(explanation="document not found")
            

    完整代码如下:

    import webob
    from webob  import exc
    from nova import exception
    from nova.api.openstack import extensions
    from docutils.nodes import document
    from _ast import alias
    from argparse import Namespace
    
    documents={"documents":[{"id":"1001","name":"docs1"},
                            {"id":"1002","name":"docs2"},
                            {"id":"1003","name":"docs3"}
                            ]}
    
    class DocumentsController():
        def index(self,req):
            '''
            get v2/{tenant_id}/os-documents
            '''
            return  documents
        
        def show(self,req,id):
            '''
             get v2/{tenant_id}/os-documents/{id}
            '''
            document = None
            for docu in documents["documents"]:
                if docu["id"] ==id:
                    document = docu
            if docu == None:
                raise webob.exc.HTTPNotFound(explanation="documents not found")
            else:
                return document
            
            
        def create(self,req,body):
            '''
            post v2/{tenant_id}/os-documents
            '''
            try:
                documents["documents"].append(body["document"])
            except:
                raise webob.exc.HTTPBadRequest(explanation="document invaild")
            return body["document"]
        
        def update(self,req,body,id):
            '''
            put v2/{tenant_id}/os-documents/{id}
            
            '''
            document = None
            for docu in documents["documents"]:
                if docu["id"] == id:
                    documents["documents"].remove(docu)
                    documents["documents"].append(body["document"])
                    document = body["document"]
            
            if document == None:
                webob.exc.HTTPNotFound(explanation="document not found")
            
            else:
                return document
            
        def delete(self,req,id):
            '''
            delete v2/{tenant_id}/os-documents/{id}
            '''
            document = None
            for docu in documents["documents"]:
                if docu["id"] == id:
                    document = docu
                    documents["documents"].remove(docu)
                    return webob.Response(status_int = 202)
                
            if document == None:
                raise webob.exc.HTTPNotFound(explanation="document not found")
            
            
            
    
    class Documents(extensions.ExtensionDescriptor):
        name = "documents"
        alias = "os-documents"
        namespace = "www.doc.com"
        update = "2017-8-23T00:00:00+00:00"
        
        def get_resources(self):
            resources = [extensions.ResourceExtension("os-documents",DocumentsController())]
            return resources

    将这个文件放到 nova/api/openstack/compute      目录下,重启nova 尝试方法一下 v2/{tenant_id}/os-documents

    参考文档  WritingRequestExtensions  

  • 相关阅读:
    起名
    用超级巡警批量清除被挂马的网页
    我在csdn回复的帖子
    多线程与socket编程
    NET面试汇总
    第5 章: Windows Message Mapping 20080111 09:04 207人阅读 评论(0) 收藏
    oo的开始 分类: VC++ 20071228 14:31 176人阅读 评论(0) 收藏
    Cstring 截获数据 20080110 11:22 234人阅读 评论(0) 收藏
    截取字符串 20080110 08:18 188人阅读 评论(0) 收藏
    CString DOWRD互转 20080110 08:26 378人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/gaizhongfeng/p/gaizhongfeng.html
Copyright © 2011-2022 走看看