Reputation: 382
I have a banch of servers and I use API to extract data about storage. To get details about storage uuid (named storage in json) I use code as below:
srvdet = requests.get('https://hhh.com/1.2/server/76856585', auth=HTTPBasicAuth('login', 'pass'))
srvdet_json = srvdet.json()
datas = srvdet.json()
print(datas)
And result in json:
{
"server": {
"boot_order": "",
"core_number": "2",
"created": ,
"firewall": "off",
"host": ,
"hostname": "hello",
"ip_addresses": {
"ip_address": [
{
"access": "private",
"address": "",
"family": ""
},
{
"access": "",
"address": "",
"family": "",
"part_of_plan": ""
}
]
},
"license": 0,
"memory_amount": "",
"nic_model": "",
"plan": "",
"plan_ipv4_bytes": "",
"plan_ipv6_bytes": "0",
"state": "started",
"storage_devices": {
"storage_device": [
{
"address": "",
"boot_disk": "0",
"part_of_plan": "",
"storage": "09845",
"storage_size": ,
"storage_title": "",
"type": ""
}
For now it works perfectly fine. The problem is when I need to get "09845" which is value for storage. When I try to use this code:
for storage in datas['server']['storage_devices']['storage_device']:
srvstorage = storage
print(srvstorage)
The result is:
{'storage': '09845', 'storage_size':, 'address': '', 'boot_disk': '0', 'type': '', 'part_of_plan': 'yes', 'storage_title': ''}
What am I doing wrong? How to save "09845" in variable?
Now I get error when trying to access details about storage. I want to extract backup status which is state in json:
{
"storage": {
"access": "private",
"backup_rule": {},
"backups": {
"backup": []
},
"license": 0,
"part_of_plan": "",
"servers": {
"server": [
""
]
},
"size": ,
"state": "online",
"tier": "",
"title": "",
"type": "",
"uuid": "",
"zone": ""
}
}
When I execute this code:
bkpdet = requests.get('https://fffff.com/1.2/storage/08475', auth=HTTPBasicAuth('login', 'pass'))
bkpdet_json = bkpdet.json()
datastg = bkpdet.json()
print(datastg)
for sts in datastg['storage']:
bkpsts = sts['state']
print(bkpsts)
I get this error:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: string indices must be integers
The whole idea is to at the end get info about status using this code:
if bkpsts == "online":
print('Backup has been created.')
else bkpsts == "backuping":
print('Backup creation is in progress.')
else:
print(bkpdet.status_code)
I was searching but still can't find what is wrong here.
Upvotes: 1
Views: 155
Reputation: 2569
You miss a key when you want to access storage, you loop over it and it's fine. But in each iteration you get a dictionary from which you need to call the right key, in your case storage
.
for storage in datas['server']['storage_devices']['storage_device']:
srvstorage = storage.get("storage", None)
print(srvstorage)
get
method will be preferable to use as you may encounter a device with out storage information inside, by using get you can avoid the KeyError
.
Upvotes: 1
Reputation: 1747
You can just do
for device in datas['server']['storage_devices']['storage_device']:
device_storage = device['storage']
print(device_storage)
Upvotes: 1