一 系统环境

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. vmware下安装centos7

    下载vmware http://down-www.newasp.net/pcdown/big/wm_pro_14_win.rar 下载centos7 https://www.centos.org/do ...

  2. XDU 1111

    对于一排n个正方形,有f(n)种方案达成目标,若第n个块是白色,则有f(n-1)种方案,若第n个块是黑色,则第n-1个块必为白色,那么有f(n-2)+n-2种方案. 则f(n)=f(n-1)+f(n- ...

  3. hdu6121 Build a tree

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121 题面: Build a tree Time Limit: 2000/1000 MS (J ...

  4. 基础知识总结之 jdk部分

    第一次安装jdk 按照操作走完  会出现 C:\Program Files\Java\jdk1.8.0_91 和 C:\Program Files\Java\jre1.8.0_91 两个目录 (平级目 ...

  5. MYSQL基本操作总结

    (1)cd c:/ //进入c:\-> (2)mysql -h 127.0.0.1 -u root -p //进入数据库 SHOW DATABASES; //显示所有数据库 CREATE DAT ...

  6. click事件多次触发 jQuery

    jQuery 中 click事件会累计绑定 例如下列代码: aNode.click(function(){ bNode.click(function(){ console.log('haha'); } ...

  7. 如何用纯 CSS 创作气泡填色的按钮特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKqZjy 可交互视频 ...

  8. 20145109 《Java程序设计》第九周学习总结

    JDBC 1 . DriverManager Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 2 . ...

  9. .Net Core Cookie跨站点共享 会话保持

    这里使用简单粗暴的方式,只为做个记录. 关键配置: services.AddDataProtection() .SetApplicationName("appname") .Dis ...

  10. 【jdk源码分析】java多线程开启的三种方式

    1.继承Thread类,新建一个当前类对象,并且运行其start()方法 package com.xiaostudy.thread; /** * @desc 第一种开启线程的方式 * @author ...