cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t..@apache.org
Subject [43/51] [abbrv] git commit: updated refs/heads/marvin_refactor to dbcfc66
Date Thu, 25 Apr 2013 14:46:09 GMT
adding doc strings to jsonHelper


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/c133636e
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/c133636e
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/c133636e

Branch: refs/heads/marvin_refactor
Commit: c133636e68f4c482970cb6e13435fc2ace7245cc
Parents: 5cea601
Author: Prasanna Santhanam <tsp@apache.org>
Authored: Thu Apr 25 16:55:48 2013 +0530
Committer: Prasanna Santhanam <tsp@apache.org>
Committed: Thu Apr 25 16:55:48 2013 +0530

----------------------------------------------------------------------
 tools/marvin/marvin/asyncJobMgr.py |    2 +-
 tools/marvin/marvin/jsonHelper.py  |   94 +++++++++++++++++-------------
 2 files changed, 54 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c133636e/tools/marvin/marvin/asyncJobMgr.py
----------------------------------------------------------------------
diff --git a/tools/marvin/marvin/asyncJobMgr.py b/tools/marvin/marvin/asyncJobMgr.py
index 6984627..f131acb 100644
--- a/tools/marvin/marvin/asyncJobMgr.py
+++ b/tools/marvin/marvin/asyncJobMgr.py
@@ -93,7 +93,7 @@ class workThread(threading.Thread):
                     jobstatus.jobId = jobId
                     try:
                         responseName = cmd.__class__.__name__.replace("Cmd", "Response")
-                        jobstatus.responsecls = jsonHelper.getclassFromName(cmd, responseName)
+                        jobstatus.responsecls = jsonHelper.getClass(cmd, responseName)
                     except:
                         pass
                     jobstatus.status = True

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c133636e/tools/marvin/marvin/jsonHelper.py
----------------------------------------------------------------------
diff --git a/tools/marvin/marvin/jsonHelper.py b/tools/marvin/marvin/jsonHelper.py
index 8316ddb..fd6bd74 100644
--- a/tools/marvin/marvin/jsonHelper.py
+++ b/tools/marvin/marvin/jsonHelper.py
@@ -22,6 +22,7 @@ from cloudstackAPI import *
 
 class jsonLoader:
     '''The recursive class for building and representing objects with.'''
+
     def __init__(self, obj):
         for k in obj:
             v = obj[k]
@@ -33,18 +34,21 @@ class jsonLoader:
                 else:
                     setattr(self, k, v)
             else:
-                setattr(self,k,v)
+                setattr(self, k, v)
+
     def __getattr__(self, val):
         if val in self.__dict__:
             return self.__dict__[val]
         else:
             return None
+
     def __repr__(self):
         return '{%s}' % str(', '.join('%s : %s' % (k, repr(v)) for (k, v) in self.__dict__.iteritems()))
+
     def __str__(self):
         return '{%s}' % str(', '.join('%s : %s' % (k, repr(v)) for (k, v) in self.__dict__.iteritems()))
-    
-    
+
+
 class jsonDump:
     @staticmethod
     def __serialize(obj):
@@ -59,7 +63,7 @@ class jsonDump:
                     if (isinstance(obj[key], list) and len(obj[key]) == 0):
                         continue
                     newobj[key] = jsonDump.__serialize(obj[key])
-                
+
             return newobj
         elif isinstance(obj, list):
             return [jsonDump.__serialize(item) for item in obj]
@@ -69,24 +73,31 @@ class jsonDump:
             return jsonDump.__serialize(obj.__dict__)
         else:
             return repr(obj) # Don't know how to handle, convert to string
-        
+
     @staticmethod
     def dump(obj):
         return jsonDump.__serialize(obj)
 
-def getclassFromName(cmd, name):
-    module = inspect.getmodule(cmd)
+
+def getClass(module, name):
+    """Get the CloudStack command class in a module given the name
+    @param module: cloudstack API module eg: createVolume
+    @param name: string name of the class within the module eg: createVolumeResponse
+    @return: response class
+    """
+    module = inspect.getmodule(module)
     return getattr(module, name)()
 
