Skip to content

Commit

Permalink
Create setProductionState.py
Browse files Browse the repository at this point in the history
Adding an example of setting a prod state from the API
  • Loading branch information
kickingsawdust authored Oct 7, 2024
1 parent d664c17 commit d82d57c
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions examples/setProductionState.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
#####################################################
# Copyright (C) Zenoss, Inc. 2024, all rights reserved.
# Author: Team Pinecone
# Contact: Bill Loss 'closs @ zenoss.com'
#####################################################

from __future__ import print_function

import sys
import zenApiLib
from zenApiDeviceRouterHelper import ZenDeviceUidFinder

router = 'DeviceRouter'
method = 'setProductionState'

usage = '%s <device_id> <device_class_name>' % (sys.argv[0])


def fail():
print('Invalid arguments. \nUsage: %s' % (usage))
sys.exit(1)


def buildArgs():
'''
This builds the data dictionary required for the API call. We check to
make sure we have exactly the correct arguments, then return the dict
'''
if len(sys.argv) != 3:
fail()
else:
try:
device = sys.argv[1]
prodStateNumber = sys.argv[2]
except:
fail()
data = {'deviceName': device,
'prodState': prodStateNumber
}
return data


def setProdState(**data):
'''
This makes the API call and returns the result
'''
dr = zenApiLib.zenConnector(routerName = router)
response = dr.callMethod(method, **data)
#response = dr.callMethod(method, override=data)
if response.get('result', {}).get('success', False) is False:
raise Exception('API call returned unsucessful result.\n%s' % response)
return response['result']


if __name__ == '__main__':
'''
Build the args and make the API call to set the device production state
'''
data = buildArgs()
print(data)
deviceFindResults = ZenDeviceUidFinder(name=data['deviceName'])
if deviceFindResults.getCount() > 1:
raise Exception('Multiple devices found that matched "%s"' % data['deviceName'])
data['uid'] = deviceFindResults.getFirstUid()
#import pdb; pdb.set_trace()
api_response = setProdState(uids=data['uid'], prodState=data['prodState'], hashcheck=0)
print(data)
print(api_response)

0 comments on commit d82d57c

Please sign in to comment.