使用curl调试openstack的api
一 系统环境
OpenStack: Mitaka
工具: 最简单的工具:restclient,本次使用curl
二 开搞
访问openstack的API之前,用户使用用户名和密码向keystone进行认证。在通过keystone认证后,keystone会在返回数据中包含一个ServiceCatalog。此ServiceCatalog中包含keystone中的所有service endpoints。用户即可根据这些endpoint来进行后续的RestAPI调用。
本文主要示例如何调用V3版本API。对于V2版本,使用openstack命令加--debug参数,可以看到keystone调用curl的具体写法:
如果不知道该怎么取调用可以使用--debug查看
[root@controller01 ~]# openstack --debug role list START with options: ['--debug', 'role', 'list'] options: Namespace(access_token_endpoint='', auth_type='', auth_url='http://controller:35357/v3', cacert='', client_id='', client_secret='***', cloud='', debug=True, default_domain='default', deferred_help=False, domain_id='', domain_name='', endpoint='', identity_provider='', identity_provider_url='', insecure=None, interface='', log_file=None, os_compute_api_version='', os_data_processing_api_version='1.1', os_data_processing_url='', os_dns_api_version='2', os_identity_api_version='3', os_image_api_version='2', os_network_api_version='', os_object_api_version='', os_orchestration_api_version='1', os_project_id=None, os_project_name=None, os_volume_api_version='', password='***', profile=None, project_domain_id='', project_domain_name='default', project_id='', project_name='admin', protocol='', region_name='', scope='', service_provider_endpoint='', timing=False, token='***', trust_id='', url='', user_domain_id='', user_domain_name='default', user_id='', username='admin', verbose_level=3, verify=None) defaults: {u'auth_type': 'password', u'compute_api_version': u'2', 'key': None, u'database_api_version': u'1.0', 'api_timeout': None, u'baremetal_api_version': u'1', u'image_api_version': u'2', 'cacert': None, u'image_api_use_tasks': False, u'floating_ip_source': u'neutron', u'orchestration_api_version': u'1', u'interface': None, u'network_api_version': u'2', u'image_format': u'qcow2', u'key_manager_api_version': u'v1', u'metering_api_version': u'2', 'verify': True, u'identity_api_version': u'2.0', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', u'container_api_version': u'1', u'dns_api_version': u'2', u'object_store_api_version': u'1', u'disable_vendor_agent': {}} cloud cfg: {'auth_type': 'password', u'compute_api_version': u'2', 'key': None, u'database_api_version': u'1.0', 'data_processing_api_version': '1.1', u'network_api_version': u'2', u'image_format': u'qcow2', u'image_api_version': '2', 'verify': True, u'dns_api_version': '2', u'object_store_api_version': u'1', 'verbose_level': 3, 'region_name': '', 'api_timeout': None, u'baremetal_api_version': u'1', 'auth': {'username': 'admin', 'project_name': 'admin', 'user_domain_name': 'default', 'auth_url': 'http://controller:35357/v3', 'password': '***', 'project_domain_name': 'default'}, 'default_domain': 'default', u'container_api_version': u'1', u'image_api_use_tasks': False, u'floating_ip_source': u'neutron', u'orchestration_api_version': '1', 'timing': False, 'cacert': None, u'key_manager_api_version': u'v1', u'metering_api_version': u'2', 'deferred_help': False, u'identity_api_version': '3', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', 'debug': True, u'interface': None, u'disable_vendor_agent': {}} compute API version 2, cmd group openstack.compute.v2 network API version 2, cmd group openstack.network.v2 image API version 2, cmd group openstack.image.v2 volume API version 2, cmd group openstack.volume.v2 identity API version 3, cmd group openstack.identity.v3 object_store API version 1, cmd group openstack.object_store.v1 data_processing API version 1.1, cmd group openstack.data_processing.v1 orchestration API version 1, cmd group openstack.orchestration.v1 dns API version 2, cmd group openstack.dns.v2 command: role list -> openstackclient.identity.v3.role.ListRole Auth plugin password selected auth_type: password Using auth plugin: password Using parameters {'username': 'admin', 'project_name': 'admin', 'auth_url': 'http://controller:35357/v3', 'user_domain_name': 'default', 'password': '***', 'project_domain_name': 'default'} Get auth_ref REQ: curl -g -i -X GET http://controller:35357/v3 -H "Accept: application/json" -H "User-Agent: python-openstackclient keystoneauth1/2.4.1 python-requests/2.10.0 CPython/2.7.5" Starting new HTTP connection (1): controller "GET /v3 HTTP/1.1" 200 250 RESP: [200] Date: Tue, 20 Mar 2018 03:06:46 GMT Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 Vary: X-Auth-Token x-openstack-request-id: req-7730d910-9c4f-4ba3-956f-1dbdb8fc63fb Content-Length: 250 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/json RESP BODY: {"version": {"status": "stable", "updated": "2016-04-04T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v3.6", "links": [{"href": "http://controller:35357/v3/", "rel": "self"}]}} Making authentication request to http://controller:35357/v3/auth/tokens "POST /v3/auth/tokens HTTP/1.1" 201 5362 run(Namespace(columns=[], domain=None, formatter='table', group=None, group_domain=None, inherited=False, max_width=0, noindent=False, project=None, project_domain=None, quote_mode='nonnumeric', user=None, user_domain=None)) Instantiating identity client: <class 'keystoneclient.v3.client.Client'> Making authentication request to http://controller:35357/v3/auth/tokens "POST /v3/auth/tokens HTTP/1.1" 201 5362 REQ: curl -g -i -X GET http://controller:35357/v3/roles -H "User-Agent: python-keystoneclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}84424885abb0746761572e750c8c8add1685ac37" "GET /v3/roles HTTP/1.1" 200 790 RESP: [200] Date: Tue, 20 Mar 2018 03:06:47 GMT Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 Vary: X-Auth-Token x-openstack-request-id: req-bac27bef-ee3f-415b-b448-84a4f7ba73cc Content-Length: 790 Keep-Alive: timeout=5, max=97 Connection: Keep-Alive Content-Type: application/json RESP BODY: {"links": {"self": "http://controller:35357/v3/roles", "previous": null, "next": null}, "roles": [{"domain_id": null, "id": "0bf276f24ffe4aa9a4c217879a43216c", "links": {"self": "http://controller:35357/v3/roles/0bf276f24ffe4aa9a4c217879a43216c"}, "name": "heat_stack_user"}, {"domain_id": null, "id": "129b07bb445d42ae81fad8aa36d77d1f", "links": {"self": "http://controller:35357/v3/roles/129b07bb445d42ae81fad8aa36d77d1f"}, "name": "admin"}, {"domain_id": null, "id": "71d924953a3141c4955839020dd8aba8", "links": {"self": "http://controller:35357/v3/roles/71d924953a3141c4955839020dd8aba8"}, "name": "heat_stack_owner"}, {"domain_id": null, "id": "c797f238b7fc4f4a990879619afe758b", "links": {"self": "http://controller:35357/v3/roles/c797f238b7fc4f4a990879619afe758b"}, "name": "user"}]} +----------------------------------+------------------+ | ID | Name | +----------------------------------+------------------+ | 0bf276f24ffe4aa9a4c217879a43216c | heat_stack_user | | 129b07bb445d42ae81fad8aa36d77d1f | admin | | 71d924953a3141c4955839020dd8aba8 | heat_stack_owner | | c797f238b7fc4f4a990879619afe758b | user | +----------------------------------+------------------+ clean_up ListRole: END return value: 0
1 获取token和catalog
[root@controller01 ~]# curl -i -X POST http://controller:35357/v3/auth/tokens -H "Content-type: application/json" -d '{"auth": {"identity": {"methods": ["password"],"password": {"user": {"domain": {"name": "Default"},"name": "admin","password": "trendy123"}}},"scope": {"project": {"domain": {"name": "Default"},"name": "admin"}}}}' HTTP/1.1 201 Created Date: Tue, 20 Mar 2018 03:18:20 GMT Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 X-Subject-Token: 30494bb13fa746a8a8752acd280f06d0 Vary: X-Auth-Token x-openstack-request-id: req-88e73a96-32af-49fb-92bb-d05e595fd284 Content-Length: 5362 Content-Type: application/json {"token": {"methods": ["password"], "roles": [{"id": "129b07bb445d42ae81fad8aa36d77d1f", "name": "admin"}], "expires_at": "2018-03-20T04:18:20.962364Z", "project": {"domain": {"id": "bb1b0223fdc84934af86523729e4502a", "name": "default"}, "id": "4a3b38b999da4c37bd049a2d5c37920f", "name": "admin"}, "catalog": [{"endpoints": [{"region_id": "RegionOne", "url": "http://controller:9696", "region": "RegionOne", "interface": "internal", "id": "221ef83c7064464e9c4059fdb120a524"}, {"region_id": "RegionOne", "url": "http://controller:9696", "region": "RegionOne", "interface": "public", "id": "7558ce17648e42309a713a296883b7b5"}, {"region_id": "RegionOne", "url": "http://controller:9696", "region": "RegionOne", "interface": "admin", "id": "8dc2142351d24c88ac7cc34ae1d1298f"}], "type": "network", "id": "1e8988a8279144ee94d00d6fd050af84", "name": "neutron"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:9292", "region": "RegionOne", "interface": "public", "id": "433b2acf9a1440879f2d7608563fb950"}, {"region_id": "RegionOne", "url": "http://controller:9292", "region": "RegionOne", "interface": "admin", "id": "64aec5873b2d4b93909d86846fddd198"}, {"region_id": "RegionOne", "url": "http://controller:9292", "region": "RegionOne", "interface": "internal", "id": "8bddd1fba6144709b7864c007e430acc"}], "type": "image", "id": "3ee10bb8c3cc4dfcba0094b0f2609547", "name": "glance"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8776/v1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "admin", "id": "7b2bb710d2ff40a38bef331835787855"}, {"region_id": "RegionOne", "url": "http://controller:8776/v1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "public", "id": "808e9ec2276c43279b9318efc983c1a6"}, {"region_id": "RegionOne", "url": "http://controller:8776/v1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "internal", "id": "ea239eb9e1934183aea8de7e2fa4b50d"}], "type": "volume", "id": "45b6f11db6104e0284af07e013866a32", "name": "cinder"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8776/v2/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "public", "id": "3730e124b62b4c2290728a5f3d7c3172"}, {"region_id": "RegionOne", "url": "http://controller:8776/v2/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "admin", "id": "b2aab32788e5435d930193332bf68d42"}, {"region_id": "RegionOne", "url": "http://controller:8776/v2/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "internal", "id": "c0395d0d8bd34dbcb1f587b13cc68b1b"}], "type": "volumev2", "id": "478e853e09b5421ab141fbd98c0346ed", "name": "cinderv2"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8774/v2.1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "public", "id": "2942c4fd810144c1be14028dffa83c91"}, {"region_id": "RegionOne", "url": "http://controller:8774/v2.1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "admin", "id": "336396d3d5954410b5940e8f1bcb48e7"}, {"region_id": "RegionOne", "url": "http://controller:8774/v2.1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "internal", "id": "c3ace97f947b45238ba29ed831a21dd3"}], "type": "compute", "id": "6b598898644647fca69840ea2963aefc", "name": "nova"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:5000/v3", "region": "RegionOne", "interface": "public", "id": "336fc0683cf44d5c85a1e56efcce0aaa"}, {"region_id": "RegionOne", "url": "http://controller:35357/v3", "region": "RegionOne", "interface": "admin", "id": "ac31a17c6abd46a4bfafb9ccf0f7c38e"}, {"region_id": "RegionOne", "url": "http://controller:5000/v3", "region": "RegionOne", "interface": "internal", "id": "fdd2e827f6ef45538c1aa360df913280"}], "type": "identity", "id": "78e660c901c545c7b065bc35e8351d0b", "name": "keystone"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8004/v1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "admin", "id": "2e7259d915ba4a51ae4ca3327bcfe2ef"}, {"region_id": "RegionOne", "url": "http://controller:8004/v1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "internal", "id": "4f1dbaa50be94b29ad492ba6b632ca0c"}, {"region_id": "RegionOne", "url": "http://controller:8004/v1/4a3b38b999da4c37bd049a2d5c37920f", "region": "RegionOne", "interface": "public", "id": "f17603f93de24e1aacb6d74759ecb700"}], "type": "orchestration", "id": "bce75791740843c09e4c9ef09be5a668", "name": "heat"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8000/v1", "region": "RegionOne", "interface": "public", "id": "411f9576ac874cbf818808342546823a"}, {"region_id": "RegionOne", "url": "http://controller:8000/v1", "region": "RegionOne", "interface": "admin", "id": "5116b8f7a49d46cb8024d297e352e98f"}, {"region_id": "RegionOne", "url": "http://controller:8000/v1", "region": "RegionOne", "interface": "internal", "id": "63d8906e594f45a29fb2f2a833bc2e17"}], "type": "cloudformation", "id": "f5539459039f4fc090d091b37d17c187", "name": "heat-cfn"}], "user": {"domain": {"id": "bb1b0223fdc84934af86523729e4502a", "name": "default"}, "id": "69d239d3f58147ef8ff679bdb352c19d", "name": "admin"}, "audit_ids": ["1U5BTGV4SKi7x4S-cUkrFA"], "issued_at": "2018-03-20T03:18:20.962421Z"}}[root@controller01 ~]# clear [root@controller01 ~]# [root@controller01 ~]# [root@controller01 ~]# [root@controller01 ~]# curl -i -X POST http://controller:35357/v3/auth/tokens -H "Content-type: application/json" -d '{"auth": {"identity": {"methods": ["password"],"password": {"user": {"domain": {"name": "Default"},"name": "admin","password": "trendy123"}}},"scope": {"project": {"domain": {"name": "Default"},"name": "admin"}}}}'|grep X-Subject-Token % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 5575 100 5362 100 213 12924 513 --:X-Subject-Token: a0e8c4b32a324caf9ce6e6d77d67edd8 --:-- --:--:-- --:--:-- 12920
2 get方法(获取虚拟机)
[root@controller01 ~]# curl -i http://controller:8774/v2.1/4a3b38b999da4c37bd049a2d5c37920f/servers -v -H "X-Auth-Token:a0e8c4b32a324caf9ce6e6d77d67edd8" * About to connect() to controller port 8774 (#0) * Trying 172.20.1.13... * Connected to controller (172.20.1.13) port 8774 (#0) > GET /v2.1/4a3b38b999da4c37bd049a2d5c37920f/servers HTTP/1.1 > User-Agent: curl/7.29.0 > Host: controller:8774 > Accept: */* > X-Auth-Token:a0e8c4b32a324caf9ce6e6d77d67edd8 > < HTTP/1.1 200 OK HTTP/1.1 200 OK < Content-Length: 706 Content-Length: 706 < Content-Type: application/json Content-Type: application/json < X-Openstack-Nova-Api-Version: 2.1 X-Openstack-Nova-Api-Version: 2.1 < Vary: X-OpenStack-Nova-API-Version Vary: X-OpenStack-Nova-API-Version < X-Compute-Request-Id: req-60e0c85d-55ce-4771-977d-1a7513dfd3e8 X-Compute-Request-Id: req-60e0c85d-55ce-4771-977d-1a7513dfd3e8 < Date: Tue, 20 Mar 2018 03:27:23 GMT Date: Tue, 20 Mar 2018 03:27:23 GMT < * Connection #0 to host controller left intact {"servers": [{"id": "7e2993be-10bd-4481-929c-b6adff09d72c", "links": [{"href": "http://controller:8774/v2.1/4a3b38b999da4c37bd049a2d5c37920f/servers/7e2993be-10bd-4481-929c-b6adff09d72c", "rel": "self"}, {"href": "http://controller:8774/4a3b38b999da4c37bd049a2d5c37920f/servers/7e2993be-10bd-4481-929c-b6adff09d72c", "rel": "bookmark"}], "name": "Lidb_test"}, {"id": "d9413831-756c-43dd-a027-57b45749f085", "links": [{"href": "http://controller:8774/v2.1/4a3b38b999da4c37bd049a2d5c37920f/servers/d9413831-756c-43dd-a027-57b45749f085", "rel": "self"}, {"href": "http://controller:8774/4a3b38b999da4c37bd049a2d5c37920f/servers/d9413831-756c-43dd-a027-57b45749f085", "rel": "bookmark"}], "name": "LTM-test"}]}
3 post方法(举例启动虚拟机)
curl -i 'http://mycontroller:8774/v2.1/tenant-id/servers/server-id/action' -X POST -d '{"os-start": null}' -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token:token-id"
生活不会突变,你要做的只是耐心和积累。人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。你的时间有限,做喜欢的事情会令人愉悦,所以跟随自己的本心。