+
 def finalizeResultObj(result, responseName, responsecls):
     if responsecls is None and responseName.endswith("response") and responseName != "queryasyncjobresultresponse":
         '''infer the response class from the name'''
         moduleName = responseName.replace("response", "")
         try:
-            responsecls = getclassFromName(moduleName, responseName)
+            responsecls = getClass(moduleName, responseName)
         except:
             pass
-    
+
     if responseName is not None and responseName == "queryasyncjobresultresponse" and responsecls
is not None and result.jobresult is not None:
         result.jobresult = finalizeResultObj(result.jobresult, None, responsecls)
         return result
@@ -96,32 +107,33 @@ def finalizeResultObj(result, responseName, responsecls):
         if not isinstance(value, jsonLoader):
             return result
 
-        mirrorObj = True
-        for k,v in value.__dict__.iteritems():
-            if k == 'jobstatus':
-                continue
-            if k not in responsecls.__dict__:
+        mirrorObj = False
+        for k, v in value.__dict__.iteritems():
+            if k in responsecls.__dict__:
+                mirrorObj = True
+            else:
                 mirrorObj = False
                 break
         if mirrorObj:
             responsecls.__dict__.update(value.__dict__)
-            return responsecls
         else:
-            return result
+            responsecls.__dict__.update(result.__dict__)
+        return responsecls
     else:
         return result
 
+
 def getResultObj(returnObj, responsecls=None):
     if len(returnObj) == 0:
         return None
-    responseName = filter(lambda a: a!=u'cloudstack-version', returnObj.keys())[0]
+    responseName = filter(lambda a: a != u'cloudstack-version', returnObj.keys())[0]
     response = returnObj[responseName]
     if len(response) == 0:
         return None
-    
+
     result = jsonLoader(response)
     if result.errorcode is not None:
-        errMsg = "errorCode: %s, errorText:%s"%(result.errorcode, result.errortext)
+        errMsg = "errorCode: %s, errorText:%s" % (result.errorcode, result.errortext)
         raise cloudstackException.cloudstackAPIException(responseName.replace("response",
""), errMsg)
 
     if result.count is not None:
@@ -132,49 +144,49 @@ def getResultObj(returnObj, responsecls=None):
                 return getattr(result, key)
     else:
         return finalizeResultObj(result, responseName, responsecls)
-    
+
 if __name__ == "__main__":
 
-    result = '{ "listnetworkserviceprovidersresponse" : { "count":1 ,"networkserviceprovider"
: [ {"name":"VirtualRouter","physicalnetworkid":"ad2948fc-1054-46c7-b1c7-61d990b86710","destinationphysicalnetworkid":"0","state":"Disabled","id":"d827cae4-4998-4037-95a2-55b92b6318b1","servicelist":["Vpn","Dhcp","Dns","Gateway","Firewall","Lb","SourceNat","StaticNat","PortForwarding","UserData"]}
] } }'
+    result = { "listnetworkserviceprovidersresponse" : { "count":1 ,"networkserviceprovider"
: [ {"name":"VirtualRouter","physicalnetworkid":"ad2948fc-1054-46c7-b1c7-61d990b86710","destinationphysicalnetworkid":"0","state":"Disabled","id":"d827cae4-4998-4037-95a2-55b92b6318b1","servicelist":["Vpn","Dhcp","Dns","Gateway","Firewall","Lb","SourceNat","StaticNat","PortForwarding","UserData"]}
] } }
     nsp = getResultObj(result, listNetworkServiceProviders.listNetworkServiceProvidersResponse)
-    print nsp
+    print "NetworkServiceProvder %s" % nsp
 
