1 krisbash 1.1 #!/usr/bin/python
2
3 import xml.etree.ElementTree as ET
4 import sys
5 import string
6 from optparse import OptionParser
7
8 parser = OptionParser()
9 parser.add_option("-i","--Input",action="store",type="string",dest="Input",help="Input CDXML file")
10 parser.add_option("-d","--Domain",action="store",type="string",dest="Domain",help="Domain name used to connect to the server")
11 parser.add_option("-u","--Username",action="store",type="string",dest="Username",help="Username used to connect to the server")
12 parser.add_option("-p","--Password",action="store",type="string",dest="Password",help="Password used to connect to the server")
13 #parser.add_option("-f","--file",action="store",type="string",dest="filename",help="Write result to FILE",metavar="FILE")
14
15 (options,args) = parser.parse_args()
16 if options.Input == None:
17 parser.error("Input cdxml cannot be empty!")
18 if options.Domain == None:
19 parser.error("Domain cannot be empty!")
20 if options.Username == None:
21 parser.error("Username cannot be empty!")
22 krisbash 1.1 if options.Password == None:
23 parser.error("Password cannot be empty!")
24 try:
25 tree = ET.parse(options.Input)
26 #missing file error
27 except IOError,e:
28 print "%s" % e
29 sys.exit(1)
30 #bad cdxml format error
31 except Exception,e:
32 print "Cannot parse the input CDXML,%s" % e
33 sys.exit(1)
34 root = tree.getroot()
35
36 #Get namespace and classname
37 classElement = root.find('Class')
38 attrib = classElement.attrib['ClassName']
39 namespace,classname= attrib.rsplit('/',1)
40
41 #new file
42 scriptFile = None
43 krisbash 1.1
44 #Get Default noun
45 defaultNoun = classElement.find('DefaultNoun').text
46
47 #InstanceCmdlets node
48 instCmd = classElement.find('InstanceCmdlets')
49
50 #read global set of parameters
51 #global boolean to indicate it's getting operation or others
52 getOperation = False
53
54 #Check cmdlet type by checking if child node GetCmdlet exists
55 getCmd = instCmd.find('GetCmdlet')
56 if getCmd != None:
57 getOperation = True
58 #create script get-noun.py
59 filename = 'Get-'+defaultNoun+'.py'
60 scriptFile = open(filename,'w+')
61
62 #Get parameters if overrided: #add to propertyDict
63 propertyDict = {}
64 krisbash 1.1 #list of mandatory parameters
65 propertyMand = []
66 paras = getCmd.find('GetCmdletParameters')
67 properties = paras.find('QueryableProperties')
68 #for each property, get the name and type of the property
69 for pro in properties.findall('Property'):
70 pName = pro.attrib['PropertyName']
71 typeTag = pro.find('Type')
72 pType = typeTag.attrib['PSType']
73 propertyDict[pName]=pType
74
75 #check QueryType ########################################don't know the difference between query types???#
76 queryType = pro.find('RegularQuery')
77 if queryType != None:
78 queryPara = queryType.find('CmdletParameterMetadata')
79 isMandatory = queryPara.attrib['IsMandatory']
80 if isMandatory == 'true':
81 propertyMand.append(pName)
82
83 #write cmdlet content to the generated file:
84 #imports:
85 krisbash 1.1 scriptFile.write('import mi \n')
86 scriptFile.write('import ast \n')
87 scriptFile.write('from optparse import OptionParser \n')
88 scriptFile.write('\n')
89
90 #optparse:
91 scriptFile.write('parser = OptionParser() \n')
92 if propertyDict !={}:
93 for name in propertyDict:
94 optType = propertyDict[name]
95 if 'int' in optType:
96 scriptFile.write('parser.add_option("--'+name+'", action="store",type="int",'+'dest="'+name+'",help="value of the property:'+name+'"'+')')
97 else:
98 scriptFile.write('parser.add_option("--'+name+'", action="store",type="'+optType+'",'+'dest="'+name+'",help="value of the property:'+name+'"'+')')
99
100 #end of property section
101 scriptFile.write('\n')
102 scriptFile.write('(options,args) = parser.parse_args() \n')
103 #warning of mandatory parameters:
104 for item in propertyMand:
105 scriptFile.write('if options.'+item+' == None: \n')
106 krisbash 1.1 scriptFile.write(' parser.error("'+item+' cannot be empty!") \n')
107
108 #connection, get session:
109 scriptFile.write('\n')
110 scriptFile.write('session = mi.connect("'+options.Domain+'","'+options.Username+'","'+options.Password+'") \n')
111 scriptFile.write('\n')
112
113 #Get-instance:
114 #make the key instance based on the key and input
115 if getOperation and propertyDict !={}:
116 for name in propertyDict:
117 scriptFile.write('keyProperties = {} \n')
118 scriptFile.write('propertyValue = '+'options.'+name +'\n')
119 scriptFile.write('keyProperties["'+name+'"] = propertyValue'+'\n')
120 scriptFile.write('\n')
121
122 #api call
123 scriptFile.write('inst = session.get_instance("'+namespace+'","'+classname+'",keyProperties) \n')
124 scriptFile.write('mi.print_instance(inst)')
|