forked from zenoss/pywbem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcim_provider.py
1914 lines (1676 loc) · 76.8 KB
/
cim_provider.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# (C) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Bart Whiteley
# Jon Carey
####
r"""Python CIM Providers (aka "nirvana")
This module is an abstraction and utility layer between a CIMOM and
Python providers. The CIMOM uses this module to load Python providers,
and route requests to those providers.
Python Provider Modules
Python Providers are implemented as Python modules. By convention
these modules are installed into /usr/lib/pycim. However, they can
be anywhere. These modules are loaded on demand using load_source()
from the imp module. The CIMOM's pycim interface stores the timestamp
of the provider modules. If the modules change, the CIMOM reloads the
modules. This is very useful while developing providers, since the
latest code will always be loaded and used.
A Python Provider Module will contain functions, attributes, and
instances that will be accessed and manipulated by this module.
Providers are often classified in the following catagories:
Instance -- Instrument the retrieval, creation, modification,
and deletion of CIM instances.
Association -- Instrument CIM associations (CIM classes with the
Association qualifier).
Method -- Instrument methods as defined on CIM instances or CIM
classes.
Indication -- Generates indications based on indication
subscriptions.
Indication Consumer -- "Consumes" (or "Handles") an indication,
possibly delivering it through some other means, such as email.
Polled -- A polled provider is allowed to run periodically (by
calling its poll function). This allows a provider to do some
periodic work, without the need to create its own thread.
An Instance, Association, and/or Method provider is created by defining
one or more subclasses of CIMProvider within the provider module, and
registering instances of the subclass(es) with CIM class names by way
of the get_providers function (described below). Refer to
the documentation for CIMProvider in this module.
Indication, Indication Consumer, and Polled providers are defined by
implementing some functions within the provider module.
Provider module functions:
init(env):
This module function is optional. It is called immediately
after the provider module is imported.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
get_providers(env):
Return a dict that maps CIM class names to instances of
CIMProvider subclasses. Note that multiple classes can be
instrumented by the same instance of a CIMProvider subclass.
The CIM class names are case-insensitive, since this dict is
converted to a NocaseDict.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
For example, a Python Provider Module may contain the following:
class Py_FooBarProvider(CIMProvider):
...
def get_providers(env):
_fbp = Py_FooBarProvider()
return {'Py_Foo':_fbp, 'Py_Bar':_fbp}
get_initial_polling_interval(env):
Return the number of seconds before the first call to poll.
If this method returns zero, then the poll method is never called.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
poll(env):
Do some work, and return the number of seconds until the next poll.
A polled provider's poll function will be called periodically by
the CIMOM. The polled provider can use this opportunity to do
some work, such as checking on some conditions, and generating
indications. The poll function returns the number of seconds the
CIMOM should wait before calling poll again. A return value of -1
indicates to the CIMOM that the previous poll value should be used.
A return value of 0 indicates that the poll function should never
be called again.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
can_unload(env):
Return True if the provider can be unloaded.
The CIMOM may try to unload a provider after a period of inactivity.
Before unloading a provider, the CIMOM asks the provider if it can
be unloaded.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
shutdown(env):
Perform any cleanup tasks prior to being unloaded.
The provider will shortly be unloaded, and is given an opportunity
to perform any needed cleanup. The provider may be unloaded after
a period of inactivity (see the documentation for can_unload), or
because the CIMOM is shutting down.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
handle_indication(env, ns, handler_instance, indication_instance):
Process an indication.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
ns -- The namespace where the even occurred
handler_instance --
indication_instance -- The indication
activate_filter (env, filter, ns, classes,
first_activation):
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
filter --
namespace --
classes --
first_activation --
deactivate_filter(env, filter, ns, classes,
last_activation):
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
filter --
ns --
classes --
last_activation --
Provider Environment
A pycimmb.ProviderEnvironment is passed to many functions. This is
a handle back into the CIMOM. You can use it for logging and for
making "up-calls" to the CIMOM. For example:
logger = env.get_logger()
logger.log_debug('Debug Info')
ch = env.get_cimom_handle()
other_inst = ch.GetInstance(inst_path, LocalOnly=False,
IncludeQualifiers=False,
IncludeClassOrigin=False)
The API of the pycimmb.CIMOMHandle resembles that of
pywbem.WBEMConnection.
For more information on the ProviderEnvironments, and other features
provided by pycimmb, refer to the pycimmb documentation.
CodeGen
The codegen function can be used to generate provider stub code for a
given CIM class. This is a quick way to get started writing a provider.
"""
import sys
from os.path import dirname
import pywbem
from imp import load_source
import types
__all__ = ['CIMProvider']
def _path_equals_ignore_host(lhs, rhs):
"""If one object path doesn't inlcude a host, don't include the hosts
in the comparison
"""
if lhs is rhs:
return True
if lhs.host is not None and rhs.host is not None and lhs.host != rhs.host:
return False
# need to make sure this stays in sync with CIMInstanceName.__cmp__()
return not (pywbem.cmpname(rhs.classname, lhs.classname) or
cmp(rhs.keybindings, lhs.keybindings) or
pywbem.cmpname(rhs.namespace, lhs.namespace))
class CIMProvider(object):
"""Base class for CIM Providers.
A derived class might normally override the following:
- enum_instances
- get_instance
- set_instance
- delete_instance
- references
If the provider is a "read-only" instance provider, set_instance and
delete_instance need not be overridden.
Only association providers need to override references.
A method provider should implement a method of the form:
def cim_method_<method_name>(self, env, object_name, method,
param_<input_param_1>,
param_<input_param_2>,
...):
Where <method_name> is the name of the method from the CIM schema.
<method_name> needs to be all lowercase, regardless of the case of
the method name in the CIM schema (CIM method names are case
insensitive).
Keyword arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
object_name -- A pywbem.CIMInstanceName or pywbem.CIMClassname
specifying the object on which the method is to be invoked.
method -- A pywbem.CIMMethod, representing the method to execute.
param_<param_name> -- Corresponds to the input parameter <param_name>
from the CIM schema. <param_name> needs to be all lowercase,
regardless of the case of the parameter name in the CIM schema
(CIM parameter names are case insensitive).
The method returns a two-tuple containing the return value of the
method, and a dictionary containing the output parameters.
Example:
def cim_method_requeststatechange(self, env, object_name, method,
param_requestedstate,
param_timeoutperiod):
# do stuff.
out_params = {'job': pywbem.CIMInstanceName(...)}
rval = pywbem.Uint32(0)
return (rval, out_params)
The methods prefixed with "MI_" correspond to the WBEM operations
from http://www.dmtf.org/standards/published_documents/DSP200.html
The default implementations of these methods call the methods
described above. These will not normally be overridden or extended
by a subclass.
"""
def get_instance (self, env, model, cim_class):
"""Return an instance.
Keyword arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
model -- A template of the pywbem.CIMInstance to be returned. The
key properties are set on this instance to correspond to the
instanceName that was requested. The properties of the model
are already filtered according to the PropertyList from the
request. Only properties present in the model need to be
given values. If you prefer, you can set all of the
values, and the instance will be filtered for you.
cim_class -- The pywbem.CIMClass
Possible Errors:
CIM_ERR_ACCESS_DENIED
CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
or otherwise incorrect parameters)
CIM_ERR_NOT_FOUND (the CIM Class does exist, but the requested CIM
Instance does not exist in the specified namespace)
CIM_ERR_FAILED (some other unspecified error occurred)
"""
return None
def enum_instances(self, env, model, cim_class, keys_only):
"""Enumerate instances.
The WBEM operations EnumerateInstances and EnumerateInstanceNames
are both mapped to this method.
This method is a python generator
Keyword arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
model -- A template of the pywbem.CIMInstances to be generated.
The properties of the model are already filtered according to
the PropertyList from the request. Only properties present in
the model need to be given values. If you prefer, you can
always set all of the values, and the instance will be filtered
for you.
cim_class -- The pywbem.CIMClass
keys_only -- A boolean. True if only the key properties should be
set on the generated instances.
Possible Errors:
CIM_ERR_FAILED (some other unspecified error occurred)
"""
pass
def set_instance(self, env, instance, previous_instance, cim_class):
"""Return a newly created or modified instance.
Keyword arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
instance -- The new pywbem.CIMInstance. If modifying an existing
instance, the properties on this instance have been filtered by
the PropertyList from the request.
previous_instance -- The previous pywbem.CIMInstance if modifying
an existing instance. None if creating a new instance.
cim_class -- The pywbem.CIMClass
Return the new instance. The keys must be set on the new instance.
Possible Errors:
CIM_ERR_ACCESS_DENIED
CIM_ERR_NOT_SUPPORTED
CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
or otherwise incorrect parameters)
CIM_ERR_ALREADY_EXISTS (the CIM Instance already exists -- only
valid if previous_instance is None, indicating that the operation
was CreateInstance)
CIM_ERR_NOT_FOUND (the CIM Instance does not exist -- only valid
if previous_instance is not None, indicating that the operation
was ModifyInstance)
CIM_ERR_FAILED (some other unspecified error occurred)
"""
raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED, "")
def delete_instance(self, env, instance_name):
"""Delete an instance.
Keyword arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
instance_name -- A pywbem.CIMInstanceName specifying the instance
to delete.
Possible Errors:
CIM_ERR_ACCESS_DENIED
CIM_ERR_NOT_SUPPORTED
CIM_ERR_INVALID_NAMESPACE
CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
or otherwise incorrect parameters)
CIM_ERR_INVALID_CLASS (the CIM Class does not exist in the specified
namespace)
CIM_ERR_NOT_FOUND (the CIM Class does exist, but the requested CIM
Instance does not exist in the specified namespace)
CIM_ERR_FAILED (some other unspecified error occurred)
"""
raise pywbem.CIMError(pywbem.CIM_ERR_NOT_SUPPORTED, "")
def references(self, env, object_name, model, assoc_class,
result_class_name, role, result_role, keys_only):
"""Instrument Associations.
All four association-related operations (Associators, AssociatorNames,
References, ReferenceNames) are mapped to this method.
This method is a python generator
Keyword arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
object_name -- A pywbem.CIMInstanceName that defines the source
CIM Object whose associated Objects are to be returned.
model -- A template pywbem.CIMInstance to serve as a model
of the objects to be returned. Only properties present on this
model need to be set.
assoc_class -- The pywbem.CIMClass.
result_class_name -- If not empty, this string acts as a filter on
the returned set of Instances by mandating that each returned
Instances MUST represent an association between object_name
and an Instance of a Class whose name matches this parameter
or a subclass.
role -- If not empty, MUST be a valid Property name. It acts as a
filter on the returned set of Instances by mandating that each
returned Instance MUST refer to object_name via a Property
whose name matches the value of this parameter.
result_role -- If not empty, MUST be a valid Property name. It acts
as a filter on the returned set of Instances by mandating that
each returned Instance MUST represent associations of
object_name to other Instances, where the other Instances play
the specified result_role in the association (i.e. the
name of the Property in the Association Class that refers to
the Object related to object_name MUST match the value of this
parameter).
keys_only -- A boolean. True if only the key properties should be
set on the generated instances.
The following diagram may be helpful in understanding the role,
result_role, and result_class_name parameters.
+------------------------+ +-------------------+
| object_name.classname | | result_class_name |
| ~~~~~~~~~~~~~~~~~~~~~ | | ~~~~~~~~~~~~~~~~~ |
+------------------------+ +-------------------+
| +-----------------------------------+ |
| | [Association] assoc_class | |
| object_name | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
+--------------+ object_name.classname REF role | |
(CIMInstanceName) | result_class_name REF result_role +------+
| |(CIMInstanceName)
+-----------------------------------+
Possible Errors:
CIM_ERR_ACCESS_DENIED
CIM_ERR_NOT_SUPPORTED
CIM_ERR_INVALID_NAMESPACE
CIM_ERR_INVALID_PARAMETER (including missing, duplicate, unrecognized
or otherwise incorrect parameters)
CIM_ERR_FAILED (some other unspecified error occurred)
"""
pass
def _set_filter_results(self, value):
self._filter_results = value
def _get_filter_results(self):
if hasattr(self, '_filter_results'):
return self._filter_results
return True
filter_results = property(_get_filter_results,
_set_filter_results,
None,
"""Determines if the CIMProvider base class should filter results
If True, the subclass of CIMProvider in the provider module
does not need to filter returned results based on property_list,
and in the case of association providers, role, result_role, and
result_class_name. The results will be filtered by the
CIMProvider base class.
If False, the CIMProvider base class will do no filtering.
Therefore the subclass of CIMProvider in the provider module will
have to filter based on property_list, and in the case of
association providers, role, result_role, and result_class_name.""")
def MI_enumInstanceNames(self,
env,
ns,
cimClass):
"""Return instance names of a given CIM class
Implements the WBEM operation EnumerateInstanceNames in terms
of the enum_instances method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_enumInstanceNames called...')
provClass = False
keys = pywbem.NocaseDict()
[keys.__setitem__(p.name, p) for p in cimClass.properties.values()\
if 'key' in p.qualifiers]
_strip_quals(keys)
path = pywbem.CIMInstanceName(classname=cimClass.classname,
namespace=ns)
model = pywbem.CIMInstance(classname=cimClass.classname,
properties=keys,
path=path)
gen = self.enum_instances(env=env,
model=model,
cim_class=cimClass,
keys_only=True)
try:
iter(gen)
except TypeError:
logger.log_debug('CIMProvider MI_enumInstanceNames returning')
return
for inst in gen:
rval = build_instance_name(inst)
yield rval
logger.log_debug('CIMProvider MI_enumInstanceNames returning')
def MI_enumInstances(self,
env,
ns,
propertyList,
requestedCimClass,
cimClass):
"""Return instances of a given CIM class
Implements the WBEM operation EnumerateInstances in terms
of the enum_instances method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_enumInstances called...')
keyNames = get_keys_from_class(cimClass)
plist = None
if propertyList is not None:
lkns = [kn.lower() for kn in keyNames]
props = pywbem.NocaseDict()
plist = [s.lower() for s in propertyList]
pklist = plist + lkns
[props.__setitem__(p.name, p) for p in cimClass.properties.values()
if p.name.lower() in pklist]
else:
props = cimClass.properties
_strip_quals(props)
path = pywbem.CIMInstanceName(classname=cimClass.classname,
namespace=ns)
model = pywbem.CIMInstance(classname=cimClass.classname, properties=props,
path=path)
gen = self.enum_instances(env=env,
model=model,
cim_class=cimClass,
keys_only=False)
try:
iter(gen)
except TypeError:
logger.log_debug('CIMProvider MI_enumInstances returning')
return
for inst in gen:
inst.path = build_instance_name(inst, keyNames)
if self.filter_results and plist is not None:
inst = inst.copy()
filter_instance(inst, plist)
yield inst
logger.log_debug('CIMProvider MI_enumInstances returning')
def MI_getInstance(self,
env,
instanceName,
propertyList,
cimClass):
"""Return a specific CIM instance
Implements the WBEM operation GetInstance in terms
of the get_instance method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_getInstance called...')
keyNames = get_keys_from_class(cimClass)
plist = None
if propertyList is not None:
lkns = [kn.lower() for kn in keyNames]
props = pywbem.NocaseDict()
plist = [s.lower() for s in propertyList]
pklist = plist + lkns
[props.__setitem__(p.name, p) for p in cimClass.properties.values()
if p.name.lower() in pklist]
else:
props = cimClass.properties
_strip_quals(props)
model = pywbem.CIMInstance(classname=instanceName.classname,
properties=props,
path=instanceName)
for k, v in instanceName.keybindings.items():
type = cimClass.properties[k].type
if type != 'reference':
v = val = pywbem.tocimobj(type, v)
model.__setitem__(k, pywbem.CIMProperty(name=k, type=type,
value=v))
rval = self.get_instance(env=env,
model=model,
cim_class=cimClass)
if self.filter_results:
filter_instance(rval, plist)
logger.log_debug('CIMProvider MI_getInstance returning')
if rval is None:
raise pywbem.CIMError(pywbem.CIM_ERR_NOT_FOUND, "")
return rval
def MI_createInstance(self,
env,
instance):
"""Create a CIM instance, and return its instance name
Implements the WBEM operation CreateInstance in terms
of the set_instance method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_createInstance called...')
rval = None
ch = env.get_cimom_handle()
cimClass = ch.GetClass(instance.classname,
instance.path.namespace,
LocalOnly=False,
IncludeQualifiers=True)
# CIMOM has already filled in default property values for
# props with default values, if values not supplied by client.
rval = self.set_instance(env=env,
instance=instance,
previous_instance=None,
cim_class=cimClass)
rval = build_instance_name(rval, cimClass)
logger.log_debug('CIMProvider MI_createInstance returning')
return rval
def MI_modifyInstance(self,
env,
modifiedInstance,
previousInstance,
propertyList,
cimClass):
"""Modify a CIM instance
Implements the WBEM operation ModifyInstance in terms
of the set_instance method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_modifyInstance called...')
if propertyList is not None:
plist = [p.lower() for p in propertyList]
filter_instance(modifiedInstance, plist)
modifiedInstance.update(modifiedInstance.path)
self.set_instance(env=env,
instance=modifiedInstance,
previous_instance=previousInstance,
cim_class=cimClass)
logger.log_debug('CIMProvider MI_modifyInstance returning')
def MI_deleteInstance(self,
env,
instanceName):
"""Delete a CIM instance
Implements the WBEM operation DeleteInstance in terms
of the delete_instance method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_deleteInstance called...')
self.delete_instance(env=env, instance_name=instanceName)
logger.log_debug('CIMProvider MI_deleteInstance returning')
def MI_associators(self,
env,
objectName,
assocClassName,
resultClassName,
role,
resultRole,
propertyList):
"""Return instances associated to a given object.
Implements the WBEM operation Associators in terms
of the references method. A derived class will not normally
override this method.
"""
# NOTE: This should honor the parameters resultClassName, role, resultRole,
# and propertyList
logger = env.get_logger()
logger.log_debug('CIMProvider MI_associators called. assocClass: %s' % (assocClassName))
ch = env.get_cimom_handle()
if not assocClassName:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
"Empty assocClassName passed to Associators")
assocClass = ch.GetClass(assocClassName, objectName.namespace,
LocalOnly=False,
IncludeQualifiers=True)
plist = pywbem.NocaseDict()
[plist.__setitem__(p.name, p) for p in assocClass.properties.values()
if 'key' in p.qualifiers or p.type == 'reference']
_strip_quals(plist)
model = pywbem.CIMInstance(classname=assocClass.classname,
properties=plist)
model.path = pywbem.CIMInstanceName(classname=assocClass.classname,
namespace=objectName.namespace)
for inst in self.references(env=env,
object_name=objectName,
model=model,
assoc_class=assocClass,
result_class_name=resultClassName,
role=role,
result_role=None,
keys_only=False):
for prop in inst.properties.values():
lpname = prop.name.lower()
if prop.type != 'reference':
continue
if role and role.lower() == lpname:
continue
if resultRole and resultRole.lower() != lpname:
continue
if _path_equals_ignore_host(prop.value, objectName):
continue
if resultClassName and self.filter_results and \
not pywbem.is_subclass(ch, objectName.namespace,
sub=prop.value.classname,
super=resultClassName):
continue
try:
if prop.value.namespace is None:
prop.value.namespace = objectName.namespace
inst = ch.GetInstance(prop.value,
IncludeQualifiers=True,
IncludeClassOrigin=True,
PropertyList=propertyList)
except pywbem.CIMError, (num, msg):
if num == pywbem.CIM_ERR_NOT_FOUND:
continue
else:
raise
if inst.path is None:
inst.path = prop.value
yield inst
logger.log_debug('CIMProvider MI_associators returning')
def MI_associatorNames(self,
env,
objectName,
assocClassName,
resultClassName,
role,
resultRole):
"""Return instances names associated to a given object.
Implements the WBEM operation AssociatorNames in terms
of the references method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_associatorNames called. assocClass: %s' % (assocClassName))
ch = env.get_cimom_handle()
if not assocClassName:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
"Empty assocClassName passed to AssociatorNames")
assocClass = ch.GetClass(assocClassName, objectName.namespace,
LocalOnly=False,
IncludeQualifiers=True)
keys = pywbem.NocaseDict()
[keys.__setitem__(p.name, p) for p in assocClass.properties.values()
if 'key' in p.qualifiers or p.type == 'reference' ]
_strip_quals(keys)
model = pywbem.CIMInstance(classname=assocClass.classname,
properties=keys)
model.path = pywbem.CIMInstanceName(classname=assocClass.classname,
namespace=objectName.namespace)
for inst in self.references(env=env,
object_name=objectName,
model=model,
assoc_class=assocClass,
result_class_name=resultClassName,
role=role,
result_role=None,
keys_only=False):
for prop in inst.properties.values():
lpname = prop.name.lower()
if prop.type != 'reference':
continue
if role and role.lower() == lpname:
continue
if resultRole and resultRole.lower() != lpname:
continue
if _path_equals_ignore_host(prop.value, objectName):
continue
if resultClassName and self.filter_results and \
not pywbem.is_subclass(ch, objectName.namespace,
sub=prop.value.classname,
super=resultClassName):
continue
if prop.value.namespace is None:
prop.value.namespace = objectName.namespace
yield prop.value
logger.log_debug('CIMProvider MI_associatorNames returning')
def MI_references(self,
env,
objectName,
resultClassName,
role,
propertyList):
"""Return instances of an association class.
Implements the WBEM operation References in terms
of the references method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_references called. resultClass: %s' % (resultClassName))
ch = env.get_cimom_handle()
if not resultClassName:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
"Empty resultClassName passed to References")
assocClass = ch.GetClass(resultClassName, objectName.namespace,
LocalOnly=False,
IncludeQualifiers=True)
keyNames = get_keys_from_class(assocClass)
plist = None
if propertyList is not None:
lkns = [kn.lower() for kn in keyNames]
props = pywbem.NocaseDict()
plist = [s.lower() for s in propertyList]
pklist = plist + lkns
[props.__setitem__(p.name, p) for p in \
assocClass.properties.values() \
if p.name.lower() in pklist]
else:
props = assocClass.properties
_strip_quals(props)
model = pywbem.CIMInstance(classname=assocClass.classname,
properties=props)
model.path = pywbem.CIMInstanceName(classname=assocClass.classname,
namespace=objectName.namespace)
#if role is None:
# raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
# "** this shouldn't happen")
if role:
if role not in model.properties:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
"** this shouldn't happen")
model[role] = objectName
for inst in self.references(env=env,
object_name=objectName,
model=model,
assoc_class=assocClass,
result_class_name='',
role=role,
result_role=None,
keys_only=False):
inst.path = build_instance_name(inst, keyNames)
if self.filter_results and plist is not None:
inst = inst.copy()
filter_instance(inst, plist)
for prop in inst.properties.values():
if hasattr(prop.value, 'namespace') and prop.value.namespace is None:
prop.value.namespace = objectName.namespace
yield inst
logger.log_debug('CIMProvider MI_references returning')
def MI_referenceNames(self,
env,
objectName,
resultClassName,
role):
"""Return instance names of an association class.
Implements the WBEM operation ReferenceNames in terms
of the references method. A derived class will not normally
override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_referenceNames <2> called. resultClass: %s' % (resultClassName))
ch = env.get_cimom_handle()
if not resultClassName:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
"Empty resultClassName passed to ReferenceNames")
assocClass = ch.GetClass(resultClassName, objectName.namespace,
LocalOnly=False,
IncludeQualifiers=True)
keys = pywbem.NocaseDict()
keyNames = [p.name for p in assocClass.properties.values()
if 'key' in p.qualifiers]
for keyName in keyNames:
p = assocClass.properties[keyName]
keys.__setitem__(p.name, p)
_strip_quals(keys)
model = pywbem.CIMInstance(classname=assocClass.classname,
properties=keys)
model.path = pywbem.CIMInstanceName(classname=assocClass.classname,
namespace=objectName.namespace)
#if role is None:
# raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
# "** this shouldn't happen")
if role:
if role not in model.properties:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
"** this shouldn't happen")
model[role] = objectName
for inst in self.references(env=env,
object_name=objectName,
model=model,
assoc_class=assocClass,
result_class_name='',
role=role,
result_role=None,
keys_only=True):
for prop in inst.properties.values():
if hasattr(prop.value, 'namespace') and prop.value.namespace is None:
prop.value.namespace = objectName.namespace
yield build_instance_name(inst, keyNames)
logger.log_debug('CIMProvider MI_referenceNames returning')
def MI_invokeMethod(self, env, objectName, metaMethod, inputParams):
"""Invoke an extrinsic method.
Implements the InvokeMethod WBEM operation by calling the
method on a derived class called cim_method_<method_name>,
where <method_name> is the name of the CIM method, in all
lower case.
Arguments:
env -- Provider Environment (pycimmb.ProviderEnvironment)
objectName -- The InstanceName or ClassName of the object on
which the method is invoked.
metaMethod -- The CIMMethod representing the method to be
invoked.
inputParams -- A Dictionary where the key is the parameter name
and the value is the parameter value.
The return value for invokeMethod must be a tuple of size 2
where:
element 0 is a tuple of size 2 where element 0 is the return
data type name and element 1 is the actual data value.
element 1 is a dictionary where the key is the output
parameter name and the value is a tuple of size 2 where
element 0 is the data type name for the output parameter
and element 1 is the actual value of the output parameter.
A derived class will not normally override this method.
"""
logger = env.get_logger()
logger.log_debug('CIMProvider MI_invokeMethod called. method: %s:%s' \
% (objectName.classname,metaMethod.name))
lmethName = "cim_method_%s" % metaMethod.name.lower()
if hasattr(self, lmethName) :
method = getattr(self, lmethName)
new_inputs = dict([('param_%s' % k.lower(), v) \
for k, v in inputParams.items()])
(rval, outs) = method(env=env, object_name=objectName,
method=metaMethod, **new_inputs)
def add_type(v, _tp):
lv = v
if type(v) == list and len(v) > 0:
lv = v[0]
if isinstance(lv, pywbem.CIMClass):
tp = 'class'
elif isinstance(lv, pywbem.CIMInstance):
tp = 'instance'
elif isinstance(lv, pywbem.CIMInstanceName):
tp = 'reference'
elif v is None or (type(v) == list and len(v) == 0):
tp = _tp
else:
tp = pywbem.cimtype(v)
return (tp, v)
for k, v in outs.items():
if hasattr(v, 'namespace') and v.namespace is None:
v.namespace = objectName.namespace
outs[k] = add_type(v, metaMethod.parameters[k].type)
rval = add_type(rval, metaMethod.return_type)
rval = (rval, outs)
else:
raise pywbem.CIMError(pywbem.CIM_ERR_METHOD_NOT_FOUND,
"%s:%s"%(objectName.classname, metaMethod.name))
logger.log_debug('CIMProvider MI_invokeMethod returning')
return rval
def filter_instance(inst, plist):
"""Remove properties from an instance that aren't in the PropertyList
inst -- The CIMInstance
plist -- The property List, or None. The list items must be all
lowercase.
"""
if plist is not None: