Repository: dmwm/wmcore
Issue: AgentStatusPoller failing to update agent config in reqmgr_aux db

**Impact of the bug**
WMAgent, the component doesn't crash but it keeps failing over and over

**Describe the bug**
Apparently when we call the update agent method to run an update in place, it treats a dictionary as if it was a list, doesn't failing to find the key 0.

**How to reproduce it**
none

**Expected behavior**
Make sure those `ReqMgrAux` APIs
```
getWMAgentConfig
getCampaignConfig
getTransferInfo
```
return the same data type. Then fix the update function accordingly.

**Additional context and error message**
Traceback as follows (with some additional debugging I had in place):
```
2019-12-30 09:34:24,914:139919094269696:ERROR:ReqMgrAux:AMR callName: wmagentconfig, resource: vocms0257.cern.ch, kwparams: {'AgentDrainMode': True}
2019-12-30 09:34:25,214:139919094269696:ERROR:ReqMgrAux:AMR thisDoc: {u'AgentDrainMode': False, u'SpeedDrainConfig': {u'NoJobRetries': {u'Threshold': 200, u'Enabled': False}, u'EnableAllSites': {u'Threshold': 200, u'Enabled': False}, u'CondorPriority': {u'Threshold': 500, u'Enabled': False}}, u'CondorOverflowFraction': 0.2, u'ConfigType': u'WMAGENT_CONFIG', u'SpeedDrainMode': False, u'CondorJobsFraction': 0.75, u'DiskUseThreshold': 85, u'UserDrainMode': False, u'MaxRetries': {u'default': 3, u'Merge': 4, u'Cleanup': 2, u'LogCollect': 1, u'Harvesting': 4}, u'NoRetryExitCodes': [70, 73, 8001, 8006, 8009, 8023, 8026, 8501, 50660, 50661, 50664, 71102], u'IgnoreDisks': [u'/mnt/ramdisk']}
2019-12-30 09:34:25,215:139919094269696:ERROR:AgentStatusPoller:Error occurred, will retry later.
Details: 0
Traceback (most recent call last):
  File "/data/srv/wmagent/v1.2.8/sw/slc7_amd64_gcc630/cms/wmagent/1.2.8/lib/python2.7/site-packages/WMComponent/AgentStatusWatcher/AgentStatusPoller.py", line 128, in algorithm
    agentInfo = self.collectAgentInfo()
  File "/data/srv/wmagent/v1.2.8/sw/slc7_amd64_gcc630/cms/wmagent/1.2.8/lib/python2.7/site-packages/WMComponent/AgentStatusWatcher/AgentStatusPoller.py", line 202, in collectAgentInfo
    agentInfo['disk_warning'] = listDiskUsageOverThreshold(self.config, updateDB=True)
  File "/data/srv/wmagent/v1.2.8/sw/slc7_amd64_gcc630/cms/wmagent/1.2.8/lib/python2.7/site-packages/WMCore/Services/ReqMgrAux/ReqMgrAux.py", line 386, in listDiskUsageOverThreshold
    inPlace=True)
  File "/data/srv/wmagent/v1.2.8/sw/slc7_amd64_gcc630/cms/wmagent/1.2.8/lib/python2.7/site-packages/WMCore/Services/ReqMgrAux/ReqMgrAux.py", line 146, in updateWMAgentConfig
    resp = self._updateRecords(api, agentName, content)
  File "/data/srv/wmagent/v1.2.8/sw/slc7_amd64_gcc630/cms/wmagent/1.2.8/lib/python2.7/site-packages/WMCore/Services/ReqMgrAux/ReqMgrAux.py", line 90, in _updateRecords
    thisDoc = apiMap[callName](resource)[0]
KeyError: 0
2019-12-30 09:32:12,245:140410281674496:INFO:BaseWorkerThread:AgentStatusPoller took 16.135 secs to execute
```

Changed files:
src/python/WMCore/Services/ReqMgrAux/ReqMgrAux.py

diff --git a/src/python/WMCore/Services/ReqMgrAux/ReqMgrAux.py b/src/python/WMCore/Services/ReqMgrAux/ReqMgrAux.py
index 9a16538a8..621c2eea9 100644
--- a/src/python/WMCore/Services/ReqMgrAux/ReqMgrAux.py
+++ b/src/python/WMCore/Services/ReqMgrAux/ReqMgrAux.py
@@ -86,7 +86,11 @@ class ReqMgrAux(Service):
                   'campaignconfig': self.getCampaignConfig,
                   'transferinfo': self.getTransferInfo}
 
-        thisDoc = apiMap[callName](resource)[0]
+        thisDoc = apiMap[callName](resource)
+        # getWMAgentConfig method returns directly the document, while the others
+        # return a list of document(s)
+        if isinstance(thisDoc, (list, set)):
+            thisDoc = thisDoc[0]
         thisDoc.update(kwparams)
         return self["requests"].put("%s/%s" % (callName, resource), thisDoc)[0]['result']