Class: WBEM::WBEMConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/wbem/cim_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, creds = nil, default_namespace = DEFAULT_NAMESPACE, x509 = nil) ⇒ WBEMConnection

Returns a new instance of WBEMConnection.



79
80
81
82
83
84
85
86
# File 'lib/wbem/cim_operations.rb', line 79

def initialize(url, creds = nil, default_namespace = DEFAULT_NAMESPACE,
               x509 = nil)
    @url = url
    @creds = creds
    @x509 = x509
    @last_request = @last_reply = ''
    @default_namespace = default_namespace
end

Instance Attribute Details

#credsObject (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def creds
  @creds
end

#default_namespaceObject (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def default_namespace
  @default_namespace
end

#last_raw_requestObject (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def last_raw_request
  @last_raw_request
end

#last_replyObject (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def last_reply
  @last_reply
end

#last_requestObject (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def last_request
  @last_request
end

#urlObject (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def url
  @url
end

#x509Object (readonly)

The caller may also register callback functions which are passed the request before it is sent, and the reply before it is unpacked. “”“



78
79
80
# File 'lib/wbem/cim_operations.rb', line 78

def x509
  @x509
end

Instance Method Details

#_add_objectname_param(params, object) ⇒ Object

Association provider API



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/wbem/cim_operations.rb', line 439

def _add_objectname_param(params, object)
    #"""Add an object name (either a class name or an instance
    #name) to a dictionary of parameter names."""
    
    if (object.is_a?(CIMClassName) or object.is_a?(CIMInstanceName))
        params[:ObjectName] = object
    elsif (object.is_a?(String))
        params[:ObjectName] = CIMClassName.new(object)
    else
        raise TypeError, "Expecting a classname, CIMClassName or CIMInstanceName object"
    end
    return params
end

#_map_association_params(params = {}) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/wbem/cim_operations.rb', line 453

def _map_association_params(params = {})
    #"""Convert various convenience parameters and types into their
    #correct form for passing to the imethodcall() function."""
    
    # ResultClass and Role parameters that are strings should be
    # mapped to CIMClassName objects.
    
    if (params.has_key?(:ResultClass) and params[:ResultClass].is_a?(String))
        params[:ResultClass] = CIMClassName.new(params[:ResultClass])
    end
    if (params.has_key?("AssocClass") and params["AssocClass"].is_a?(String))
        params[:AssocClass] = CIMClassName.new(params[:AssocClass])
    end
    return params
end

#AssociatorNames(object_name, params = {}) ⇒ Object



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/wbem/cim_operations.rb', line 485

def AssociatorNames(object_name, params = {})
    #"""Enumerate the names of CIM classes or instances that are
    #associated to a particular source CIM Object.  Pass a keyword
    #parameter of 'ClassName' to return associators for a CIM
    #class, pass 'InstanceName' to return the associators for a CIM
    #instance.  Returns a list of CIMInstanceName objects with the
    #host and namespace attributes set."""
    
    params = self._map_association_params(params)
    params = self._add_objectname_param(params, object_name)
    
    result = self.imethodcall("AssociatorNames",
                              params)
    return [] if result.nil?
    return result[2].collect { |x| x[2]}
end

#Associators(object_name, params = {}) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/wbem/cim_operations.rb', line 469

def Associators(object_name, params = {})
    #"""Enumerate CIM classes or instances that are associated to a
    #particular source CIM Object.  Pass a keyword parameter of
    #'ClassName' to return associators for a CIM class, pass
    #'InstanceName' to return the associators for a CIM instance."""
    
    params = self._map_association_params(params)
    params = self._add_objectname_param(params, object_name)
    
    result = self.imethodcall("Associators",
                              params)
    
    return [] if result.nil?
    return result[2].collect { |x| x[2]}
end

#CreateClass(newClass, params = {}) ⇒ Object



427
428
429
430
431
432
433
434
# File 'lib/wbem/cim_operations.rb', line 427

def CreateClass(newClass, params = {})
    #"""Create a CIM class."""

    # UNSUPPORTED

    self.imethodcall('CreateClass',
                     params.merge(Hash[:NewClass => newClass]))
end

#CreateInstance(newinstance, params = {}) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/wbem/cim_operations.rb', line 349

def CreateInstance(newinstance, params = {})
    #"""Create an instance.  Returns the name for the instance."""

    # Strip off path to avoid producing a VALUE.NAMEDINSTANCE
    # element instead of an INSTANCE element.

    instance = newinstance.clone
    instance.path = nil

    result = self.imethodcall("CreateInstance",
                              params.merge(Hash[:NewInstance => instance]))
    return result[2][0]
end

#DeleteClass(className, params = {}) ⇒ Object



409
410
411
412
413
414
415
416
# File 'lib/wbem/cim_operations.rb', line 409

def DeleteClass(className, params = {})
    #"""Delete a class by class name."""

    # UNSUPPORTED (but actually works)

    self.imethodcall("DeleteClass",
                     params.merge(Hash[:ClassName => CIMClassName.new(className)]))
end

#DeleteInstance(instancename, params = {}) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
# File 'lib/wbem/cim_operations.rb', line 337

def DeleteInstance(instancename, params = {})
    #"""Delete the instance given by instancename."""

    # Strip off host and namespace to make this a "local" object
    iname = instancename.clone
    iname.host = nil
    iname.namespace = nil

    self.imethodcall("DeleteInstance",
                     params.merge(Hash[:InstanceName => iname]))
end

#EnumerateClasses(params = {}) ⇒ Object



389
390
391
392
393
394
395
396
397
398
# File 'lib/wbem/cim_operations.rb', line 389

def EnumerateClasses(params = {})
    #"""Return a list of CIM class objects."""

    result = self.imethodcall("EnumerateClasses",
                              params)

    return [] if result.nil?

    return result[2]
end

#EnumerateClassNames(params = {}) ⇒ Object

Schema management API



379
380
381
382
383
384
385
386
387
# File 'lib/wbem/cim_operations.rb', line 379

def EnumerateClassNames(params = {})
    #"""Return a list of CIM class names. Names are returned as strings."""

    result = self.imethodcall("EnumerateClassNames",
                              params)
    
    return [] if result.nil?
    return result[2].collect { |x| x.classname}
end

#EnumerateInstanceNames(className, params = {}) ⇒ Object

Instance provider API



303
304
305
306
307
308
309
310
311
# File 'lib/wbem/cim_operations.rb', line 303

def EnumerateInstanceNames(className, params = {})
    #"""Enumerate instance names of a given classname.  Returns a
    #list of CIMInstanceName objects."""
    result = self.imethodcall("EnumerateInstanceNames",
                              params.merge(Hash[:ClassName => CIMClassName.new(className)]))

    return result[2] unless result.nil?
    return []
end

#EnumerateInstances(className, params = {}) ⇒ Object



313
314
315
316
317
318
319
320
321
# File 'lib/wbem/cim_operations.rb', line 313

def EnumerateInstances(className, params = {})
    #"""Enumerate instances of a given classname.  Returns a list
    #of CIMInstance objects."""

    result = self.imethodcall('EnumerateInstances',
                              params.merge(Hash[:ClassName => CIMClassName.new(className)]))
    return result[2] unless result.nil?
    return []
end

#GetClass(className, params = {}) ⇒ Object



400
401
402
403
404
405
406
407
# File 'lib/wbem/cim_operations.rb', line 400

def GetClass(className, params = {})
    #"""Return a CIMClass representing the named class."""
    
    result = self.imethodcall("GetClass",
                              params.merge(Hash[:ClassName => CIMClassName.new(className)]))
    
    return result[2][0]
end

#GetInstance(instancename, params = {}) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/wbem/cim_operations.rb', line 323

def GetInstance(instancename, params = {})
    #"""Fetch an instance given by instancename.  Returns a
    #CIMInstance object."""
    
    # Strip off host and namespace to make this a "local" object
    iname = instancename.clone
    iname.host = nil
    iname.namespace = nil

    result = self.imethodcall("GetInstance",
                              params.merge(Hash[:InstanceName => iname]))
    return result[2][0]
end

#imethodcall(methodname, params) ⇒ Object



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
# File 'lib/wbem/cim_operations.rb', line 92

def imethodcall(methodname, params)
    #"""Make an intrinsic method call.

    #Returns a tupletree with a IRETURNVALUE element at the root.
    #A CIMError exception is thrown if there was an error parsing
    #the call response, or an ERROR element was returned.
    
    #The parameters are automatically converted to the right
    #CIM_XML objects.
    
    #In general clients should call one of the method-specific
    #methods of the connection, such as EnumerateInstanceNames,
    #etc."""
    
    # If a LocalNamespacePath wasn't specified, use the default one

    localnamespacepath = params.delete(:LocalNamespacePath)
    localnamespacepath = self.default_namespace if localnamespacepath.nil?

    # Create HTTP headers
    
    headers = ["CIMOperation: MethodCall",
               "CIMMethod: #{methodname}",
               WBEM.get_object_header(localnamespacepath)]

    # Create parameter list
    plist = params.to_a.collect do |x|
        IPARAMVALUE.new(x[0].to_s, WBEM.tocimxml(x[1]))
    end

    # Build XML request
    
    req_xml = CIM.new(MESSAGE.new(SIMPLEREQ.new(IMETHODCALL.new(methodname,
                                                                LOCALNAMESPACEPATH.new(localnamespacepath.split("/").collect do |ns| 
                                                                                           NAMESPACE.new(ns) 
                                                                                       end
                                                                                       ),
                                                                plist)),
                                  '1001', '1.0'),
                      '2.0', '2.0')
    
    @last_raw_request = ""
    @last_request = ""
    req_xml.write(@last_raw_request)
    req_xml.write(@last_request, 2)
    # Get XML response

    begin
        resp_xml = WBEM.wbem_request(self.url, @last_raw_request, self.creds, 
                                     headers, 0, self.x509)
    rescue AuthError =>
        raise
    rescue CIMHttpError => arg
        # Convert cim_http exceptions to CIMError exceptions
        raise CIMError.new(0), arg.to_s
    end
    ## TODO: Perhaps only compute this if it's required?  Should not be
    ## all that expensive.
    
    reply_dom = REXML::Document.new(resp_xml)

    ## We want to not insert any newline characters, because
    ## they're already present and we don't want them duplicated.
    @last_reply = ""
    @last_raw_reply = ""
    reply_dom.write(@last_reply, 2)
    reply_dom.write(@last_raw_reply)