-    result = '{ "queryasyncjobresultresponse" : {"errorcode" : 431, "errortext" : "Unable
to execute API command queryasyncjobresultresponse due to missing parameter jobid"}  }'
+    result = { "queryasyncjobresultresponse" : {"errorcode" : 431, "errortext" : "Unable
to execute API command queryasyncjobresultresponse due to missing parameter jobid"}  }
     try:
         asynJob = getResultObj(result)
+        print "AsyncJob %s" % asynJob
     except cloudstackException.cloudstackAPIException, e:
         print e
 
-    result = '{ "queryasyncjobresultresponse" : {}  }'
+    result = { "queryasyncjobresultresponse" : {}  }
     asynJob = getResultObj(result)
-    print asynJob
+    print "AsyncJob %s" % asynJob
 
-    result = '{}'
+    result = {}
     asynJob = getResultObj(result)
-    print asynJob
+    print "AsyncJob %s" % asynJob
 
-    result = '{ "createzoneresponse" :  { "zone" : {"id":"88e796cd-953a-44b9-9445-a7c3ee205cc2","name":"Sandbox-simul","dns1":"10.147.28.6","internaldns1":"10.147.28.6","guestcidraddress":"10.1.1.0/24","networktype":"Advanced","securitygroupsenabled":false,"allocationstate":"Disabled","zonetoken":"ad051d80-17d3-35bf-bc44-77e500132a45","dhcpprovider":"VirtualRouter","localstorageenabled":false}
}  }'
+    result = { "createzoneresponse" :  { "zone" : {"id":"88e796cd-953a-44b9-9445-a7c3ee205cc2","name":"Sandbox-simul","dns1":"10.147.28.6","internaldns1":"10.147.28.6","guestcidraddress":"10.1.1.0/24","networktype":"Advanced","securitygroupsenabled":"false","allocationstate":"Disabled","zonetoken":"ad051d80-17d3-35bf-bc44-77e500132a45","dhcpprovider":"VirtualRouter","localstorageenabled":"false"}
}  }
     res = createZone.createZoneResponse()
     zone = getResultObj(result, res)
-    print zone.id
+    print "Zone id %s" % zone.id
 
