A Sample Scripts

This appendix provides the sample scripts that were discussed in Chapter 3 in a format that you can use to copy the desired lines into your Jython code.

Example A-1 Script That Retrieves All Targets and Prints Their Names

#emcli_get_targets.py
 
#Import all emcli verbs to current program
from emcli import *
 
def print_target_details(target):
   '''
   print the target name and target type given a target tuple.
   '''
   print target['Target Name'] + ' ' + target['Target Type']
 
#Set the OMS URL to connect to
set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em')
#Accept all the certificates
set_client_property('EMCLI_TRUSTALL','true')

#Login to the OMS
login(username='adminuser')

#Invoke get_targets and loop over the targets array
targets_array = get_targets().out()['data']
for target in targets_array:
      #Call print_target_details function to print the target details
    print_target_details(target)

Example A-2 Script that Incorporates Functions in the get_targets Verb

#emcli_introspect_response.py

#Import all emcli verbs to current program
from emcli import *

#Set the OMS URL to connect to
set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em')
#Accept all the certificates
set_client_property('EMCLI_TRUSTALL','true')

#Login to the OMS
login(username='sysman')

res = get_targets()

print 'Number of targets:'+str(len(res.out()['data']))
print 'Errors           :'+res.error()
print 'Exit code        :'+str(res.exit_code())
print 'IsJson           :'+str(res.isJson())

Example A-3 Script that Incorporates Custom SQL with the list() Function

#emcli_json_processing.py
#Import all EM CLI verbs to current program
from emcli import *
def format(str):
   '''
   Given a string argument returns it back or returns 
   a blank string if it is of None type
   '''
   if str is None:
     return ""
   return str

def get_targets_with_props(p_prop_name, p_prop_val):
'''
Returns targets with given property name and its value. Uses list verb.
'''
l_sql = "select target_name, target_type, property_value " \
        "from mgmt$target_properties " \
        "where property_name = '" + p_prop_name + "' " + "   " \
        "and property_value like '" + p_prop_val + "'"
   obj = list(sql=l_sql)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
   return obj
#Set the OMS URL to connect to
set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em')
#Accept all the certificates
set_client_property('EMCLI_TRUSTALL','true')
#Log in to the OMS
login(username='sysman')
#Find all the targets that have Version property set to release 12
l_targets = get_targets_with_props('Version', '12%')
for target in l_targets.out()['data']:
      tn =  target['TARGET_NAME']
      tt =  target['TARGET_TYPE']
      pv =  target['PROPERTY_VALUE']
      print "Name "+tn + " Type =" + tt + " value=" + pv

Example A-4 Script that Incorporates Exception Handling

#emcli_error_exception_handling.py
 
#import all emcli verbs to current program
from emcli import *
#import the verbexecutionerror
from emcli.exception import VerbExecutionError
 
#Set the OMS URL to connect to
set_client_property('EMCLI_OMS_URL','https://host1.example.com:1234/em')
#Accept all the certificates
set_client_property('EMCLI_TRUSTALL','true')

#Login to the OMS
login(username='sysman')

#Create a group
res = create_group(name='Jan_Doe_Group')

print res.out()

#Try to create the same group again
try:
  #This will trigger an exception as the group exist already
  create_group(name='Jan_Doe_Group')
except VerbExecutionError , e:
   print e.error()
   print 'Exit code:'+str(e.exit_code())