1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Provide flexibility when retrieving facts (#46148)

* Provide flexibility when retrieving facts

* Check if keys exist before trying to read
This commit is contained in:
Jose Delarosa 2018-10-11 13:17:13 -05:00 committed by ansibot
parent 0bc5b799a6
commit 81640a2c67

View file

@ -268,8 +268,10 @@ class RedfishUtils(object):
def get_storage_controller_inventory(self): def get_storage_controller_inventory(self):
result = {} result = {}
controllers_details = []
controller_list = [] controller_list = []
controller_results = []
# Get these entries, but does not fail if not found
properties = ['Name', 'Status']
# Find Storage service # Find Storage service
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
@ -292,21 +294,26 @@ class RedfishUtils(object):
controller_list.append(controller[u'@odata.id']) controller_list.append(controller[u'@odata.id'])
for c in controller_list: for c in controller_list:
controller = {}
uri = self.root_uri + c uri = self.root_uri + c
response = self.get_request(uri) response = self.get_request(uri)
if response['ret'] is False: if response['ret'] is False:
return response return response
data = response['data'] data = response['data']
controllers_details.append(dict(
Name=data[u'Name'], for property in properties:
Health=data[u'Status'][u'Health'])) if property in data:
result["entries"] = controllers_details controller[property] = data[property]
controller_results.append(controller)
result["entries"] = controller_results
return result return result
def get_disk_inventory(self): def get_disk_inventory(self):
result = {} result = {}
disks_details = []
controller_list = [] controller_list = []
disk_results = []
# Get these entries, but does not fail if not found
properties = ['Name', 'Manufacturer', 'Model', 'Status', 'CapacityBytes']
# Find Storage service # Find Storage service
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
@ -336,14 +343,12 @@ class RedfishUtils(object):
data = response['data'] data = response['data']
for device in data[u'Devices']: for device in data[u'Devices']:
disks_details.append(dict( disk = {}
Controller=data[u'Name'], for property in properties:
Name=device[u'Name'], if property in device:
Manufacturer=device[u'Manufacturer'], disk[property] = device[property]
Model=device[u'Model'], disk_results.append(disk)
State=device[u'Status'][u'State'], result["entries"] = disk_results
Health=device[u'Status'][u'Health']))
result["entries"] = disks_details
return result return result
def restart_manager_gracefully(self): def restart_manager_gracefully(self):
@ -397,8 +402,11 @@ class RedfishUtils(object):
def list_users(self): def list_users(self):
result = {} result = {}
# listing all users has always been slower than other operations, why? # listing all users has always been slower than other operations, why?
allusers = [] user_list = []
allusers_details = [] users_results = []
# Get these entries, but does not fail if not found
properties = ['Id', 'Name', 'UserName', 'RoleId', 'Locked', 'Enabled']
response = self.get_request(self.root_uri + self.accounts_uri) response = self.get_request(self.root_uri + self.accounts_uri)
if response['ret'] is False: if response['ret'] is False:
return response return response
@ -406,22 +414,22 @@ class RedfishUtils(object):
data = response['data'] data = response['data']
for users in data[u'Members']: for users in data[u'Members']:
allusers.append(users[u'@odata.id']) # allusers[] are URIs user_list.append(users[u'@odata.id']) # user_list[] are URIs
# for each user, get details # for each user, get details
for uri in allusers: for uri in user_list:
user = {}
response = self.get_request(self.root_uri + uri) response = self.get_request(self.root_uri + uri)
if response['ret'] is False: if response['ret'] is False:
return response return response
data = response['data'] data = response['data']
if not data[u'UserName'] == "": # only care if name is not empty for property in properties:
allusers_details.append(dict( if property in data:
Id=data[u'Id'], user[property] = data[property]
Name=data[u'Name'],
UserName=data[u'UserName'], users_results.append(user)
RoleId=data[u'RoleId'])) result["entries"] = users_results
result["entries"] = allusers_details
return result return result
def add_user(self, user): def add_user(self, user):
@ -503,14 +511,18 @@ class RedfishUtils(object):
def get_manager_attributes(self): def get_manager_attributes(self):
result = {} result = {}
manager_attributes = {} manager_attributes = {}
attributes_id = "Attributes" key = "Attributes"
response = self.get_request(self.root_uri + self.manager_uri + "/" + attributes_id) response = self.get_request(self.root_uri + self.manager_uri + "/" + key)
if response['ret'] is False: if response['ret'] is False:
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
for attribute in data[u'Attributes'].items():
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
for attribute in data[key].items():
manager_attributes[attribute[0]] = attribute[1] manager_attributes[attribute[0]] = attribute[1]
result["entries"] = manager_attributes result["entries"] = manager_attributes
return result return result
@ -526,6 +538,10 @@ class RedfishUtils(object):
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
bios_uri = data[key]["@odata.id"] bios_uri = data[key]["@odata.id"]
response = self.get_request(self.root_uri + bios_uri) response = self.get_request(self.root_uri + bios_uri)
@ -544,6 +560,8 @@ class RedfishUtils(object):
boot_device_details = [] boot_device_details = []
key = "Bios" key = "Bios"
bootsources = "BootSources" bootsources = "BootSources"
# Get these entries, but does not fail if not found
properties = ['Index', 'Id', 'Name', 'Enabled']
# Search for 'key' entry and extract URI from it # Search for 'key' entry and extract URI from it
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
@ -551,6 +569,10 @@ class RedfishUtils(object):
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
bios_uri = data[key]["@odata.id"] bios_uri = data[key]["@odata.id"]
# Get boot mode first as it will determine what attribute to read # Get boot mode first as it will determine what attribute to read
@ -573,9 +595,9 @@ class RedfishUtils(object):
boot_device_list = data[u'Attributes'][boot_seq] boot_device_list = data[u'Attributes'][boot_seq]
for b in boot_device_list: for b in boot_device_list:
boot_device = {} boot_device = {}
boot_device["Index"] = b[u'Index'] for property in properties:
boot_device["Name"] = b[u'Name'] if property in b:
boot_device["Enabled"] = b[u'Enabled'] boot_device[property] = b[property]
boot_device_details.append(boot_device) boot_device_details.append(boot_device)
result["entries"] = boot_device_details result["entries"] = boot_device_details
return result return result
@ -590,6 +612,10 @@ class RedfishUtils(object):
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
bios_uri = data[key]["@odata.id"] bios_uri = data[key]["@odata.id"]
# Extract proper URI # Extract proper URI
@ -615,6 +641,10 @@ class RedfishUtils(object):
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
bios_uri = data[key]["@odata.id"] bios_uri = data[key]["@odata.id"]
response = self.get_request(self.root_uri + bios_uri) response = self.get_request(self.root_uri + bios_uri)
@ -659,6 +689,10 @@ class RedfishUtils(object):
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
bios_uri = data[key]["@odata.id"] bios_uri = data[key]["@odata.id"]
# Extract proper URI # Extract proper URI
@ -688,6 +722,10 @@ class RedfishUtils(object):
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
bios_uri = data[key]["@odata.id"] bios_uri = data[key]["@odata.id"]
# Extract proper URI # Extract proper URI
@ -710,11 +748,14 @@ class RedfishUtils(object):
def get_fan_inventory(self): def get_fan_inventory(self):
result = {} result = {}
fan_details = [] fan_results = []
key = "Thermal" key = "Thermal"
# Get these entries, but does not fail if not found
properties = ['FanName', 'Reading', 'Status']
# Go through list # Go through list
for chassis_uri in self.chassis_uri_list: for chassis_uri in self.chassis_uri_list:
fan = {}
response = self.get_request(self.root_uri + chassis_uri) response = self.get_request(self.root_uri + chassis_uri)
if response['ret'] is False: if response['ret'] is False:
return response return response
@ -730,20 +771,22 @@ class RedfishUtils(object):
data = response['data'] data = response['data']
for device in data[u'Fans']: for device in data[u'Fans']:
fan_details.append(dict( for property in properties:
# There is more information available but this is most important if property in device:
Name=device[u'FanName'], fan[property] = device[property]
RPMs=device[u'Reading'],
State=device[u'Status'][u'State'], fan_results.append(fan)
Health=device[u'Status'][u'Health'])) result["entries"] = fan_results
result["entries"] = fan_details
return result return result
def get_cpu_inventory(self): def get_cpu_inventory(self):
result = {} result = {}
cpu_details = []
cpu_list = [] cpu_list = []
cpu_results = []
key = "Processors" key = "Processors"
# Get these entries, but does not fail if not found
properties = ['Id', 'Manufacturer', 'Model', 'MaxSpeedMHz', 'TotalCores',
'TotalThreads', 'Status']
# Search for 'key' entry and extract URI from it # Search for 'key' entry and extract URI from it
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
@ -752,6 +795,9 @@ class RedfishUtils(object):
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
processors_uri = data[key]["@odata.id"] processors_uri = data[key]["@odata.id"]
# Get a list of all CPUs and build respective URIs # Get a list of all CPUs and build respective URIs
@ -763,29 +809,32 @@ class RedfishUtils(object):
for cpu in data[u'Members']: for cpu in data[u'Members']:
cpu_list.append(cpu[u'@odata.id']) cpu_list.append(cpu[u'@odata.id'])
for c in cpu_list: for c in cpu_list:
cpu = {}
uri = self.root_uri + c uri = self.root_uri + c
response = self.get_request(uri) response = self.get_request(uri)
if response['ret'] is False: if response['ret'] is False:
return response return response
data = response['data'] data = response['data']
cpu_details.append(dict(
Name=data[u'Id'], for property in properties:
Manufacturer=data[u'Manufacturer'], if property in data:
Model=data[u'Model'], cpu[property] = data[property]
MaxSpeedMHz=data[u'MaxSpeedMHz'],
TotalCores=data[u'TotalCores'], cpu_results.append(cpu)
TotalThreads=data[u'TotalThreads'], result["entries"] = cpu_results
State=data[u'Status'][u'State'],
Health=data[u'Status'][u'Health']))
result["entries"] = cpu_details
return result return result
def get_nic_inventory(self): def get_nic_inventory(self):
result = {} result = {}
nic_details = []
nic_list = [] nic_list = []
nic_results = []
key = "EthernetInterfaces" key = "EthernetInterfaces"
# Get these entries, but does not fail if not found
properties = ['Description', 'FQDN', 'IPv4Addresses', 'IPv6Addresses',
'NameServers', 'PermanentMACAddress', 'SpeedMbps', 'MTUSize',
'AutoNeg', 'Status']
# Search for 'key' entry and extract URI from it # Search for 'key' entry and extract URI from it
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
@ -794,6 +843,9 @@ class RedfishUtils(object):
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if key not in data:
return {'ret': False, 'msg': "Key %s not found" % key}
ethernetinterfaces_uri = data[key]["@odata.id"] ethernetinterfaces_uri = data[key]["@odata.id"]
# Get a list of all network controllers and build respective URIs # Get a list of all network controllers and build respective URIs
@ -814,32 +866,23 @@ class RedfishUtils(object):
return response return response
data = response['data'] data = response['data']
nic['Name'] = data[u'Name'] for property in properties:
nic['FQDN'] = data[u'FQDN'] if property in data:
for d in data[u'IPv4Addresses']: nic[property] = data[property]
nic['IPv4'] = d[u'Address']
if 'GateWay' in d: # not always available nic_results.append(nic)
nic['Gateway'] = d[u'GateWay'] result["entries"] = nic_results
nic['SubnetMask'] = d[u'SubnetMask']
for d in data[u'IPv6Addresses']:
nic['IPv6'] = d[u'Address']
for d in data[u'NameServers']:
nic['NameServers'] = d
nic['MACAddress'] = data[u'PermanentMACAddress']
nic['SpeedMbps'] = data[u'SpeedMbps']
nic['MTU'] = data[u'MTUSize']
nic['AutoNeg'] = data[u'AutoNeg']
if 'Status' in data: # not available when power is off
nic['Health'] = data[u'Status'][u'Health']
nic['State'] = data[u'Status'][u'State']
nic_details.append(nic)
result["entries"] = nic_details
return result return result
def get_psu_inventory(self): def get_psu_inventory(self):
result = {} result = {}
psu_details = []
psu_list = [] psu_list = []
psu_results = []
key = "PoweredBy"
# Get these entries, but does not fail if not found
properties = ['Name', 'Model', 'SerialNumber', 'PartNumber', 'Manufacturer',
'FirmwareVersion', 'PowerCapacityWatts', 'PowerSupplyType',
'Status']
# Get a list of all PSUs and build respective URIs # Get a list of all PSUs and build respective URIs
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
@ -848,10 +891,16 @@ class RedfishUtils(object):
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
if 'Links' not in data:
return {'ret': False, 'msg': "Property not found"}
if key not in data[u'Links']:
return {'ret': False, 'msg': "Key %s not found" % key}
for psu in data[u'Links'][u'PoweredBy']: for psu in data[u'Links'][u'PoweredBy']:
psu_list.append(psu[u'@odata.id']) psu_list.append(psu[u'@odata.id'])
for p in psu_list: for p in psu_list:
psu = {}
uri = self.root_uri + p uri = self.root_uri + p
response = self.get_request(uri) response = self.get_request(uri)
if response['ret'] is False: if response['ret'] is False:
@ -860,65 +909,31 @@ class RedfishUtils(object):
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
psu = {} for property in properties:
psu['Name'] = data[u'Name'] if property in data:
psu['Model'] = data[u'Model'] psu[property] = data[property]
psu['SerialNumber'] = data[u'SerialNumber'] psu_results.append(psu)
psu['PartNumber'] = data[u'PartNumber'] result["entries"] = psu_results
if 'Manufacturer' in data: # not available in all generations
psu['Manufacturer'] = data[u'Manufacturer']
psu['FirmwareVersion'] = data[u'FirmwareVersion']
psu['PowerCapacityWatts'] = data[u'PowerCapacityWatts']
psu['PowerSupplyType'] = data[u'PowerSupplyType']
psu['Status'] = data[u'Status'][u'State']
psu['Health'] = data[u'Status'][u'Health']
psu_details.append(psu)
result["entries"] = psu_details
return result return result
def get_system_inventory(self): def get_system_inventory(self):
result = {} result = {}
inventory = {} inventory = {}
# Get these entries, but does not fail if not found
properties = ['Status', 'HostName', 'PowerState', 'Model', 'Manufacturer',
'PartNumber', 'SystemType', 'AssetTag', 'ServiceTag',
'SerialNumber', 'BiosVersion', 'MemorySummary',
'ProcessorSummary', 'TrustedModules']
response = self.get_request(self.root_uri + self.systems_uri) response = self.get_request(self.root_uri + self.systems_uri)
if response['ret'] is False: if response['ret'] is False:
return response return response
result['ret'] = True result['ret'] = True
data = response['data'] data = response['data']
# There could be more information to extract for property in properties:
inventory['Status'] = data[u'Status'][u'Health'] if property in data:
inventory['HostName'] = data[u'HostName'] inventory[property] = data[property]
inventory['PowerState'] = data[u'PowerState']
inventory['Model'] = data[u'Model']
inventory['Manufacturer'] = data[u'Manufacturer']
inventory['PartNumber'] = data[u'PartNumber']
inventory['SystemType'] = data[u'SystemType']
inventory['AssetTag'] = data[u'AssetTag']
inventory['ServiceTag'] = data[u'SKU']
inventory['SerialNumber'] = data[u'SerialNumber']
inventory['BiosVersion'] = data[u'BiosVersion']
inventory['MemoryTotal'] = data[u'MemorySummary'][u'TotalSystemMemoryGiB']
inventory['MemoryHealth'] = data[u'MemorySummary'][u'Status'][u'Health']
inventory['CpuCount'] = data[u'ProcessorSummary'][u'Count']
inventory['CpuModel'] = data[u'ProcessorSummary'][u'Model']
inventory['CpuHealth'] = data[u'ProcessorSummary'][u'Status'][u'Health']
datadict = data[u'Boot']
if 'BootSourceOverrideMode' in datadict.keys():
inventory['BootSourceOverrideMode'] = data[u'Boot'][u'BootSourceOverrideMode']
else:
# Not available in earlier server generations
inventory['BootSourceOverrideMode'] = "Not available"
if 'TrustedModules' in data:
for d in data[u'TrustedModules']:
if 'InterfaceType' in d.keys():
inventory['TPMInterfaceType'] = d[u'InterfaceType']
inventory['TPMStatus'] = d[u'Status'][u'State']
else:
# Not available in earlier server generations
inventory['TPMInterfaceType'] = "Not available"
inventory['TPMStatus'] = "Not available"
result["entries"] = inventory result["entries"] = inventory
return result return result