Database Reference
In-Depth Information
Python Class with EM CLI to Set Target Properties
Creating a Python “class” allows us to “package” code into a single unit in which all of the code that is part of the
class is both aware of and can use all of the other code within that class. From a class, one can create an “instance.”
An instance is an object in Python, and all of the changes to pieces of that instance are persistent for the life of that
instance. Listing 6-11 shows the full text of the
updateProps.py
script, which contains the
updateProps()
class. For
best results, create a file on the file system called
updateProps.py
and paste the full text of the code in it:
Listing 6-11.
update Props.py
creates the
updateProps( )
class
import emcli
import re
import operator
class updateProps():
def __init__(self, agentfilter='.*', typefilter='.*',
namefilter='.*', propdict={}):
self.targs = []
self.reloadtargs = True
self.props(propdict)
self.__loadtargobjects()
self.filt(agentfilter=agentfilter, typefilter=typefilter,
namefilter=namefilter)
def __loadtargobjects(self):
if self.reloadtargs == True:
self.reloadtargs = False
self.fulltargs = \
emcli.list(resource='Targets').out()['data']
self.targprops = \
emcli.list(resource='TargetProperties'
).out()['data']
def props(self, propdict):
assert isinstance(propdict, dict), \
'propdict parameter must be ' + \
'a dictionary of ' + \
'{"property_name":"property_value"}'
self.propdict = propdict
def filt(self, agentfilter='.*', typefilter='.*',
namefilter='.*',
sort=('TARGET_TYPE','TARGET_NAME'), show=False):
self.targs = []
__agentcompfilt = re.compile(agentfilter)
__typecompfilt = re.compile(typefilter)
__namecompfilt = re.compile(namefilter)
self.__loadtargobjects()
for __inttarg in self.fulltargs:
if __typecompfilt.search(__inttarg['TARGET_TYPE']) \
and __namecompfilt.search(
__inttarg['TARGET_NAME']) \
and (__inttarg['EMD_URL'] == None or \
__agentcompfilt.search(__inttarg['EMD_URL'])):
self.targs.append(__inttarg)
__myoperator = operator
Search WWH ::
Custom Search