File: ManagedMethodExecutorHelper.py

package info (click to toggle)
python-pyvmomi 5.5.0-2014.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,348 kB
  • ctags: 497
  • sloc: python: 6,594; makefile: 12
file content (120 lines) | stat: -rw-r--r-- 4,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python

# VMware vSphere Python SDK
# Copyright (c) 2008-2014 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module provides convinent fns related to ManagedMethodExecutor
"""
__author__ = "VMware, Inc."

from pyVmomi import VmomiSupport, SoapAdapter, vmodl
from SoapAdapter import SoapStubAdapterBase, Serialize, Deserialize

## ManagedMethodExecutor soap stub adapter
#
class MMESoapStubAdapter(SoapStubAdapterBase):
   """ Managed method executor stub adapter  """

   ## Constructor
   #
   # The endpoint can be specified individually as either a host/port
   # combination, or with a URL (using a url= keyword).
   #
   # @param self self
   # @param mme  managed method executor
   def __init__(self, mme):
      stub = mme._stub
      SoapStubAdapterBase.__init__(self, version=stub.version)
      self.mme = mme

   ## Compute the version information for the specified namespace
   #
   # @param ns the namespace
   def ComputeVersionInfo(self, version):
      SoapStubAdapterBase.ComputeVersionInfo(self, version)
      self.versionId = self.versionId[1:-1]

   ## Invoke a managed method, with _ExecuteSoap. Wohooo!
   #
   # @param self self
   # @param mo the 'this'
   # @param info method info
   # @param args arguments
   def InvokeMethod(self, mo, info, args):
      # Serialize parameters to soap parameters
      methodArgs = None
      if info.params:
         methodArgs = vmodl.Reflect.ManagedMethodExecutor.SoapArgument.Array()
         for param, arg in zip(info.params, args):
            if arg is not None:
               # Serialize parameters to soap snippets
               soapVal = Serialize(val=arg, info=param, version=self.version)

               # Insert argument
               soapArg = vmodl.Reflect.ManagedMethodExecutor.SoapArgument(
                                                  name=param.name, val=soapVal)
               methodArgs.append(soapArg)

      moid = mo._GetMoId()
      version = self.versionId
      methodName = VmomiSupport.GetVmodlName(info.type) + "." + info.name

      # Execute method
      result = self.mme.ExecuteSoap(moid=moid,
                                    version=version,
                                    method=methodName,
                                    argument=methodArgs)
      return self._DeserializeExecutorResult(result, info.result)

   ## Invoke a managed property accessor
   #
   # @param self self
   # @param mo the 'this'
   # @param info property info
   def InvokeAccessor(self, mo, info):
      moid = mo._GetMoId()
      version = self.versionId
      prop = info.name

      # Fetch property
      result = self.mme.FetchSoap(moid=moid, version=version, prop=prop)
      return self._DeserializeExecutorResult(result, info.type)

   ## Deserialize result from ExecuteSoap / FetchSoap
   #
   # @param self self
   # @param result result from ExecuteSoap / FetchSoap
   # @param resultType Expected result type
   def _DeserializeExecutorResult(self, result, resultType):
      obj = None
      if result:
         # Parse the return soap snippet. If fault, raise exception
         if result.response:
            # Deserialize back to result
            obj = Deserialize(result.response, resultType, stub=self)
         elif result.fault:
            # Deserialize back to fault (or vmomi fault)
            fault = Deserialize(result.fault.faultDetail,
                                object,
                                stub=self)
            # Silent pylint
            raise fault # pylint: disable-msg=E0702
         else:
            # Unexpected: result should have either response or fault
            msg = "Unexpected execute/fetchSoap error"
            reason = "execute/fetchSoap did not return response or fault"
            raise vmodl.Fault.SystemError(msg=msg, reason=reason)
      return obj