Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit 0ede3cc45d2f7ba14474599fcb86bb9e98c74584
Author: Peter Hatina <phatina(a)redhat.com>
Date: Tue Feb 12 12:51:06 2013 +0100
introduce _LmiClass::create_instance()
---------------------------------------------------------------
cli-tools/lmi/lmi_client_base.py | 10 +++++++
cli-tools/lmi/lmi_client_shell.py | 53 ++++++++++++++++++++++++++----------
2 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_base.py b/cli-tools/lmi/lmi_client_base.py
index 87e5f3d..2509d1f 100644
--- a/cli-tools/lmi/lmi_client_base.py
+++ b/cli-tools/lmi/lmi_client_base.py
@@ -198,6 +198,16 @@ class LmiBaseClient(object):
raise_or_dump_exception(e)
return []
+ def _create_instance(self, classname, namespace, properties = {}, qualifiers = {},
property_list = None):
+ cim_instance = pywbem.CIMInstance(classname, properties, qualifiers,
+ pywbem.CIMInstanceName(classname, namespace = namespace), property_list)
+ try:
+ cim_path = self._cliconn.CreateInstance(NewInstance = cim_instance)
+ except pywbem.CIMError, e:
+ raise_or_dump_exception(e)
+ return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[1])
+ return self._get_instance(cim_path, LocalOnly = False)
+
def _modify_instance(self, instance, **params):
try:
self._cliconn.ModifyInstance(instance, **params)
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py
index 1011e1c..30931f6 100644
--- a/cli-tools/lmi/lmi_client_shell.py
+++ b/cli-tools/lmi/lmi_client_shell.py
@@ -33,12 +33,33 @@ class LmiUnknownParameterError(Exception):
"""Raised, when there is a method call issued and unknown
method parameter is provided"""
+class LmiUnknownPropertyError(Exception):
+ """Raised, when there is an attempt to create instance with
+ unknown property provided"""
+
class LmiMethodCallError(Exception):
"""Raised, when an error occurs within method call"""
class LmiNoPagerError(Exception):
"""Raised, when tere is no default pager like less or
more"""
+def cast_lmi_to_cim(t, value):
+ cast = {
+ "sint8" : lambda x: pywbem.Sint8(x),
+ "uint8" : lambda x: pywbem.Uint8(x),
+ "sint16" : lambda x: pywbem.Sint16(x),
+ "uint16" : lambda x: pywbem.Uint16(x),
+ "sint32" : lambda x: pywbem.Sint32(x),
+ "uint32" : lambda x: pywbem.Uint32(x),
+ "sint64" : lambda x: pywbem.Sint64(x),
+ "uint64" : lambda x: pywbem.Uint64(x),
+ "reference": lambda x: x._cim_instance.path if isinstance(x,
_LmiInstance) else x
+ }
+ if not t.lower() in cast:
+ return value
+ cast_func = cast[t]
+ return cast_func(value)
+
class _LmiTextFormatter(object):
def __init__(self, text):
self._text = text
@@ -356,6 +377,21 @@ class _LmiClass(BaseObject):
def __repr__(self):
return "%s(classname='%s', ...)" % (self.__class__.__name__,
self.classname)
+ def create_instance(self, properties = {}, qualifiers = {}, property_list = None):
+ self_properties = self._cim_class.properties
+ for (key, value) in properties.iteritems():
+ if not key in self._cim_class.properties:
+ if LmiBaseClient._get_use_exceptions():
+ raise LmiUnknownPropertyError
+ return None
+ t = self._cim_class.properties[key].type
+ properties[key] = cast_lmi_to_cim(t, value)
+ (cim_instance, rparams, errorstr) = self._client._create_instance(self.classname,
self.namespace,
+ properties, qualifiers, property_list)
+ if not cim_instance:
+ return None
+ return _LmiInstance(self._client, self, cim_instance)
+
def doc(self):
_LmiXmlFormatter(self._cim_class.tocimxml()).fancy_format(self._client.interactive)
@@ -481,23 +517,10 @@ class _LmiInstanceMethod(BaseObject):
return lmi_rparams
def __call__(self, *args, **kwargs):
- cast = {
- "sint8" : lambda x: pywbem.Sint8(x),
- "uint8" : lambda x: pywbem.Uint8(x),
- "sint16" : lambda x: pywbem.Sint16(x),
- "uint16" : lambda x: pywbem.Uint16(x),
- "sint32" : lambda x: pywbem.Sint32(x),
- "uint32" : lambda x: pywbem.Uint32(x),
- "sint64" : lambda x: pywbem.Sint64(x),
- "uint64" : lambda x: pywbem.Uint64(x),
- "reference": lambda x: x._cim_instance.path if isinstance(x,
_LmiInstance) else x
- }
for (param, value) in kwargs.iteritems():
if param in self._method.parameters:
- t = self._method.parameters[param].type.lower()
- if t in cast:
- cast_func = cast[t]
- kwargs[param] = cast_func(value)
+ t = self._method.parameters[param].type
+ kwargs[param] = cast_lmi_to_cim(t, value)
else:
# NOTE: maybe we could check for pywbem type
# and not to exit prematurely