-    result = '{ "queryasyncjobresultresponse" : {"accountid":"4a8c3cd0-a696-11e2-b7a5-1aab0c3b0463","userid":"4a8c671e-a696-11e2-b7a5-1aab0c3b0463","cmd":"org.apache.cloudstack.api.command.admin.network.CreatePhysicalNetworkCmd","jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"physicalnetwork":{"id":"e0bc9017-9ba8-4551-a6f9-6b3b2ac1d59c","name":"Sandbox-pnet","broadcastdomainrange":"ZONE","zoneid":"88e796cd-953a-44b9-9445-a7c3ee205cc2","state":"Disabled"}},"created":"2013-04-16T18:37:01+0530","jobid":"8fc09350-f42a-4e04-9427-3d1b68f73dd0"}
}'
+    result = { "queryasyncjobresultresponse" : {"accountid":"4a8c3cd0-a696-11e2-b7a5-1aab0c3b0463","userid":"4a8c671e-a696-11e2-b7a5-1aab0c3b0463","cmd":"org.apache.cloudstack.api.command.admin.network.CreatePhysicalNetworkCmd","jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"physicalnetwork":{"id":"e0bc9017-9ba8-4551-a6f9-6b3b2ac1d59c","name":"Sandbox-pnet","broadcastdomainrange":"ZONE","zoneid":"88e796cd-953a-44b9-9445-a7c3ee205cc2","state":"Disabled"}},"created":"2013-04-16T18:37:01+0530","jobid":"8fc09350-f42a-4e04-9427-3d1b68f73dd0"}
}
     res = createPhysicalNetwork.createPhysicalNetworkResponse()
     res = getResultObj(result, res)
-    print res
+    print "PhysicalNetworkResponse %s" % res
+    print "PhysicalNetwork %s" % res.jobresult.id
 
-    result = '{ "listtemplatesresponse" : { } }'
+    result = { "listtemplatesresponse" : { } }
     print getResultObj(result, listTemplates.listTemplatesResponse())
 
-    result = '{ "queryasyncjobresultresponse" : {"jobid":34,"jobstatus":2,"jobprocstatus":0,"jobresultcode":530,"jobresulttype":"object","jobresult":{"errorcode":431,"errortext":"Please
provide either a volume id, or a tuple(device id, instance id)"}} }'
-    print getResultObj(result, listTemplates.listTemplatesResponse())
-    result = '{ "queryasyncjobresultresponse" : {"jobid":41,"jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"virtualmachine":{"id":37,"name":"i-2-37-TEST","displayname":"i-2-37-TEST","account":"admin","domainid":1,"domain":"ROOT","created":"2011-08-25T11:13:42-0700","state":"Running","haenable":false,"zoneid":1,"zonename":"test0","hostid":5,"hostname":"SimulatedAgent.1e629060-f547-40dd-b792-57cdc4b7d611","templateid":10,"templatename":"CentOS
5.3(64-bit) no GUI (Simulator)","templatedisplaytext":"CentOS 5.3(64-bit) no GUI (Simulator)","passwordenabled":false,"serviceofferingid":7,"serviceofferingname":"Small
Instance","cpunumber":1,"cpuspeed":500,"memory":512,"guestosid":11,"rootdeviceid":0,"rootdevicetype":"NetworkFilesystem","securitygroup":[{"id":1,"name":"default","description":"Default
Security Group"}],"nic":[{"id":43,"networkid":204,"netmask":"255.255.255.0","gateway":"192.168.1.1","ipaddress":"192.168.1.27","isolationuri":"ec2://untagg
 ed","broadcasturi":"vlan://untagged","traffictype":"Guest","type":"Direct","isdefault":true,"macaddress":"06:56:b8:00:00:53"}],"hypervisor":"Simulator"}}}
}'
+    result = { "queryasyncjobresultresponse" : {"jobid":34,"jobstatus":2,"jobprocstatus":0,"jobresultcode":530,"jobresulttype":"object","jobresult":{"errorcode":431,"errortext":"Please
provide either a volume id, or a tuple(device id, instance id)"}} }
+    print "Error API %s " % getResultObj(result, listTemplates.listTemplatesResponse()).jobresult
+    result = { "queryasyncjobresultresponse" : {"jobid":41,"jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"virtualmachine":{"id":37,"name":"i-2-37-TEST","displayname":"i-2-37-TEST","account":"admin","domainid":1,"domain":"ROOT","created":"2011-08-25T11:13:42-0700","state":"Running","haenable":"false","zoneid":1,"zonename":"test0","hostid":5,"hostname":"SimulatedAgent.1e629060-f547-40dd-b792-57cdc4b7d611","templateid":10,"templatename":"CentOS
5.3(64-bit) no GUI (Simulator)","templatedisplaytext":"CentOS 5.3(64-bit) no GUI (Simulator)","passwordenabled":"false","serviceofferingid":7,"serviceofferingname":"Small
Instance","cpunumber":1,"cpuspeed":500,"memory":512,"guestosid":11,"rootdeviceid":0,"rootdevicetype":"NetworkFilesystem","securitygroup":[{"id":1,"name":"default","description":"Default
Security Group"}],"nic":[{"id":43,"networkid":204,"netmask":"255.255.255.0","gateway":"192.168.1.1","ipaddress":"192.168.1.27","isolationuri":"ec2://unt
 agged","broadcasturi":"vlan://untagged","traffictype":"Guest","type":"Direct","isdefault":"true","macaddress":"06:56:b8:00:00:53"}],"hypervisor":"Simulator"}}}
}
+    print "VM response %s " % getResultObj(result, deployVirtualMachine.deployVirtualMachineResponse()).jobresult.id
 
-    result='{ "queryasyncjobresultresponse" : {"accountid":"30910093-22e4-4d3c-a464-8b36b60c8001","userid":"cb0aeca3-42ee-47c4-838a-2cd9053441f2","cmd":"com.cloud.api.commands.DeployVMCmd","jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"virtualmachine":{"id":"d2e4d724-e089-4e59-be8e-647674059016","name":"i-2-14-TEST","displayname":"i-2-14-TEST","account":"admin","domainid":"8cfafe79-81eb-445e-8608-c5b7c31fc3a5","domain":"ROOT","created":"2012-01-15T18:30:11+0530","state":"Running","haenable":false,"zoneid":"30a397e2-1c85-40c0-8463-70278952b046","zonename":"Sandbox-simulator","hostid":"cc0105aa-a2a9-427a-8ad7-4d835483b8a9","hostname":"SimulatedAgent.9fee20cc-95ca-48b1-8268-5513d6e83a1b","templateid":"d92570fa-bf40-44db-9dff-45cc7042604d","templatename":"CentOS
5.3(64-bit) no GUI (Simulator)","templatedisplaytext":"CentOS 5.3(64-bit) no GUI (Simulator)","passwordenabled":false,"serviceofferingid":"3734d632-797b-4f1d-ac62-33f9cf70d005","serviceo
 fferingname":"Sample SO","cpunumber":1,"cpuspeed":100,"memory":128,"guestosid":"1e36f523-23e5-4e90-869b-a1b5e9ba674d","rootdeviceid":0,"rootdevicetype":"NetworkFilesystem","nic":[{"id":"4d3ab903-f511-4dab-8a6d-c2a3b51de7e0","networkid":"faeb7f24-a4b9-447d-bec6-c4956c4ab0f6","netmask":"255.255.240.0","gateway":"10.6.240.1","ipaddress":"10.6.253.89","isolationuri":"vlan://211","broadcasturi":"vlan://211","traffictype":"Guest","type":"Isolated","isdefault":true,"macaddress":"02:00:04:74:00:09"}],"hypervisor":"Simulator"}},"created":"2012-01-15T18:30:11+0530","jobid":"f4a13f28-fcd6-4d7f-b9cd-ba7eb5a5701f"}
}'
+    result= { "queryasyncjobresultresponse" : {"accountid":"30910093-22e4-4d3c-a464-8b36b60c8001","userid":"cb0aeca3-42ee-47c4-838a-2cd9053441f2","cmd":"com.cloud.api.commands.DeployVMCmd","jobstatus":1,"jobprocstatus":0,"jobresultcode":0,"jobresulttype":"object","jobresult":{"virtualmachine":{"id":"d2e4d724-e089-4e59-be8e-647674059016","name":"i-2-14-TEST","displayname":"i-2-14-TEST","account":"admin","domainid":"8cfafe79-81eb-445e-8608-c5b7c31fc3a5","domain":"ROOT","created":"2012-01-15T18:30:11+0530","state":"Running","haenable":"false","zoneid":"30a397e2-1c85-40c0-8463-70278952b046","zonename":"Sandbox-simulator","hostid":"cc0105aa-a2a9-427a-8ad7-4d835483b8a9","hostname":"SimulatedAgent.9fee20cc-95ca-48b1-8268-5513d6e83a1b","templateid":"d92570fa-bf40-44db-9dff-45cc7042604d","templatename":"CentOS
5.3(64-bit) no GUI (Simulator)","templatedisplaytext":"CentOS 5.3(64-bit) no GUI (Simulator)","passwordenabled":"false","serviceofferingid":"3734d632-797b-4f1d-ac62-33f9cf70d005","serv
 iceofferingname":"Sample SO","cpunumber":1,"cpuspeed":100,"memory":128,"guestosid":"1e36f523-23e5-4e90-869b-a1b5e9ba674d","rootdeviceid":0,"rootdevicetype":"NetworkFilesystem","nic":[{"id":"4d3ab903-f511-4dab-8a6d-c2a3b51de7e0","networkid":"faeb7f24-a4b9-447d-bec6-c4956c4ab0f6","netmask":"255.255.240.0","gateway":"10.6.240.1","ipaddress":"10.6.253.89","isolationuri":"vlan://211","broadcasturi":"vlan://211","traffictype":"Guest","type":"Isolated","isdefault":"true","macaddress":"02:00:04:74:00:09"}],"hypervisor":"Simulator"}},"created":"2012-01-15T18:30:11+0530","jobid":"f4a13f28-fcd6-4d7f-b9cd-ba7eb5a5701f"}
}
     vm = getResultObj(result, deployVirtualMachine.deployVirtualMachineResponse())
-    print vm.jobresult.id
+    print "VM job result %s" % vm
+    print "VM response %s " % vm.jobresult.id
 
-    cmd = deployVirtualMachine.deployVirtualMachineCmd()
-    responsename = cmd.__class__.__name__.replace("Cmd", "Response")
-    response = getclassFromName(cmd, responsename)
-    print response.id


Mime
View raw message