0

如何获取openstacksdk返回的openstack.compute.v2.server.ServerDetail数据?

下面是openstack.compute.v2.server.ServerDetail实例数据:

[openstack.compute.v2.server.ServerDetail(
OS-EXT-AZ:availability_zone=, 
key_name=None, 
hostId=, 
os-extended-volumes:volumes_attached=[], 
OS-SRV-USG:launched_at=None, 
OS-EXT-STS:vm_state=error, 
flavor={'id': '5c5dca53-9f96-4851-afd4-60de75faf896', 'links': [{'href': 'http://controller:8774/233cf23186bf4c52afc464ee008cdf7f/flavors/5c5dca53-9f96-4851-afd4-60de75faf896', 'rel': 'bookmark'}]}, updated=2017-11-27T10:29:50Z, 
accessIPv4=, 
image={'id': '60f4005e-5daf-4aef-a018-4c6b2ff06b40', 'links': [{'href': 'http://controller:8774/233cf23186bf4c52afc464ee008cdf7f/images/60f4005e-5daf-4aef-a018-4c6b2ff06b40', 'rel': 'bookmark'}]}, 
created=2017-11-27T10:29:49Z, 
metadata={}, 
links=[{'href': 'http://controller:8774/v2.1/233cf23186bf4c52afc464ee008cdf7f/servers/3db46b7b-a641-49ce-97ef-f17c9a11f58a', 'rel': 'self'}, {'href': 'http://controller:8774/233cf23186bf4c52afc464ee008cdf7f/servers/3db46b7b-a641-49ce-97ef-f17c9a11f58a', 'rel': 'bookmark'}], 
OS-DCF:diskConfig=MANUAL, 
id=3db46b7b-a641-49ce-97ef-f17c9a11f58a, user_id=41bb48ee30e449d5868f7af9e6251156, 
OS-SRV-USG:terminated_at=None, 
name=123456, 
config_drive=, 
accessIPv6=, 
OS-EXT-STS:power_state=0, 
addresses={}, 
OS-EXT-STS:task_state=None, 
status=ERROR, 
tenant_id=233cf23186bf4c52afc464ee008cdf7f),]

你看,我可以instance.property用来获取值,下面server_generator是 openstack 服务器的生成器:

for sd in server_generator:
    print (sd.addresses)

但是,有两点我无法处理:

  1. 如何处理OS-EXT-STS:power_statekey或os-extended-volumes:volumes_attachedkey?

  2. 我用sd.hostId的时候会报错,说没有hostId。

4

1 回答 1

1

你必须首先使用to_dict函数来获取dict objcet。然后你可以得到你想要的。

server_dict = server.to_dict()
## get all keys 
print server_dict.keys()
## get volumes_attached 
print server_dict['volumes_attached']
于 2018-03-09T04:34:22.480 回答