外部访问 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