一、CURL
二、API
三、SDK

一、CURL

root@node-10:~# 
curl https://test.iaas.url:5000 | python -m json.tool

获取 token:
curl -s -X POST \
https://test.iaas.url:5000/v2.0/tokens \
-H "Content-Type: application/json" \
-d '{"auth": {"tenantName": "admin", "passwordCredentials": {"username": "admin", "password":  "admin"}}}' \
| python -m json.tool

List Servers Detailed:
curl -H \
"X-Auth-Token:{{ token }}" \
https://test.iaas.url:8774/v2/30722ef3cedf4df6bc81d63b9ccdd8ee/servers/detail \
| python -m json.tool

{
    "servers": [
        {
            "OS-DCF:diskConfig": "AUTO",
            "OS-EXT-AZ:availability_zone": "nova",
            "OS-EXT-SRV-ATTR:host": "node-11.domain.tld",
            "OS-EXT-SRV-ATTR:hypervisor_hostname": "node-11.domain.tld",
            "OS-EXT-SRV-ATTR:instance_name": "instance-00000015",
            "OS-EXT-STS:power_state": 1,
            "OS-EXT-STS:task_state": null,
            "OS-EXT-STS:vm_state": "active",
            "OS-SRV-USG:launched_at": "2016-06-16T01:50:53.000000",
            "OS-SRV-USG:terminated_at": null,
            "accessIPv4": "",
            "accessIPv6": "",
            "addresses": {
                "paasnet": [
                    {
                        "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:fa:d1:78",
                        "OS-EXT-IPS:type": "fixed",
                        "addr": "192.168.110.13",
                        "version": 4
                    },
                    {
                        "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:fa:d1:78",
                        "OS-EXT-IPS:type": "floating",
                        "addr": "192.168.44.44",
                        "version": 4
                    }
                ]
            },
            "config_drive": "",
            "created": "2016-06-16T01:50:46Z",
            "flavor": {
                "id": "1",
                "links": [
                    {
                        "href": "http://test.iaas.url:8774/30722ef3cedf4df6bc81d63b9ccdd8ee/flavors/1",
                        "rel": "bookmark"
                    }
                ]
            },
            "hostId": "89035500f155609a0c41a7d5537f66d6406e3abf539aa31fe09baac4",
            "id": "7ba6714c-ff1b-43b3-b061-178fe576b7d4",
            "image": {
                "id": "59289c0a-af73-4d48-b112-01db517e1096",
                "links": [
                    {
                        "href": "http://test.iaas.url:8774/30722ef3cedf4df6bc81d63b9ccdd8ee/images/59289c0a-af73-4d48-b112-01db517e1096",
                        "rel": "bookmark"
                    }
                ]
            },
            "key_name": "testkey",
            "links": [
                {
                    "href": "http://test.iaas.url:8774/v2/30722ef3cedf4df6bc81d63b9ccdd8ee/servers/7ba6714c-ff1b-43b3-b061-178fe576b7d4",
                    "rel": "self"
                },
                {
                    "href": "http://test.iaas.url:8774/30722ef3cedf4df6bc81d63b9ccdd8ee/servers/7ba6714c-ff1b-43b3-b061-178fe576b7d4",
                    "rel": "bookmark"
                }
            ],
            "metadata": {},
            "name": "testnet2",
            "os-extended-volumes:volumes_attached": [],
            "progress": 0,
            "security_groups": [
                {
                    "name": "default"
                }
            ],
            "status": "ACTIVE",
            "tenant_id": "30722ef3cedf4df6bc81d63b9ccdd8ee",
            "updated": "2016-06-16T01:50:53Z",
            "user_id": "f2813e91cff641ffb211e4c50d522f29"
        },
............
    ]
}


curl -H \
"X-Auth-Token:gAAAAABXYgWPsljqTUt-W2tZzg38r_NplrE1GJBTy8timIU_ZsgazOzeek4BVYPPt6f4JPAuOotBbocT3a1WcxPS1qiqvdPy0eMO8FP-fYJtOsFxEa4Q5m-dvftWrLelY4DFg5LFXymXeUeP7bjhFD8Q9eWo8tmBtE7QSuG2cTpc
L6c-VIHWXuo" \
https://test.iaas.url:8774/v2/30722ef3cedf4df6bc81d63b9ccdd8ee/os-hosts \
| python -m json.tool 


    "hosts": [
        {
            "host_name": "node-10.domain.tld",
            "service": "cert",
            "zone": "internal"
        },
        {
            "host_name": "node-10.domain.tld",
            "service": "consoleauth",
            "zone": "internal"
        },
......
        {
            "host_name": "node-11.domain.tld",
            "service": "compute",
            "zone": "nova"
        }
    ]
}


