How to get the openstacksdk returned `openstack.compute.v2.server.ServerDetail` data?

676 views Asked by At

How to get the openstacksdk returned openstack.compute.v2.server.ServerDetail data?

The bellow is the openstack.compute.v2.server.ServerDetail instance data:

[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),]

You see, I can use instance.property to get the value, bellow server_generator is the generator of openstack servers:

for sd in server_generator:
    print (sd.addresses)

But, however there are two point I can not deal with:

  1. how to deal with OS-EXT-STS:power_state key or os-extended-volumes:volumes_attached key?

  2. when I use sd.hostId there will report error, says there is no hostId.

1

There are 1 answers

0
chou jayway On

you must first use to_dict function to get dict objcet.then you can get what you want.

server_dict = server.to_dict()
## get all keys 
print server_dict.keys()
## get volumes_attached 
print server_dict['volumes_attached']