Skip to content

Commit a02adad

Browse files
committed
ZEN-35546: Add API that allows to get productionState from deviceRouter in batch
1 parent 75e4b3c commit a02adad

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist
1010
*.pyc
1111
*~
1212
*.sw?
13+
.vscode/

src/Products/Zuul/facades/devicefacade.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from AccessControl import getSecurityManager
2020
from Acquisition import aq_base
21-
from Products.AdvancedQuery import Eq, Or, Generic, And, MatchGlob
21+
from Products.AdvancedQuery import Eq, Or, Generic, And, MatchGlob, In
2222
from ZODB.transact import transact
2323
from zope.component import getMultiAdapter, getUtility
2424
from zope.event import notify
@@ -1486,3 +1486,26 @@ def maskPropertyPassword(self, inst, propname):
14861486
if inst.zenPropIsPassword(propname):
14871487
prop = "*" * len(prop)
14881488
return prop
1489+
1490+
def getDevicesProductionState(self, uids):
1491+
"""
1492+
Retrieves the production state for a list of devices.
1493+
"""
1494+
model_catalog = IModelCatalogTool(self._dmd)
1495+
uids_set = set(uids)
1496+
query = In("uid", list(uids_set))
1497+
results = list(
1498+
model_catalog.search(
1499+
types="Products.ZenModel.Device.Device",
1500+
query=query,
1501+
fields=["productionState", "uid"],
1502+
limit=None,
1503+
).results
1504+
)
1505+
1506+
if len(results) != len(uids_set):
1507+
found_uids = set(b.uid for b in results)
1508+
invalid_uids = uids_set - found_uids
1509+
raise Exception("Invalid device UIDs: %s" % ", ".join(invalid_uids))
1510+
1511+
return [{"uid": b.uid, "productionState": b.productionState} for b in results]

src/Products/Zuul/interfaces/device.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,12 @@ def resetBoundTemplates(self, uid):
221221
@type uid: str
222222
@rtype: void
223223
"""
224+
def getDevicesProductionState(uids):
225+
"""
226+
Retrieves the production state for a list of devices.
227+
228+
@type uids: list
229+
@param uids: List of device uids to get production state for
230+
@rtype: list
231+
@return: List of dictionaries containing device uid and productionState
232+
"""

src/Products/Zuul/routers/device.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,25 @@ def getDevices(self, uid=None, start=0, params=None, limit=50, sort='name',
509509
return DirectResponse(devices=data, totalCount=devices.total,
510510
hash=devices.hash_)
511511

512+
@serviceConnectionError
513+
def getDevicesProductionState(self, uids):
514+
"""
515+
Retrieves the production state for a list of devices.
516+
517+
@type uids: list
518+
@param uids: List of device uids to get production state for
519+
@rtype: DirectResponse
520+
@return: B{Properties}:
521+
- data: (list) List of dictionaries containing device uid and productionState
522+
"""
523+
facade = self._getFacade()
524+
try:
525+
data = facade.getDevicesProductionState(uids)
526+
return DirectResponse.succeed(data=data)
527+
except Exception as e:
528+
log.exception(e)
529+
return DirectResponse.exception(e, "Failed to get production states.")
530+
512531
def renameDevice(self, uid, newId, retainGraphData=False):
513532
"""
514533
Set the device specified by the uid,"uid" to have the

0 commit comments

Comments
 (0)