tgcloud tgcloud - 2 months ago 27
JSON Question

Openstack output in json format

I am using novaclient to get the details from Openstack. i am able to retrieve the information, however i want to convert it into json format. i am using "to_dic()" but it;s throwing "Attribute" error on "server_details = server_id_name.to_dict()", not sure why.

AttributeError: "'tuple' object has no attribute 'to_dict'"


Code is given below,

from novaclient import client as novaclient
import json

nova = novaclient.Client(version='2.0',username='xxxx',api_key='xxxx',project_id='xxxx',auth_url='http://192.168.12.3:5000/v2.0/',insecure='True')

server_details = dict()
server = nova.servers.list()
for server in nova.servers.list():
print server.id, server.name
server_id_name = server.id, server.name
server_details = server_id_name.to_dict()
for network in server.networks.items():
print network

Answer

It's a error in your python code. you should use

server_details[server.id] = server.name

to replace your code

server_id_name = server.id, server.name
server_details = server_id_name.to_dict()

BTW, you should learn more about python. If you want to change a tuple to a dict, you should see python-tuple-to-dict