#            STDOUT << "response: #{@last_reply}\n"

    # Parse response
    tmptt = WBEM.dom_to_tupletree(reply_dom)
#            STDOUT << "tmp tt: #{WBEM.tupletree_to_s(tmptt)}\n"
    tt = WBEM.parse_cim(tmptt)

    if (tt[0] != "CIM")
        raise CIMError.new(0), "Expecting CIM element, got #{tt[0]}"
    end
    tt = tt[2]

    if (tt[0] != "MESSAGE")
        raise CIMError.new(0), "Expecting MESSAGE element, got #{tt[0]}"
    end
    tt = tt[2]

    if (tt.length != 1)
        raise CIMError.new(0), "Expecting one SIMPLERSP element: nelements: #{tt.length}"
    end
    if (tt[0][0] != "SIMPLERSP")
        raise CIMError.new(0), "Expecting one SIMPLERSP element, found #{tt[0][0]}"
    end
    tt = tt[0][2]

    if (tt[0] != "IMETHODRESPONSE")
        raise CIMError.new(0), "Expecting IMETHODRESPONSE element, got #{tt[0]}"
    end

    if (tt[1]["NAME"] != methodname)
        raise CIMError.new(0), "Expecting attribute NAME=#{methodname}, got #{tt[1]['NAME']}"
    end
    tt = tt[2]

    # At this point we either have a IRETURNVALUE, ERROR element
    # or None if there was no child nodes of the IMETHODRESPONSE
    # element.

    if (tt.nil?)
        return nil
    end
    if (tt[0] == "ERROR")
        code = tt[1]['CODE'].to_i
        if tt[1].has_key?("DESCRIPTION")
            raise CIMError.new(code), tt[1]["DESCRIPTION"]
        end
        raise CIMError.new(code), "Error code #{tt[1]['CODE']}"
        
        if (tt[0] != "IRETURNVALUE")
            raise CIMError,new(0), "Expecting IRETURNVALUE element, got #{tt[0]}"
        end
    end
    return tt
end

#InvokeMethod(methodname, objectname, params = {}) ⇒ Object

Method provider API



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
# File 'lib/wbem/cim_operations.rb', line 538

def InvokeMethod(methodname, objectname, params = {})
    
    obj = objectname.clone
    
    if (obj.is_a?(String))
        obj = CIMLocalClassPath.new(self.default_namespace, obj)
    end

    if obj.is_a?(CIMInstanceName) and obj.namespace.nil?
        obj.namespace = DEFAULT_NAMESPACE
    end

    result = self.methodcall(methodname, obj, params)
    
    # Convert the RETURNVALUE into a Ruby object
    if (!result.empty? and result[0][0] == "RETURNVALUE")
        returnvalue = tocimobj(result[0][1]["PARAMTYPE"],
                               result[0][2])
        
        # Convert output parameters into a dictionary of Python
        # objects.
        
        output_params = {}
        
        result[1..-1].each do |p|
            output_params[p[0]] = tocimobj(p[1], p[2])
        end
        return returnvalue, output_params
    else
        return nil, {}
    end
end

#methodcall(methodname, localobject, params) ⇒ Object



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
# File 'lib/wbem/cim_operations.rb', line 214

