研发环境

  • 系统:CentOS Linux release 7.7.1908 (Core)
  • OpenStack:Train版
  • Python:2.7.5
Python库 版本
keystoneauth1 4.0.1
openstacksdk 0.45.0
oslo.config 7.0.0
oslo.i18n 3.25.1
oslo.serialization 2.29.2
oslo.utils 3.42.1
python-cinderclient 6.0.0
python-keystoneclient 3.22.0
python-novaclient 16.0.0
python-openstackclient 5.2.1

源码

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from keystoneauth1 import loading
from keystoneauth1 import session
from novaclient import client as nova_client

NOVA_API_VERSION = "2.1"

# 从keystone处获取认证信息
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url="http://[2016::145]:35357/v3",
username="admin",
password="Inspur@123",
project_name="admin",
project_domain_id="default",
user_domain_id="default")
sess = session.Session(auth=auth)

# 使用认证信息,获取novaclient
nova = nova_client.Client(NOVA_API_VERSION, session=sess)

# 调用novaclient,获取nova相关信息
servers = nova.servers.list()

for ser in servers:
print(ser.id)
print(ser.name)
print(ser.image)
print(ser.flavor)
print(ser.key_name)
print(ser.status)
print(ser.created)
print(ser.addresses)
1
2
3
4
5
6
7
8
9
10
[nova]
endpoint_type = internal
auth_url = http://111.111.111.80:35357
auth_type = password
project_domain_id = default
user_domain_id = default
region_name = RegionOne
project_name = service
username = nova
password = 8rMN88CaCkvNyvx5b9YrXYwkPDEgKTE7Kb5xfWnL
1
2
3
4
5
6
7
8
9
10
[nova]
auth_url = http://111.111.111.80:35357
auth_type = password
project_domain_id = default
user_domain_id = default
region_name = RegionOne
project_name = admin
username = admin
password = admin
endpoint_type = internal

结构体

nova.servers.list()

item结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{
"OS-EXT-STS:task_state": null,
"addresses": {
"demo-net": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:8b:c8:00",
"version": 4,
"addr": "10.0.0.221",
"OS-EXT-IPS:type": "fixed"
}
]
},
"links": [
{
"href": "http://111.111.111.80:8774/v2.1/servers/077c1c6d-5390-4856-89dc-7072c5d5d5d4",
"rel": "self"
},
{
"href": "http://111.111.111.80:8774/servers/077c1c6d-5390-4856-89dc-7072c5d5d5d4",
"rel": "bookmark"
}
],
"locked_reason": null,
"image": {
"id": "3086f549-23e2-4c0d-b791-b17998952d68",
"links": [
{
"href": "http://111.111.111.80:8774/images/3086f549-23e2-4c0d-b791-b17998952d68",
"rel": "bookmark"
}
]
},
"OS-EXT-SRV-ATTR:user_data": null,
"OS-EXT-STS:vm_state": "active",
"OS-EXT-SRV-ATTR:instance_name": "instance-00000001",
"OS-EXT-SRV-ATTR:root_device_name": "/dev/vda",
"OS-SRV-USG:launched_at": "2020-12-08T17:41:17.000000",
"flavor": {
"ephemeral": 0,
"ram": 512,
"original_name": "m1.tiny",
"vcpus": 1,
"extra_specs": {},
"swap": 0,
"disk": 1
},
"id": "077c1c6d-5390-4856-89dc-7072c5d5d5d4",
"security_groups": [
{
"name": "default"
}
],
"description": "demo",
"host_status": "UP",
"user_id": "439ca237905e417593165391458960d6",
"OS-EXT-SRV-ATTR:hostname": "demo",
"OS-DCF:diskConfig": "MANUAL",
"accessIPv4": "",
"accessIPv6": "",
"OS-EXT-SRV-ATTR:reservation_id": "r-56k4gluj",
"progress": 0,
"OS-EXT-STS:power_state": 1,
"OS-EXT-AZ:availability_zone": "nova",
"config_drive": "",
"status": "ACTIVE",
"OS-EXT-SRV-ATTR:ramdisk_id": "",
"updated": "2020-12-08T17:41:18Z",
"hostId": "cd9cf8191851aeae24077dd7d3ca4c6c01d1b5d0ff7be81de482259b",
"OS-EXT-SRV-ATTR:host": "node02",
"OS-SRV-USG:terminated_at": null,
"tags": [],
"key_name": "mykey",
"OS-EXT-SRV-ATTR:kernel_id": "",
"locked": false,
"OS-EXT-SRV-ATTR:hypervisor_hostname": "node02",
"name": "demo",
"OS-EXT-SRV-ATTR:launch_index": 0,
"created": "2020-12-08T17:40:57Z",
"tenant_id": "1249bd160bab49e89c4c1d795ec68746",
"os-extended-volumes:volumes_attached": [],
"trusted_image_certificates": null,
"metadata": {}
}

参考文档