curl调用openstack API总结

curl

curl是利用 URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在 Unix、多种 Linux发行版中,并且有 DOS和 Win32、Win64下的移植版本。

其支持...

DICT,FILE,FTP,FTPS,Gopher,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,POP3,POP3S,RTMP,RTSP,SCP,SFTP,SMB,SMBS,SMTP,SMTPS,Telnet和TFTP。curl支持SSL证书,HTTP POST,HTTP PUT,FTP上传,基于HTTP表单的上传,代理服务器,HTTP / 2,cookies,用户名密码认证(Basic,Plain,Digest,CRAM-MD5,NTLM,Negotiate and Kerberos)等等。

 

 

 

 

 

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

首先我们直接使用命令,利用debug查看。

[root@controller ~]# openstack role list --debug

显示结果:

 1 START with options: [u'role', u'list', u'--debug']
 2 options: Namespace(access_key='', access_secret='***', access_token='***', access_token_endpoint='', access_token_type='', auth_type='', auth_url='http://controller:35357/v3', cacert=None, cert='', client_id='', client_secret='***', cloud='', code='', consumer_key='', consumer_secret='***', debug=True, default_domain='default', default_domain_id='', default_domain_name='', deferred_help=False, discovery_endpoint='', domain_id='', domain_name='', endpoint='', identity_provider='', identity_provider_url='', insecure=None, interface='', key='', log_file=None, old_profile=None, openid_scope='', os_beta_command=False, os_compute_api_version='', os_data_processing_api_version='1.1', os_data_processing_url='', os_database_api_version='1', os_dns_api_version='2', os_identity_api_version='3', os_image_api_version='2', os_key_manager_api_version='1', 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='', os_workflow_api_version='2', passcode='', password='***', profile=None, project_domain_id='', project_domain_name='Default', project_id='', project_name='admin', protocol='', redirect_uri='', region_name='', timing=False, token='***', trust_id='', url='', user_domain_id='', user_domain_name='Default', user_id='', username='admin', verbose_level=3, verify=None)
 3 Auth plugin password selected
 4 auth_config_hook(): {'auth_type': 'password', 'beta_command': False, u'compute_api_version': u'2', 'key': None, u'database_api_version': '1', 'data_processing_api_version': '1.1', 'auth_url': 'http://controller:35357/v3', u'network_api_version': u'2', u'message': u'', u'image_format': u'qcow2', 'networks': [], u'image_api_version': '2', 'verify': True, u'dns_api_version': '2', u'object_store_api_version': u'1', 'username': 'admin', u'container_infra_api_version': u'1', 'verbose_level': 3, 'region_name': '', 'api_timeout': None, u'baremetal_api_version': u'1', 'auth': {'user_domain_name': 'Default', 'project_name': 'admin', '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, 'password': '***', u'application_catalog_api_version': u'1', 'cacert': None, u'key_manager_api_version': '1', u'metering_api_version': u'2', 'deferred_help': False, u'identity_api_version': '3', 'workflow_api_version': '2', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', u'status': u'active', 'debug': True, u'interface': None, u'disable_vendor_agent': {}}
 5 defaults: {u'auth_type': 'password', u'status': u'active', 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', u'container_infra_api_version': u'1', u'metering_api_version': u'2', u'image_api_use_tasks': False, u'floating_ip_source': u'neutron', u'orchestration_api_version': u'1', 'cacert': None, u'network_api_version': u'2', u'message': u'', u'image_format': u'qcow2', u'application_catalog_api_version': u'1', u'key_manager_api_version': u'v1', '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'interface': None, u'disable_vendor_agent': {}}
 6 cloud cfg: {'auth_type': 'password', 'beta_command': False, u'compute_api_version': u'2', 'key': None, u'database_api_version': '1', 'data_processing_api_version': '1.1', 'auth_url': 'http://controller:35357/v3', u'network_api_version': u'2', u'message': u'', u'image_format': u'qcow2', 'networks': [], u'image_api_version': '2', 'verify': True, u'dns_api_version': '2', u'object_store_api_version': u'1', 'username': 'admin', u'container_infra_api_version': u'1', 'verbose_level': 3, 'region_name': '', 'api_timeout': None, u'baremetal_api_version': u'1', 'auth': {'user_domain_name': 'Default', 'project_name': 'admin', '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, 'password': '***', u'application_catalog_api_version': u'1', 'cacert': None, u'key_manager_api_version': '1', u'metering_api_version': u'2', 'deferred_help': False, u'identity_api_version': '3', 'workflow_api_version': '2', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', u'status': u'active', 'debug': True, u'interface': None, u'disable_vendor_agent': {}}
 7 compute API version 2, cmd group openstack.compute.v2
 8 network API version 2, cmd group openstack.network.v2
 9 image API version 2, cmd group openstack.image.v2
10 volume API version 2, cmd group openstack.volume.v2
11 identity API version 3, cmd group openstack.identity.v3
12 object_store API version 1, cmd group openstack.object_store.v1
13 neutronclient API version 2, cmd group openstack.neutronclient.v2
14 dns API version 2, cmd group openstack.dns.v2
15 orchestration API version 1, cmd group openstack.orchestration.v1
16 workflow_engine API version 2, cmd group openstack.workflow_engine.v2
17 database API version 1, cmd group openstack.database.v1
18 data_processing API version 1.1, cmd group openstack.data_processing.v1
19 key_manager API version 1, cmd group openstack.key_manager.v1
20 Auth plugin password selected
21 auth_config_hook(): {'auth_type': 'password', 'beta_command': False, u'compute_api_version': u'2', 'key': None, u'database_api_version': '1', 'data_processing_api_version': '1.1', 'auth_url': 'http://controller:35357/v3', u'network_api_version': u'2', u'message': u'', u'image_format': u'qcow2', 'networks': [], u'image_api_version': '2', 'verify': True, u'dns_api_version': '2', u'object_store_api_version': u'1', 'username': 'admin', u'container_infra_api_version': u'1', 'verbose_level': 3, 'region_name': '', 'api_timeout': None, u'baremetal_api_version': u'1', 'auth': {'user_domain_name': 'Default', 'project_name': 'admin', '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, 'password': '***', u'application_catalog_api_version': u'1', 'cacert': None, u'key_manager_api_version': '1', u'metering_api_version': u'2', 'deferred_help': False, u'identity_api_version': '3', 'workflow_api_version': '2', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', u'status': u'active', 'debug': True, u'interface': None, u'disable_vendor_agent': {}}
22 Auth plugin password selected
23 auth_config_hook(): {'auth_type': 'password', 'beta_command': False, u'compute_api_version': u'2', 'key': None, u'database_api_version': '1', 'data_processing_api_version': '1.1', 'auth_url': 'http://controller:35357/v3', u'network_api_version': u'2', u'message': u'', u'image_format': u'qcow2', 'networks': [], u'image_api_version': '2', 'verify': True, u'dns_api_version': '2', u'object_store_api_version': u'1', 'username': 'admin', u'container_infra_api_version': u'1', 'verbose_level': 3, 'region_name': '', 'api_timeout': None, u'baremetal_api_version': u'1', 'auth': {'user_domain_name': 'Default', 'project_name': 'admin', '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, 'password': '***', u'application_catalog_api_version': u'1', 'cacert': None, u'key_manager_api_version': '1', u'metering_api_version': u'2', 'deferred_help': False, u'identity_api_version': '3', 'workflow_api_version': '2', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', u'status': u'active', 'debug': True, u'interface': None, u'disable_vendor_agent': {}}
24 command: role list -> openstackclient.identity.v3.role.ListRole
25 Auth plugin password selected
26 auth_config_hook(): {'auth_type': 'password', 'beta_command': False, u'compute_api_version': u'2', 'key': None, u'database_api_version': '1', 'data_processing_api_version': '1.1', 'auth_url': 'http://controller:35357/v3', u'network_api_version': u'2', u'message': u'', u'image_format': u'qcow2', 'networks': [], u'image_api_version': '2', 'verify': True, u'dns_api_version': '2', u'object_store_api_version': u'1', 'username': 'admin', u'container_infra_api_version': u'1', 'verbose_level': 3, 'region_name': '', 'api_timeout': None, u'baremetal_api_version': u'1', 'auth': {'user_domain_name': 'Default', 'project_name': 'admin', '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, 'password': '***', u'application_catalog_api_version': u'1', 'cacert': None, u'key_manager_api_version': '1', u'metering_api_version': u'2', 'deferred_help': False, u'identity_api_version': '3', 'workflow_api_version': '2', u'volume_api_version': u'2', 'cert': None, u'secgroup_source': u'neutron', u'status': u'active', 'debug': True, u'interface': None, u'disable_vendor_agent': {}}
27 Using auth plugin: password
28 Using parameters {'username': 'admin', 'project_name': 'admin', 'user_domain_name': 'Default', 'auth_url': 'http://controller:35357/v3', 'password': '***', 'project_domain_name': 'Default'}
29 Get auth_ref
30 REQ: curl -g -i -X GET http://controller:35357/v3 -H "Accept: application/json" -H "User-Agent: osc-lib/1.3.0 keystoneauth1/2.18.0 python-requests/2.11.1 CPython/2.7.5"
31 Starting new HTTP connection (1): controller
32 "GET /v3 HTTP/1.1" 200 250
33 RESP: [200] Date: Thu, 21 Dec 2017 08:28:06 GMT Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 Vary: X-Auth-Token x-openstack-request-id: req-757b2e59-bbc6-4351-8de5-5c2b1a55f540 Content-Length: 250 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/json 
34 RESP BODY: {"version": {"status": "stable", "updated": "2017-02-22T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v3.8", "links": [{"href": "http://controller:35357/v3/", "rel": "self"}]}}
35 
36 GET call to None for http://controller:35357/v3 used request id req-757b2e59-bbc6-4351-8de5-5c2b1a55f540
37 Making authentication request to http://controller:35357/v3/auth/tokens
38 "POST /v3/auth/tokens HTTP/1.1" 201 4693
39 {"token": {"is_domain": false, "methods": ["password"], "roles": [{"id": "b4b688c6bbae4ea39098a022850d1dce", "name": "admin"}], "expires_at": "2017-12-21T09:28:07.000000Z", "project": {"domain": {"id": "default", "name": "Default"}, "id": "b891812a986648739b6a702a4ff5b5c1", "name": "admin"}, "catalog": [{"endpoints": [{"url": "http://controller:8774/v2.1", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "6464f289591b40d886870bdb80a628c4"}, {"url": "http://controller:8774/v2.1", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "71a0f745e96149ca917f0ca6437d8d9b"}, {"url": "http://controller:8774/v2.1", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "a0bc9463e27249a1909ba3bef125d8fb"}], "type": "compute", "id": "13353857ef0743a29c438fd46b34c838", "name": "nova"}, {"endpoints": [], "type": "haha", "id": "263ceccf712242ceaa58dffbcb09fd7e", "name": "hehe"}, {"endpoints": [{"url": "http://controller:8776/v3/b891812a986648739b6a702a4ff5b5c1", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "26ccc7b72ba74aedb8cd698e2a905065"}, {"url": "http://controller:8776/v3/b891812a986648739b6a702a4ff5b5c1", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "79c62f5352b0436eaf5db176e112b514"}, {"url": "http://controller:8776/v3/b891812a986648739b6a702a4ff5b5c1", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "f8f7f0eaf89c4d85a1882e3acb722981"}], "type": "volumev3", "id": "511157ba011c45c58b585fc4c05405ca", "name": "cinderv3"}, {"endpoints": [{"url": "http://controller:5000/v3/", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "22df58f2514f4d6da0c25b64a4e3050b"}, {"url": "http://controller:35357/v3/", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "44def12f4ff445bfa3b1e04746417cde"}, {"url": "http://controller:5000/v3/", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "c422372c3c974b20a2ef05e5b926e64c"}], "type": "identity", "id": "683ed52c1ace40ee92aba1149710f922", "name": "keystone"}, {"endpoints": [{"url": "http://controller:9292", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "17f538e8b2cb412f911d14a81db808d3"}, {"url": "http://controller:9292", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "48c03233efb14410a95dc508abfaa34d"}, {"url": "http://controller:9292", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "6149e3cc56b34c85a16456ed56b7f05f"}], "type": "image", "id": "9a11ba64481f415d819203c14507196c", "name": "glance"}, {"endpoints": [{"url": "http://controller:9696", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "0c6d5ba3383244198fa04778491f2805"}, {"url": "http://controller:9696", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "31942a3f6466400ebce28a480eb09195"}, {"url": "http://controller:9696", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "af179b4e7d23444695cd75abdd999aba"}], "type": "network", "id": "c80031f4b6db4bcab36b90d86f0ecb83", "name": "neutron"}, {"endpoints": [{"url": "http://controller:8778", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "5112af2c3fd0427cafb1c300e94eed40"}, {"url": "http://controller:8778", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "9042f1bcea5241cdb6061980956ad080"}, {"url": "http://controller:8778", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "e11cc63e66a248659db7330f3bd11300"}], "type": "placement", "id": "cc96cdac927641ff9f923e9e6f670b85", "name": "placement"}, {"endpoints": [{"url": "http://controller:8776/v2/b891812a986648739b6a702a4ff5b5c1", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "36a45c745c5e468c92271e8182011f2a"}, {"url": "http://controller:8776/v2/b891812a986648739b6a702a4ff5b5c1", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "41329a5795964acc926825b87dee45b7"}, {"url": "http://controller:8776/v2/b891812a986648739b6a702a4ff5b5c1", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "9194c1b4b6c44a16b204a3d85d934c28"}], "type": "volumev2", "id": "cce3ce69c57f459f80e4b057948fdd8e", "name": "cinderv2"}], "user": {"domain": {"id": "default", "name": "Default"}, "password_expires_at": null, "name": "admin", "id": "0d7def2d27524bba951f72569ed87ef0"}, "audit_ids": ["gENYhjx2QiqolfJtbRxVFQ"], "issued_at": "2017-12-21T08:28:07.000000Z"}}
40 run(Namespace(columns=[], domain=None, formatter='table', group=None, group_domain=None, inherited=False, max_width=0, noindent=False, print_empty=False, project=None, project_domain=None, quote_mode='nonnumeric', user=None, user_domain=None))
41 Instantiating identity client: <class 'keystoneclient.v3.client.Client'>
42 Making authentication request to http://controller:35357/v3/auth/tokens
43 "POST /v3/auth/tokens HTTP/1.1" 201 4693
44 {"token": {"is_domain": false, "methods": ["password"], "roles": [{"id": "b4b688c6bbae4ea39098a022850d1dce", "name": "admin"}], "expires_at": "2017-12-21T09:28:07.000000Z", "project": {"domain": {"id": "default", "name": "Default"}, "id": "b891812a986648739b6a702a4ff5b5c1", "name": "admin"}, "catalog": [{"endpoints": [{"url": "http://controller:8774/v2.1", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "6464f289591b40d886870bdb80a628c4"}, {"url": "http://controller:8774/v2.1", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "71a0f745e96149ca917f0ca6437d8d9b"}, {"url": "http://controller:8774/v2.1", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "a0bc9463e27249a1909ba3bef125d8fb"}], "type": "compute", "id": "13353857ef0743a29c438fd46b34c838", "name": "nova"}, {"endpoints": [], "type": "haha", "id": "263ceccf712242ceaa58dffbcb09fd7e", "name": "hehe"}, {"endpoints": [{"url": "http://controller:8776/v3/b891812a986648739b6a702a4ff5b5c1", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "26ccc7b72ba74aedb8cd698e2a905065"}, {"url": "http://controller:8776/v3/b891812a986648739b6a702a4ff5b5c1", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "79c62f5352b0436eaf5db176e112b514"}, {"url": "http://controller:8776/v3/b891812a986648739b6a702a4ff5b5c1", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "f8f7f0eaf89c4d85a1882e3acb722981"}], "type": "volumev3", "id": "511157ba011c45c58b585fc4c05405ca", "name": "cinderv3"}, {"endpoints": [{"url": "http://controller:5000/v3/", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "22df58f2514f4d6da0c25b64a4e3050b"}, {"url": "http://controller:35357/v3/", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "44def12f4ff445bfa3b1e04746417cde"}, {"url": "http://controller:5000/v3/", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "c422372c3c974b20a2ef05e5b926e64c"}], "type": "identity", "id": "683ed52c1ace40ee92aba1149710f922", "name": "keystone"}, {"endpoints": [{"url": "http://controller:9292", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "17f538e8b2cb412f911d14a81db808d3"}, {"url": "http://controller:9292", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "48c03233efb14410a95dc508abfaa34d"}, {"url": "http://controller:9292", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "6149e3cc56b34c85a16456ed56b7f05f"}], "type": "image", "id": "9a11ba64481f415d819203c14507196c", "name": "glance"}, {"endpoints": [{"url": "http://controller:9696", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "0c6d5ba3383244198fa04778491f2805"}, {"url": "http://controller:9696", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "31942a3f6466400ebce28a480eb09195"}, {"url": "http://controller:9696", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "af179b4e7d23444695cd75abdd999aba"}], "type": "network", "id": "c80031f4b6db4bcab36b90d86f0ecb83", "name": "neutron"}, {"endpoints": [{"url": "http://controller:8778", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "5112af2c3fd0427cafb1c300e94eed40"}, {"url": "http://controller:8778", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "9042f1bcea5241cdb6061980956ad080"}, {"url": "http://controller:8778", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "e11cc63e66a248659db7330f3bd11300"}], "type": "placement", "id": "cc96cdac927641ff9f923e9e6f670b85", "name": "placement"}, {"endpoints": [{"url": "http://controller:8776/v2/b891812a986648739b6a702a4ff5b5c1", "interface": "internal", "region": "RegionOne", "region_id": "RegionOne", "id": "36a45c745c5e468c92271e8182011f2a"}, {"url": "http://controller:8776/v2/b891812a986648739b6a702a4ff5b5c1", "interface": "admin", "region": "RegionOne", "region_id": "RegionOne", "id": "41329a5795964acc926825b87dee45b7"}, {"url": "http://controller:8776/v2/b891812a986648739b6a702a4ff5b5c1", "interface": "public", "region": "RegionOne", "region_id": "RegionOne", "id": "9194c1b4b6c44a16b204a3d85d934c28"}], "type": "volumev2", "id": "cce3ce69c57f459f80e4b057948fdd8e", "name": "cinderv2"}], "user": {"domain": {"id": "default", "name": "Default"}, "password_expires_at": null, "name": "admin", "id": "0d7def2d27524bba951f72569ed87ef0"}, "audit_ids": ["HTeZAQgtTK6JGDUz1ys6pA"], "issued_at": "2017-12-21T08:28:07.000000Z"}}
45 REQ: curl -g -i -X GET http://controller:35357/v3/ -H "Accept: application/json" -H "User-Agent: osc-lib/1.3.0 keystoneauth1/2.18.0 python-requests/2.11.1 CPython/2.7.5"
46 "GET /v3/ HTTP/1.1" 200 250
47 RESP: [200] Date: Thu, 21 Dec 2017 08:28:07 GMT Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 Vary: X-Auth-Token x-openstack-request-id: req-4484299b-d8bb-4deb-a741-1331d4c4f825 Content-Length: 250 Keep-Alive: timeout=5, max=97 Connection: Keep-Alive Content-Type: application/json 
48 RESP BODY: {"version": {"status": "stable", "updated": "2017-02-22T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v3.8", "links": [{"href": "http://controller:35357/v3/", "rel": "self"}]}}
49 
50 GET call to None for http://controller:35357/v3/ used request id req-4484299b-d8bb-4deb-a741-1331d4c4f825
51 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}a1168b8f89263b83221117c6326bbffb8d7bb165"
52 "GET /v3/roles HTTP/1.1" 200 1840
53 RESP: [200] Date: Thu, 21 Dec 2017 08:28:07 GMT Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 Vary: X-Auth-Token x-openstack-request-id: req-198cb6e2-0dd2-4c42-8e34-57578f13b7d5 Content-Length: 1840 Keep-Alive: timeout=5, max=96 Connection: Keep-Alive Content-Type: application/json 
54 RESP BODY: {"links": {"self": "http://controller:35357/v3/roles", "previous": null, "next": null}, "roles": [{"domain_id": null, "id": "06c434f4d7844682875471a366dfd83c", "links": {"self": "http://controller:35357/v3/roles/06c434f4d7844682875471a366dfd83c"}, "name": "cinder02_role"}, {"domain_id": null, "id": "48627d4bc04345a1a0be6f97609c4009", "links": {"self": "http://controller:35357/v3/roles/48627d4bc04345a1a0be6f97609c4009"}, "name": "keystone01_role"}, {"domain_id": null, "id": "70273399a6864eb7ab00b1d6add2ae50", "links": {"self": "http://controller:35357/v3/roles/70273399a6864eb7ab00b1d6add2ae50"}, "name": "user"}, {"domain_id": null, "id": "9fe2ff9ee4384b1894a90878d3e92bab", "links": {"self": "http://controller:35357/v3/roles/9fe2ff9ee4384b1894a90878d3e92bab"}, "name": "_member_"}, {"domain_id": null, "id": "a887ee92e8b640a6813e1055471369fa", "links": {"self": "http://controller:35357/v3/roles/a887ee92e8b640a6813e1055471369fa"}, "name": "cinder01_role"}, {"domain_id": null, "id": "b27b8b59a723468aba8ba58305df969b", "links": {"self": "http://controller:35357/v3/roles/b27b8b59a723468aba8ba58305df969b"}, "name": "glance01_role"}, {"domain_id": null, "id": "b4b688c6bbae4ea39098a022850d1dce", "links": {"self": "http://controller:35357/v3/roles/b4b688c6bbae4ea39098a022850d1dce"}, "name": "admin"}, {"domain_id": null, "id": "bc1d9b8c4fa64f0f82142b18df3b7c9f", "links": {"self": "http://controller:35357/v3/roles/bc1d9b8c4fa64f0f82142b18df3b7c9f"}, "name": "neutron02_role"}, {"domain_id": null, "id": "cb52f77f5d7046b79efbfb7a46285df6", "links": {"self": "http://controller:35357/v3/roles/cb52f77f5d7046b79efbfb7a46285df6"}, "name": "neutron01_role"}, {"domain_id": null, "id": "f069eb6842b1428b93387dab50663a48", "links": {"self": "http://controller:35357/v3/roles/f069eb6842b1428b93387dab50663a48"}, "name": "glance02_role"}]}
55 
56 GET call to identity for http://controller:35357/v3/roles used request id req-198cb6e2-0dd2-4c42-8e34-57578f13b7d5
57 +----------------------------------+-----------------+
58 | ID                               | Name            |
59 +----------------------------------+-----------------+
60 | 06c434f4d7844682875471a366dfd83c | cinder02_role   |
61 | 48627d4bc04345a1a0be6f97609c4009 | keystone01_role |
62 | 70273399a6864eb7ab00b1d6add2ae50 | user            |
63 | 9fe2ff9ee4384b1894a90878d3e92bab | _member_        |
64 | a887ee92e8b640a6813e1055471369fa | cinder01_role   |
65 | b27b8b59a723468aba8ba58305df969b | glance01_role   |
66 | b4b688c6bbae4ea39098a022850d1dce | admin           |
67 | bc1d9b8c4fa64f0f82142b18df3b7c9f | neutron02_role  |
68 | cb52f77f5d7046b79efbfb7a46285df6 | neutron01_role  |
69 | f069eb6842b1428b93387dab50663a48 | glance02_role   |
70 +----------------------------------+-----------------+
71 clean_up ListRole: 
72 END return value: 0
View Code

不用去看完整的结果,太多了太难找了。我们只看其中curl的语句:

curl -g -i -X GET http://controller:35357/v3 -H "Accept: application/json" -H "User-Agent: osc-lib/1.3.0 keystoneauth1/2.18.0 python-requests/2.11.1 CPython/2.7.5"

curl -g -i -X GET http://controller:35357/v3/ -H "Accept: application/json" -H "User-Agent: osc-lib/1.3.0 keystoneauth1/2.18.0 python-requests/2.11.1 CPython/2.7.5"

curl -g -i -X GET http://controller:35357/v3/roles? -H "User-Agent: python-keystoneclient" -H "Accept: application/json" -H "X-Auth-Token:{SHA1}a1168b8f89263b83221117c6326bbffb8d7bb165"

 

在这三个中间,只看第三个。记录下位置(看下面命令和上面的相同之处):

curl http://controller:35357/v3/roles -H "Content-type: application/json" -H "X-Auth-Token:gAAAAABaO25jV6Lodw0dFrGyD30oxcRDlFv8o6Tkst2uKIfV2ux9ipXgtWAZnNXEJjoHn64zfBmOI9vCvEzEMfsrxqHDATCT6nsTFrG48FeJQlXJohnDBrRPP_BdKSMYOrLj6UIqiyDSDBI9ytqsFQ0-G2q4KFvNvAc7ktMjUrgtjQPhgpfMktA"|python -mjson.tool

 

!!!token在这里要自己去获取的!!!

下面是使用curl获取token的方法:

提供user_id和密码,获得token:
# curl -i -X POST http://controller:35357/v3/auth/tokens -H "Content-type: application/json" -d '{"auth": {"identity": {"methods": ["password"],"password": {"user": {"id": "0ebdfa91267c48ee88876d9f5ee1369b","password": "123456"}}},"scope": {"project": {"id": "f7b8022f0794462ba55accbadf8fda37"}}}}'|grep X-Subject-Token
显示结果:X-Subject-Token: 81d579ec7c2d48f1a5fe28d7e1258f56
# curl -i -X POST http://controller:35357/v3/auth/tokens -H "Content-type: application/json" -d '{"auth": {"identity": {"methods": ["password"],"password": {"user": {"id": "0355aaaf717f491792161850435878da","password": "123456"}}},"scope": {"domain": {"id": "660450adcc194c0bbf9e462bb21b0935"}}}}'|grep X-Subject-Token
显示结果:X-Subject-Token: d703659e3560480fbf5a92b772d0d4e4

 

User_id 和 project_id  可以使用openstack user(project) list命令获取。

运行上面curl命令得到的就是token的值。

 

同时,我们也可以使用openstack命令去获取token的值:

[root@controller ~]# openstack token issue
curl调用openstack API总结_第1张图片

使用admin的token列出所有用户:
# curl http://controller:35357/v3/users -H "X-Auth-Token:81d579ec7c2d48f1a5fe28d7e1258f56"|python -mjson.tool

只列出domain_id为660450adcc194c0bbf9e462bb21b0935的用户:
# curl http://controller:35357/v3/users?domain_id=660450adcc194c0bbf9e462bb21b0935 -H "X-Auth-Token:81d579ec7c2d48f1a5fe28d7e1258f56"|python -mjson.tool

列出所有域:
# curl http://controller:35357/v3/domains -H "X-Auth-Token:81d579ec7c2d48f1a5fe28d7e1258f56"|python -mjson.tool

创建用户:
# curl -X POST http://controller:35357/v3/users -H "Content-type: application/json" -H "X-Auth-Token:81d579ec7c2d48f1a5fe28d7e1258f56" -d '{"user": {"default_project_id": "c0d6c4a09b7649a19c394a6cd946f53f","domain_id": "660450adcc194c0bbf9e462bb21b0935","enabled": true,"name": "test001","password":"123456"}}'|python -mjson.tool
# curl -X POST http://controller:35357/v3/users -H "Content-type: application/json" -H "X-Auth-Token:81d579ec7c2d48f1a5fe28d7e1258f56" -d '{"user": {"domain_id": "660450adcc194c0bbf9e462bb21b0935","enabled": true,"name": "test002","password":"123456"}}'|python -mjson.tool


授权用户_member_角色(role_id=9fe2ff9ee4384b1894a90878d3e92bab)以访问项目(project_id=c0d6c4a09b7649a19c394a6cd946f53f):
# curl -X PUT http://controller:35357/v3/projects/c0d6c4a09b7649a19c394a6cd946f53f/users/735c4d1fc8eb4bf8b96ee6866b441d9d/roles/9fe2ff9ee4384b1894a90878d3e92bab -H "X-Auth-Token:22142d114ddc454a9fbf6d282793840e"

授权用户_member_角色(role_id=9fe2ff9ee4384b1894a90878d3e92bab)以访问项目(domain_id=660450adcc194c0bbf9e462bb21b0935):
# curl -X PUT http://controller:35357/v3/domains/660450adcc194c0bbf9e462bb21b0935/users/735c4d1fc8eb4bf8b96ee6866b441d9d/roles/9fe2ff9ee4384b1894a90878d3e92bab -H "X-Auth-Token:22142d114ddc454a9fbf6d282793840e"

如果用户没有任何角色,无法获得token。

删除用户(user_id=31d38aec54684281a993e248835e6d9b)
# curl -X DELETE http://controller:35357/v3/users/31d38aec54684281a993e248835e6d9b -H "X-Auth-Token:81d579ec7c2d48f1a5fe28d7e1258f56"

 

 

此处参照:www.cnblogs.com/endoresu/p/5018688.html

你可能感兴趣的:(curl调用openstack API总结)