novaclient的api调用流程与开发
2015年07月05日 19:27:17 qiushanjushi 阅读数:3915
http://blog.csdn.net/tpiperatgod/article/details/18375387?utm_source=tuicool
另一个地址:http://neuromancer.sinaapp.com/?p=64
从nova client的入口查看
cat /usr/bin/nova
[python] view
plaincopy
- #!/usr/bin/python
# PBR Generated from ‘console_scripts‘import
fromimport
if:
- sys.exit(main())
其中导入了novaclient.shell这个文件中导入了main方法,进入novaclient.shell.py查看
[python] view
plaincopy
- def
try
:])) - except
) - print"ERROR: %s"
) - .cs = client.Client(options.os_compute_api_version, os_username,
- .extensions, service_type=service_type,
- cacert=cacert, timeout=timeout)
self.cs是从client中创建出的一个Client实例,进入novaclient.client.py查看这个实例的具体方法
[python] view
plaincopy
- def
: ,
- : ,
- : ,
- try
except
.join(version_map.keys())))
- raise
return
def
return client_class(*args, **kwargs)
用的是v1_1这个版本的api,对应的是novaclient.v1_1.client.py里的Client类
[python] view
plaincopy
- class
Top-level object to access the OpenStack Compute API.
- Create an instance with your creds::
- >>> client = Client(USERNAME, PASSWORD, PROJECT_ID, AUTH_URL)
- Then call methods on its managers::
- >>> client.servers.list()
- ...
- >>> client.flavors.list()
- ...
- """
注释里讲了怎么使用python命令行调用nova的client
client里给流入的指令分了很多类,以flavors为例,看nova flavor-list这个命令的流程
[python] view
plaincopy
- self)
- class
Manage :class:`Flavor` resources. - """
- def, detailed=, is_public=):
- Get a list of all flavors.
- :rtype: list of :class:`Flavor`.
- """
- ifnot
] = is_public - % urlutils.urlencode(qparams) ifelse<span " <="" span="">
- <span " <="" span="">
- if
- return._list( % (detail, query_string), )
self._list进入novaclient.base.py
[python] view
plaincopy
- class
Managers interact with a particular type of API (servers, flavors, images,
- etc.) and provide CRUD operations for them.
- """
- def, api):
- .api = api
- def, url, response_key, obj_class=, body=):
- if
.api.client.post(url, body=body) - else
.api.client.get(url) - ifis:
- .resource_class
- if
try
] - except
pass.completion_cache(, obj_class, mode=):
- .completion_cache(, obj_class, mode=):
- return, res, loaded=)
- forinif res]
novaclient.v1_1.flavors.py里FlavorManager的resource_class = Flavor即class Flavor(base.Resource)
所以最后obj_class为Flavor
调用api的过程:
[python] view
plaincopy
- if
.api.client.post(url, body=body) - else
.api.client.get(url)
[python] view
plaincopy
- 通过.api到了nova的api里nova.api.openstack.compute.__init__.py
- ifis or in
.resources[] = flavors.create_resource() - , ,
- .resources[],
- : },
- : })
找到nova.api.openstack.flavors.py
[python] view
plaincopy
- @wsgi
def, req): - ._get_flavors(req)
- return._view_builder.index(req, limited_flavors)
它最后会返回一个存放flavors信息的字典,这些原始数据经过提取和加工,最后在终端被打印出来
nova.api.openstack.compute.views.flavors.py
[python] view
plaincopy
- def, func, request, flavors):
- ] forin
._get_collection_links(request, - ._collection_name,
- )
- if
] = flavors_links - return flavors_dict
添加一个新的client流程:
功能:快速备份虚拟机,三个参数,虚拟机uuid、备份的名字、备份的描述,调用地方和方法如下:
[plain] view
plaincopy
novaclient.shell.py
class OpenStackComputeShell(object) 的get_subcommand_parser 方法里指定了actions_module
转到novaclient.v1_1.shell.py
增加一个新的方法,装饰器里是需要的参数,有顺序,和执行时的参数顺序一致
[python] view
plaincopy
- @utils‘server‘, help=)
- @utils‘displayname‘
, - )
- @utils‘description‘
, - ,
- )
- def
args.description)
这个功能是加在servers部分里的,转到novaclient.v1_1.servers.py
在ServerManager类里添加
[python] view
plaincopy
- def, server, backup_name, backup_description):
- Backup a server instance quickly.
- :param server: The :class:`Server` (or its ID) to share onto.
- :param backup_name: Name of the backup image
- :param backup_description: The backup description
- """
: backup_name, - : backup_description}
- return._create( % base.getid(server),
- )
response_key是指返回数据里的key,这里返回的数据是{‘id‘: "*****"},所以response_key = "id"
因为这个api返回的是一个json字符串,不能通过novaclient.base.py里Manager类里的方法把数据提取出来(它需要字典),于是把return_raw这个参数设置为True
然后就可以在nova的命令行里看到这个新的功能了:nova
backup-instance;使用方法:nova backup-instance <server_id>
<bak_name> <bak_description>
原文地址:https://www.cnblogs.com/zpaixx/p/10662403.html