curl -H \
"X-Auth-Token:gAAAAABXYgWPsljqTUt-W2tZzg38r_NplrE1GJBTy8timIU_ZsgazOzeek4BVYPPt6f4JPAuOotBbocT3a1WcxPS1qiqvdPy0eMO8FP-fYJtOsFxEa4Q5m-dvftWrLelY4DFg5LFXymXeUeP7bjhFD8Q9eWo8tmBtE7QSuG2cTpc L6c-VIHWXuo" \
https://test.iaas.url:8774/v2/30722ef3cedf4df6bc81d63b9ccdd8ee/os-hypervisors \
| python -m json.tool


    "hypervisors": [
        {
            "hypervisor_hostname": "node-11.domain.tld",
            "id": 3,
            "state": "up",
            "status": "enabled"
        }
    ]
}


curl -H "X-Auth-Token:gAAAAABXYgWPsljqTUt-W2tZzg38r_NplrE1GJBTy8timIU_ZsgazOzeek4BVYPPt6f4JPAuOotBbocT3a1WcxPS1qiqvdPy0eMO8FP-fYJtOsFxEa4Q5m-dvftWrLelY4DFg5LFXymXeUeP7bjhFD8Q9eWo8tmBtE7QSuG2cTpc
L6c-VIHWXuo" https://test.iaas.url:8774/v2/30722ef3cedf4df6bc81d63b9ccdd8ee/os-hypervisors/detail | python -m json.tool

    "hypervisors": [
        {
            "cpu_info": "{\"vendor\": \"Intel\", \"model\": \"Nehalem\", \"arch\": \"x86_64\", \"features\": [\"pge\", \"clflush\", \"sep\", \"syscall\", \"vme\", \"dtes64\", \"msr\", \"vmx\", \"xtpr\", \"cmov\
", \"pbe\", \"est\", \"pat\", \"monitor\", \"lm\", \"tsc\", \"nx\", \"fxsr\", \"tm\", \"sse4.1\", \"pae\", \"sse4.2\", \"acpi\", \"mmx\", \"cx8\", \"mce\", \"de\", \"tm2\", \"ht\", \"dca\", \"lahf_lm\", \"pdcm\", \"mca\", \"apic\", \"sse\", \"pse\", \"ds\", \"invtsc\", \"pni\", \"rdtscp\", \"sse2\", \"ss\", \"ds_cpl\", \"ssse3\", \"fpu\", \"cx16\", \"pse36\", \"mtrr\", \"popcnt\"], \"topology\": {\"cores\": 4, \"cells\": 1, \"threads\": 1, \"sockets\": 1}}",            "current_workload": 0,
            "disk_available_least": 3636,
            "free_disk_gb": 3637,
            "free_ram_mb": 18980,
            "host_ip": "192.168.42.105",
            "hypervisor_hostname": "node-11.domain.tld",
            "hypervisor_type": "QEMU",
            "hypervisor_version": 2000000,
            "id": 3,
            "local_gb": 3660,
            "local_gb_used": 23,
            "memory_mb": 24100,
            "memory_mb_used": 5120,
            "running_vms": 4,
            "service": {
                "disabled_reason": null,
                "host": "node-11.domain.tld",
                "id": 33
            },
            "state": "up",
            "status": "enabled",
            "vcpus": 4,
            "vcpus_used": 4
        }
    ]
}


