一 系统环境

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"

使用curl调试openstack的api的更多相关文章

  1. curl调用openstack API总结

    curl命令是Linux下一个可以使用多种协议收发数据的工具,包括http协议.openstack的API接口都是URL地址:http://controller:35357/v3可以使用curl命令进 ...

  2. 使用curl操作openstack swift

    openstack官网有专门的开发者文档介绍如何使用curl操作swift(http://docs.openstack.org/api/openstack-object-storage/1.0/con ...

  3. OpenStack REST API使用

    以前一直想写博客,但因各种因素一直没写.最近在做OpenStack,而OpenStack对外提供REST API,今天就简要介绍一下OpenStack REST API 使用,关于什么是REST AP ...

  4. 用pdb调试OpenStack Havana

    作为个人学习笔记分享,有任何问题欢迎交流! Note:若是想要用pdb调试OpenStack,必须通过Devstack安装它. Devstack安装Havana过程 1.   使用一般用户(非root ...

  5. Openstack python api 学习文档 api创建虚拟机

    Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack ...

  6. python调用openstack的api,create_instance的程序解析

    python调用openstack的api,create_instance的程序解析 2017年10月17日 15:27:24 CloudXli 阅读数:848   版权声明:本文为博主原创文章,未经 ...

  7. (弃)解读Openstack Identity API v2.0

    目前OpenStack社区提供了详尽的API文档,并配有丰富的说明和示例,关于Identity API v2查看这里, 关于Identity API v3请查看这里. 尽管现在官方已经不建议OpenS ...

  8. 远程调试openstack

    之前一直没有找到方法调试openstack的horizon代码,现在终于找到方法了,特别感谢下面这篇博客,讲解非常清晰: http://blog.csdn.net/tantexian/article/ ...

  9. 利用pycharm远程调试openstack代码

    1.安装pycharm专业版 本文安装pycharm 2016.2.3专业版.网上教程较多,这里不做详细介绍,只要到pycharm官网上下载应用程序进行安装即可. 2.pycharm配置 (1)首先按 ...

随机推荐

  1. SQL Server 使用 Hierarchyid 操作层次结构数据

    层次结构数据定义为一组通过层次结构关系互相关联的数据项. 在层次结构关系中,一个数据项是另一个项的父级或子级. sql server2008开始内置的 hierarchyid 数据类型使存储和查询层次 ...

  2. Android Camera API ISO Setting

    https://stackoverflow.com/questions/2978095/android-camera-api-iso-setting exif this.mCameraParamete ...

  3. win10 安装 tensorflow 并运行helloworld

    win10 安装 tensorflow 并运行helloworld   折腾了一下,在win10上成功安装tensorflow. 1 下载安装python,注意一定要是64位(比如python-3.5 ...

  4. 【读书笔记】《深入浅出nodejs》第五章 内存控制

    海量请求+长时间运行 -> 内存控制 -> 一切资源高效循环利用 1. V8的垃圾回收机制与内存限制 在Node中通过JavaScript使用内存时,只能使用部分内存(64位系统下约1.4 ...

  5. tiny4412的烧录工具minitool安装【学习笔记】

    烧录了半天,在win10下一直就是烧录不进去,但是在Ubuntuh环境却可以,找了很久终于找到了,原来在win10安装minitool驱动的时候没有注意到报了错误,错误内容是驱动的数字签名问题,后来禁 ...

  6. MysQL使用一创建库与表

    数据库简介 人类在进化的过程中,创造了数字.文字.符号等来进行数据的记录,但是承受着认知能力和创造能力的提升,数据量越来越大,对于数据的记录和准确查找,成为了一个重大难题 计算机诞生后,数据开始在计算 ...

  7. oracle的 分表 详解 -----表分区

    此文从以下几个方面来整理关于分区表的概念及操作:         1.表空间及分区表的概念         2.表分区的具体作用         3.表分区的优缺点         4.表分区的几种类 ...

  8. PyTorch源码解读之torch.utils.data.DataLoader(转)

    原文链接 https://blog.csdn.net/u014380165/article/details/79058479 写得特别好!最近正好在学习pytorch,学习一下! PyTorch中数据 ...

  9. ubuntu16.04 python3.5 opencv的安装与卸载(转载)

    转载https://blog.csdn.net/qq_37541097/article/details/79045595 Ubuntu16.04 自带python2.7和python3.5两个版本,默 ...

  10. codeforce 589B枚举

    2017-08-25 12:00:53 writer:pprp 很简单的枚举,但是我调试了很长时间,出现各种各样的问题 /* theme:cf 589B writer:pprp declare:枚举 ...