def methodcall(methodname, localobject, params)
    #"""Make an extrinsic method call.
    
    #Returns a tupletree with a RETURNVALUE element at the root.
    #A CIMError exception is thrown if there was an error parsing
    #the call response, or an ERROR element was returned.
    
    #The parameters are automatically converted to the right
    #CIM_XML objects."""
    
    # Create HTTP headers
    
    headers = ["CIMOperation: MethodCall",
               "CIMMethod: #{methodname}",
               WBEM.get_object_header(localobject)]
    # Create parameter list

    
    plist = params.to_a.collect do |x|
        PARAMVALUE.new(x[0].to_s, WBEM.tocimxml(x[1], true), WBEM.cimtype(x[1]))
    end

    # Build XML request

    req_xml = CIM.new(MESSAGE.new(SIMPLEREQ.new(METHODCALL.new(methodname,
                                                               localobject.tocimxml(),
                                                               plist)),
                                  '1001', '1.0'),
                      '2.0', '2.0')

    @last_raw_request = ""
    @last_request = ""
    req_xml.write(@last_raw_request)
    req_xml.write(@last_request, 2)

    # Get XML response

    begin
        resp_xml = WBEM.wbem_request(self.url, @last_raw_request, self.creds, 
                                     headers)
    rescue CIMHttpError => arg
        # Convert cim_http exceptions to CIMError exceptions
        raise CIMError.new(0), arg.to_s
    end

    @last_reply = resp_xml

    tt = WBEM.parse_cim(WBEM.xml_to_tupletree(resp_xml))

    if (tt[0] != "CIM")
        raise CIMError.new(0), "Expecting CIM element, got #{tt[0]}"
    end
    tt = tt[2]

    if (tt[0] != "MESSAGE")
        raise CIMError.new(0), "Expecting MESSAGE element, got #{tt[0]}"
    end
    tt = tt[2]

    if (tt.length != 1 or tt[0][0] != "SIMPLERSP")
        raise CIMError.new(0), "Expecting one SIMPLERSP element"
    end
    tt = tt[0][2]

    if (tt[0] != "METHODRESPONSE")
        raise CIMError.new(0), "Expecting METHODRESPONSE element, got #{tt[0]}"
    end

    if (tt[1]["NAME"] != methodname)
        raise CIMError.new(0), "Expecting attribute NAME=#{methodname}, got #{tt[1]['NAME']}"
    end
    tt = tt[2]

    # At this point we have an optional RETURNVALUE and zero or more PARAMVALUE
    # elements representing output parameters.
    if (!tt.empty? and tt[0][0] == "ERROR")
        code = tt[0][1]["CODE"].to_i
        if tt[0][1].has_key?("DESCRIPTION")
            raise CIMError.new(code), tt[0][1]['DESCRIPTION']
        end
        raise CIMError.new(code), "Error code #{tt[0][1]['CODE']}"
    end

    return tt
end

#ModifyClass(modifiedClass, params = {}) ⇒ Object



418
419
420
421
422
423
424
425
# File 'lib/wbem/cim_operations.rb', line 418

def ModifyClass(modifiedClass, params = {})
    #"""Modify a CIM class."""

    # UNSUPPORTED

    self.imethodcall('ModifyClass',
                     params.merge(Hash[:ModifiedClass => modifiedClass]))
end

#ModifyInstance(modifiedinstance, params = {}) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/wbem/cim_operations.rb', line 363

def ModifyInstance(modifiedinstance, params = {})
    #"""Modify properties of a named instance."""
    # last arg is hash

    if modifiedinstance.path.nil?
        raise ArgumentError, 'modifiedinstance parameter must have path attribute set'
    end

    return self.imethodcall("ModifyInstance",
                            params.merge(Hash[:ModifiedInstance => modifiedinstance]))
end

#ReferenceNames(object_name, params = {}) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/wbem/cim_operations.rb', line 518

def ReferenceNames(object_name, params = {})
    #"""Enumerate the name of association objects that refer to a
    #particular target CIM class or instance.  Pass a keyword
    #parameter of 'ClassName' to return associators for a CIM
    #class, pass 'InstanceName' to return the associators for a CIM
    #instance."""
    
    params = self._map_association_params(params)
    params = self._add_objectname_param(params, object_name)
    
    result = self.imethodcall("ReferenceNames",
                              params)
    return [] if result.nil?
    return result[2].collect { |x| x[2]}
end

#References(object_name, params = {}) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/wbem/cim_operations.rb', line 502

def References(object_name, params = {})
    #"""Enumerate the association objects that refer to a
    #particular target CIM class or instance.  Pass a keyword
    #parameter of 'ClassName' to return associators for a CIM
    #class, pass 'InstanceName' to return the associators for a CIM
    #instance."""
    
    params = self._map_association_params(params)
    params = self._add_objectname_param(params, object_name)
    
    result = self.imethodcall("References",
                              params)
    return [] if result.nil?
    return result[2].collect { |x| x[2]}
end

#to_sObject



88
89
90
# File 'lib/wbem/cim_operations.rb', line 88

def to_s
    "#{self.class}(#{self.url}, user=#{self.creds[0]})"
end