From 7b59b7759bc2faec8eb6d18d44f0ebc9d737aef6 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 29 Apr 2016 16:25:14 -0400 Subject: [PATCH] Briefly cache adapter data It is frequent that a client may hammer request for firmware inventory. To facilitate this use case, store and reuse data for 30 seconds, so long as the ipmi session has not changed. Change-Id: Ia356e3161a2ccf1c717eec09696e265263d8255b --- pyghmi/ipmi/oem/lenovo/imm.py | 37 +++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/pyghmi/ipmi/oem/lenovo/imm.py b/pyghmi/ipmi/oem/lenovo/imm.py index ced947d3..98c5ebfe 100644 --- a/pyghmi/ipmi/oem/lenovo/imm.py +++ b/pyghmi/ipmi/oem/lenovo/imm.py @@ -16,6 +16,7 @@ from datetime import datetime import json +from pyghmi.ipmi.private.session import _monotonic_time import pyghmi.util.webclient as webclient import urllib @@ -86,11 +87,28 @@ def fetch_grouped_properties(ipmicmd, groupinfo): return retdata -def fetch_adapter_firmware(wc): - wc.request('GET', '/designs/imm/dataproviders/imm_adapters.php') - rsp = wc.getresponse() - if rsp.status == 200: - adapterdata = json.loads(rsp.read()) +def fetch_adapter_firmware(ipmicmd, certverify): + adapterdata = None + try: + vintage = ipmicmd.ipmi_session.lenovo_cached_adapters[1] + if vintage > _monotonic_time() - 30: + adapterdata = ipmicmd.ipmi_session.lenovo_cached_adapters[0] + except AttributeError: + pass + if not adapterdata: + wc = get_imm_webclient(ipmicmd.bmc, certverify, + ipmicmd.ipmi_session.userid, + ipmicmd.ipmi_session.password) + if not wc: + return + wc.request('GET', '/designs/imm/dataproviders/imm_adapters.php') + rsp = wc.getresponse() + if rsp.status == 200: + adapterdata = json.loads(rsp.read()) + ipmicmd.ipmi_session.lenovo_cached_adapters = (adapterdata, + _monotonic_time()) + wc.request('GET', '/data/logout') + if adapterdata: for adata in adapterdata['items']: aname = adata['adapter.adapterName'] donenames = set([]) @@ -148,10 +166,5 @@ def get_firmware_inventory(ipmicmd, bmcver, certverify): 'build': '/v2/bios/pending_build_id'}) if bdata: yield ('UEFI Pending Update', bdata) - wc = get_imm_webclient(ipmicmd.bmc, certverify, - ipmicmd.ipmi_session.userid, - ipmicmd.ipmi_session.password) - if wc: - for firm in fetch_adapter_firmware(wc): - yield firm - wc.request('GET', '/data/logout') + for firm in fetch_adapter_firmware(ipmicmd, certverify): + yield firm