novaclient的api調用流程與開發
http://blog.csdn.net/tpiperatgod/article/details/18375387?utm_source=tuicool
html
另外一個地址:http://neuromancer.sinaapp.com/?p=64
python
從nova client的入口查看shell
cat /usr/bin/nova
- #!/usr/bin/python # PBR Generated from 'console_scripts' import fromimport if:
- sys.exit(main())
其中導入了novaclient.shell這個文件中導入了main方法,進入novaclient.shell.py查看
- 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查看這個實例的具體方法
- def : ,
- : ,
- : ,
- try except .join(version_map.keys())))
- raise return def return client_class(*args, **kwargs)
用的是v1_1這個版本的api,對應的是novaclient.v1_1.client.py裏的Client類
- 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這個命令的流程
- 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
- 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的過程:
- if .api.client.post(url, body=body)
- else .api.client.get(url)
- 經過.api到了nova的api裏nova.api.openstack.compute.__init__.py
- ifis or in .resources[] = flavors.create_resource()
- , ,
- .resources[],
- : },
- : })
找到nova.api.openstack.flavors.py
- @wsgi def, req):
- ._get_flavors(req)
- return._view_builder.index(req, limited_flavors)
它最後會返回一個存放flavors信息的字典,這些原始數據通過提取和加工,最後在終端被打印出來
nova.api.openstack.compute.views.flavors.py
- def, func, request, flavors):
- ] forin ._get_collection_links(request,
- ._collection_name,
- )
- if ] = flavors_links
- return flavors_dict
添加一個新的client流程:
功能:快速備份虛擬機,三個參數,虛擬機uuid、備份的名字、備份的描述,調用地方和方法以下:
novaclient.shell.py
class OpenStackComputeShell(object) 的get_subcommand_parser 方法裏指定了actions_module
轉到novaclient.v1_1.shell.py
增長一個新的方法,裝飾器裏是須要的參數,有順序,和執行時的參數順序一致
- @utils'server', help=)
- @utils'displayname' ,
- )
- @utils'description' ,
- ,
- )
- def args.description)
這個功能是加在servers部分裏的,轉到novaclient.v1_1.servers.py
在ServerManager類裏添加
- 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),
- )