curl -H "X-Auth-Token:gAAAAABXYgWPsljqTUt-W2tZzg38r_NplrE1GJBTy8timIU_ZsgazOzeek4BVYPPt6f4JPAuOotBbocT3a1WcxPS1qiqvdPy0eMO8FP-fYJtOsFxEa4Q5m-dvftWrLelY4DFg5LFXymXeUeP7bjhFD8Q9eWo8tmBtE7QSuG2cTpc
L6c-VIHWXuo" https://test.iaas.url:8774/v2/30722ef3cedf4df6bc81d63b9ccdd8ee/os-hypervisors/node-11.domain.tld/servers | python -m json.tool  

    "hypervisors": [
        {
            "hypervisor_hostname": "node-11.domain.tld",
            "id": 3,
            "servers": [
                {
                    "name": "instance-00000003",
                    "uuid": "2738ec98-8bcd-4141-b9ed-4de1fbe1608c"
                },
                {
                    "name": "instance-0000000f",
                    "uuid": "8b16dcc9-5f61-41a3-8738-ce5ac83a116d"
                },
                {
                    "name": "instance-00000012",
                    "uuid": "742f1904-4385-4d33-bed3-2e99ab991fc1"
                },
                {
                    "name": "instance-00000015",
                    "uuid": "7ba6714c-ff1b-43b3-b061-178fe576b7d4"
                }
            ],
            "state": "up",
            "status": "enabled"
        }
    ]
}


二、API
http://developer.openstack.org/api-ref/compute/?expanded=list-hypervisor-servers-detail#
-------------------------------------------------------------
import urllib2
import urllib
import json
import socket

def http_get():
    url = "https://iaas.url:5000"
    return urllib.urlopen(url).read()

def request_token():
    try:
        url = "https://iaas.url:5000" + "/v2.0/tokens"
        # POST data.
        data = json.dumps({"auth": {"tenantName": "admin", "passwordCredentials": {"username": "admin", "password":  "admin"}}})
        header = {"Content-Type": "application/json"}
        #enable cookie
        request = urllib2.Request(url,data,header)
        response = urllib2.urlopen(request)
        return json.loads(response.read())["access"]["token"]["id"]
    except Exception, e:
        print "ERROR:",e

def request_servers_detail():
    token = request_token()
    #print token
    url = "https://iaas.url:8774/v2/c03abe8abf0649c1832af6ca6e8a5544/servers/detail"
    request = urllib2.Request(url)
    request.add_header('X-Auth-Token', token)
    response = urllib2.urlopen(request)
    return json.loads(response.read())["servers"]

if __name__ == "__main__":
    #print http_get()
    #print request_token()

    servers = request_servers_detail()
    for i in servers:
        print '\ninstance_name:',i["OS-EXT-SRV-ATTR:instance_name"]
        print 'host_name:',i["OS-EXT-SRV-ATTR:hypervisor_hostname"]
        print 'host_ip:',socket.gethostbyname(i["OS-EXT-SRV-ATTR:hypervisor_hostname"])
        #print i["addresses"].values()[0]
        for j in i["addresses"].values()[0]:
            print j["OS-EXT-IPS:type"],':',j['addr']

----------------------------------------------------------------------------------

三、SDK
http://developer.openstack.org/sdks/python/openstacksdk/users/index.html
import json
import socket
from openstack import connection

auth_args = {
    'auth_url': "https://iaas.url:5000/v2.0",
    'project_name': 'admin',
    'username': 'admin',
    'password': 'admin',
}

conn = connection.Connection(verify = False,**auth_args)

#print conn.authorize()  # token.
#print dir(conn)

#for i in conn.image.images():
#    print i

#print dir(conn.compute)
#for i in conn.compute.hypervisors():
#    print '\n',i

for i in conn.compute.servers():
#    print '\n',dir(i)

    print "\navailability_zone:", i['OS-EXT-AZ:availability_zone']
    print "hypervisor_hostname:", i['OS-EXT-SRV-ATTR:hypervisor_hostname']
    print "hypervisor_host_IP:", socket.gethostbyname(i['OS-EXT-SRV-ATTR:hypervisor_hostname'])
    print "instance_name:", i['OS-EXT-SRV-ATTR:instance_name']
    print "power_state:", i['OS-EXT-STS:power_state']
    print "vm_state:", i["OS-EXT-STS:vm_state"]
    print "tenamt_id:", i.tenant_id
    print "user_id:", i.user_id
    print "name:", i.name
    print "hostId:", i.hostId
    print "id:", i.id

    for j in i.addresses.values()[0]:
            print j["OS-EXT-IPS:type"],':',j['addr']


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