OpenStack API 初探(调用API获取OpenStack信息)
来源:互联网 发布:2016网络小说知乎50 编辑:程序博客网 时间:2024/05/27 10:43
本文将介绍如何调用OpenStack的Restful API来获取系统的一些信息。仅仅只是初探,给出了两个API的调用全过程。
最开始写这篇的时候,是写给同事查阅的,英文版的(但是是极简单的英文)。后续会给出中文版,先粘贴出来。
1.Some environment variables setting
1.1 Download OpenStack RC file. Login to the OpenStack dashboard as administrator. Choose the admin project (the button is on the left-top of the main page) and then download the OpenStack RC V3 file (the button is on the right-top of the main page).
1.2 Modify the RC file:
1.2.1 Comment the three lines below:
#echo “Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: ”
#read -sr OS_PASSWORD_INPUT
#export OS_PASSWORD=$OS_PASSWORD_INPUT
1.2.2 Add one line:
export OS_PASSWORD=”passwd”
(Replace ‘passwd’ to the password of current log in administrator user)
1.2.3 Additional code:
if [ -z $OS_PROJECT_DOMAIN_NAME ]; then export OS_PROJECT_DOMAIN_NAME=Defaultfi
2.Call Restful API to get the host system information
2.1 Source the RC file fixed in step 1 to set particular environment variables.
Command: source ‘RcFile.sh’
(Replace the ‘RcFile.sh’ to your RC file name)
2.2 Call Restful API to get authentic token. (Here is a sample of curl command)
curl -s -i POST \$OS_AUTH_URL/auth/tokens?nocatalog -H "Content-Type: application/json" -d '{"auth":{"identity":{"methods":["password"],"password":{"user":{"domain":{"name":"'"\$OS_USER_DOMAIN_NAME"'"},"name":"'"\$OS_USERNAME"'","password":"'"\$OS_PASSWORD"'"}}},"scope":{"project":{"domain":{"name":"'"\$OS_PROJECT_DOMAIN_NAME"'"},"name":"'"\$OS_PROJECT_NAME"'"}}}}
The OS_AUTH_URL, OS_USER_DOMAIN_NAME, OS_PASSWORD, OS_PROJECT_DOMAIN_NAME and OS_PROJECT_NAME in this command are defined in the RC file. And they have been sourced into running context in last step.
2.3 Set OS_TOKEN environment variable.
2.3.1 Extract the value of ‘X-Subject-Token’ from the response header of the request in step 2.
2.3.2 export OS_TOKEN=”$XSubjectTokenData”
(Replace $XSubjectTokenData to the value of ‘X-Subject-Toke’.)
2.4 Call Restful API to list all hypervisors’ information.
2.4.1 Extract destination host address from ‘OS_AUTH_URL’
The OS_AUTH_URL is a string like “http://10.62.235.105:5000/v3”. We need extract the host address below from it.
HostAddr=http://10.62.235.105
2.4.2 Call API
curl -s -H “X-Auth-Token: $OS_TOKEN” $HostAddr:8774/v2.1/$OS_PROJECT_ID/os-hypervisors/detail
The ‘HosAddr’ is set in last step.
Use JsonTool of python to beauty print the output of this command:
{ "hypervisors": [ { "cpu_info": "{\"vendor\": \"Intel\", \"model\": \"Broadwell\", \"arch\": \"x86_64\", \"features\": [\"pge\", \"avx\", \"xsaveopt\", \"clflush\", \"sep\", \"rtm\", \"tsc_adjust\", \"vme\", \"dtes64\", \"invpcid\", \"tsc\", \"fsgsbase\", \"xsave\", \"smap\", \"bmi2\", \"vmx\", \"erms\", \"xtpr\", \"cmov\", \"hle\", \"smep\", \"pcid\", \"est\", \"pat\", \"monitor\", \"smx\", \"pbe\", \"lm\", \"msr\", \"adx\", \"3dnowprefetch\", \"nx\", \"fxsr\", \"syscall\", \"tm\", \"sse4.1\", \"pae\", \"sse4.2\", \"pclmuldq\", \"acpi\", \"fma\", \"pni\", \"tsc-deadline\", \"popcnt\", \"mmx\", \"osxsave\", \"cx8\", \"mce\", \"de\", \"rdtscp\", \"ht\", \"dca\", \"lahf_lm\", \"abm\", \"rdseed\", \"pdcm\", \"mca\", \"pdpe1gb\", \"mbm_local\", \"sse\", \"f16c\", \"pse\", \"ds\", \"invtsc\", \"mbm_total\", \"tm2\", \"avx2\", \"aes\", \"sse2\", \"ss\", \"ds_cpl\", \"arat\", \"bmi1\", \"apic\", \"ssse3\", \"fpu\", \"cx16\", \"pse36\", \"mtrr\", \"movbe\", \"rdrand\", \"cmt\", \"x2apic\"], \"topology\": {\"cores\": 10, \"cells\": 2, \"threads\": 2, \"sockets\": 1}}", "current_workload": 0, "disk_available_least": 136, "free_disk_gb": 371, "free_ram_mb": 253346, "host_ip": "10.62.235.105", "hypervisor_hostname": "localhost.localdomain", "hypervisor_type": "QEMU", "hypervisor_version": 2009000, "id": 1, "local_gb": 499, "local_gb_used": 238, "memory_mb": 262050, "memory_mb_used": 56602, "running_vms": 1, "service": { "disabled_reason": null, "host": "localhost.localdomain", "id": 32 }, "state": "up", "status": "enabled", "vcpus": 40, "vcpus_used": 2 } ]}
Each field meaning:
3. Call Restful API to list all instances in the OpenStack.
3.1 The all pre-operations are same to Step 1 to Step 2.3.
3.2 The term of instance is called server on OpenStack. The Restful API is below:
curl -s -H "X-Auth-Token: $OS_TOKEN" $HostAddr:8774/v2.1/$OS_PROJECT_ID/servers
Use JsonTool to beauty show the result as below:
{ "servers": [ { "id": "57ea68f5-127b-4171-9edb-0cf824eecb57", "links": [ { "href": "http://10.62.235.105:8774/v2.1/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57", "rel": "self" }, { "href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57", "rel": "bookmark" } ], "name": "v750OsUpgradeTest" } ]}
The all instances are shown as the array “servers”. We only care the name of each server.
The above result shows that there is only one instance in current OpenStack. And its name is “v750OsUpgradeTest”.
3.3 List the details of each instance.
curl -s -H "X-Auth-Token: $OS_TOKEN" $HostAddr:8774/v2.1//$OS_PROJECT_ID/detail | python -m json.tool
The result:
{ "servers": [ { "OS-DCF:diskConfig": "AUTO", "OS-EXT-AZ:availability_zone": "nova", "OS-EXT-STS:power_state": 1, "OS-EXT-STS:task_state": null, "OS-EXT-STS:vm_state": "active", "OS-SRV-USG:launched_at": "2017-11-15T17:36:36.000000", "OS-SRV-USG:terminated_at": null, "accessIPv4": "", "accessIPv6": "", "addresses": { "external_network": [ { "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:1f:53:03", "OS-EXT-IPS:type": "fixed", "addr": "10.62.235.110", "version": 4 } ] }, "config_drive": "", "created": "2017-11-15T17:34:53Z", "flavor": { "id": "604e722b-ceb5-4a94-a40a-640fdb570a5e", "links": [ { "href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/flavors/604e722b-ceb5-4a94-a40a-640fdb570a5e", "rel": "bookmark" } ] }, "hostId": "bfa38a8fd399e73b47731e4d2ec2f43ab9b2834436b854c0ecf86a1f", "id": "57ea68f5-127b-4171-9edb-0cf824eecb57", "image": { "id": "f2539194-b274-4049-9373-2ccd51b1dfbd", "links": [ { "href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/images/f2539194-b274-4049-9373-2ccd51b1dfbd", "rel": "bookmark" } ] }, "key_name": null, "links": [ { "href": "http://10.62.235.105:8774/v2.1/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57", "rel": "self" }, { "href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57", "rel": "bookmark" } ], "metadata": {}, "name": "v750OsUpgradeTest", "os-extended-volumes:volumes_attached": [ { "id": "a68b421c-3cc9-4918-a79d-60147a55f032" }, { "id": "8adb9dd3-a46e-4372-94b3-4f99b9fbc984" }, { "id": "b4c0e20d-824d-4ed4-9e8b-cb8499b1a72b" } ], "progress": 0, "security_groups": [ { "name": "default" } ], "status": "ACTIVE", "tenant_id": "ace44a62c3ab4c1dbdb320b5c4f40518", "updated": "2017-11-15T17:41:41Z", "user_id": "cdcb30d5cb684395a1319abee73780d6" } ]}
- OpenStack API 初探(调用API获取OpenStack信息)
- OpenStack之RESTful API调用(一)
- OpenStack之RESTful API调用(二)
- 通过Rest API调用OpenStack
- java 调用 openstack API开发
- restclient 测试 调用openstack api
- openstack rest api 调用示例
- openstack API
- openstack API
- Openstack API
- 【OpenStack】【转载】OpenStack extension api
- openstack api 指南(转)
- openStack 调用API方式create router
- 通过Python调用OpenStack 的RESTful API
- openstack中API调用源码分析
- Openstack REST API 调用开发入门
- openstack中API调用源码分析
- OpenStack通过Restful api获取token
- 最小生成树模版题Prim——修建道路
- 利用json-lib给前台传递json数据
- Servlet实现文件上传的原理
- 编译原理——短语、直接短语、句柄
- 【51nod1792】Jabby's segment tree
- OpenStack API 初探(调用API获取OpenStack信息)
- 1137. Final Grading (25)
- [Android问答] px、dp和sp,这些单位有什么区别?
- cfA. Pizza Separation(前缀和)
- 有关JAVA-IO的概述
- 2017-12-9框架
- Python 正则表达式笔记(1) 常见字符
- 第6章gp_toolkit管理架构-Checking Query Disk Spill Space Usage
- lua I/O