Class: Wbem::CimxmlClient

Inherits:
WbemClient show all
Defined in:
lib/wbem/wbem.rb,
lib/wbem/cimxml.rb

Instance Attribute Summary

Attributes inherited from WbemClient

#auth_scheme, #product, #response, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WbemClient

#enumerate, #factory, #fault_string, #network_class_name, #networks, #processes, #profile_class_name, #profiles, #response_code, #service_class_name, #services, #storage_class_name, #storages, #system_class_name, #systems, #to_s

Constructor Details

#initialize(url, auth_scheme = nil) ⇒ CimxmlClient

Initialize a CIMXML client connection



94
95
96
97
98
# File 'lib/wbem/cimxml.rb', line 94

def initialize url, auth_scheme = nil
  super url, auth_scheme
  @client = Sfcc::Cim::Client.connect( { :uri => url, :verify => false } )
  @product = _identify
end

Class Method Details

.parse_object_path(s) ⇒ Object

parse ObjectPath to Sfcc::Cim::ObjectPath

  1. root/cimv2:Linux_ComputerSystem.CreationClassName=“Linux_ComputerSystem”,Name=“foo.bar”

  2. localhost:5989/root/cimv2:Linux_ComputerSystem.CreationClassName=“Linux_ComputerSystem”,Name=“heron.suse.de”



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

def self.parse_object_path s
  op = nil
  front, values = s.split(".",2)
  STDERR.puts "self.parse_object_path >#{front}<(#{values})"
  frontargs = front.split(":")
  if frontargs.size == 2
    # case 1 above
    namespace, classname = frontargs
    op = Sfcc::Cim::ObjectPath.new(namespace, classname)
  elsif frontargs.size == 3
    host, port_and_namespace, classname = frontargs
    port, namespace = port_and_namespace.split("/", 2)
    op = Sfcc::Cim::ObjectPath.new(namespace, classname)      
  else
    raise "CimxmlClient.parse_object_path: Can't parse >#{frontargs.inspect}<"
  end
  # add values
  while values && !values.empty?
    puts "Values >#{values.inspect}<"
    name, rest = values.split("=", 2)
    arg, values = if rest[0,1] == '"' # quoted arg
                    rest[1..-1].split(/\",?/, 2)
                  else # non-quoted arg
                    rest.split(",", 2)
                  end
    puts "name(#{name}), arg(#{arg})"
    op.add_key(name, arg)
  end
  puts "Op #{op}"
  op
end

Instance Method Details

#class_names(op, deep_inheritance = false) ⇒ Object

Return list of classnames for given object_path



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wbem/cimxml.rb', line 142

def class_names op, deep_inheritance = false
  ret = []
  unless op.is_a? Sfcc::Cim::ObjectPath
    op = Sfcc::Cim::ObjectPath.new(op.to_s, nil) # assume namespace
  end
  flags = deep_inheritance ? Sfcc::Flags::DeepInheritance : 0
  begin
    @client.class_names(op, flags).each do |name|
	ret << name.to_s
    end
  rescue Sfcc::Cim::ErrorInvalidNamespace
  end
  ret
end

#each_association(objectpath) ⇒ Object

Return associations for instance



249
250
251
252
253
254
255
256
# File 'lib/wbem/cimxml.rb', line 249

def each_association( objectpath )
  begin
    @client.associators(objectpath).each do |assoc|
      yield assoc
    end
  rescue Sfcc::Cim::ErrorInvalidClass, Sfcc::Cim::ErrorInvalidNamespace
  end
end

#each_instance(namespace_or_objectpath, classname = nil) ⇒ Object

Return instances for namespace and classname



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wbem/cimxml.rb', line 125

def each_instance( namespace_or_objectpath, classname = nil )
  op = if namespace_or_objectpath.is_a? Sfcc::Cim::ObjectPath
    namespace_or_objectpath
  else
    objectpath namespace_or_objectpath, classname
  end
  begin
    @client.instances(op).each do |inst|
      yield inst
    end
  rescue Sfcc::Cim::ErrorInvalidClass, Sfcc::Cim::ErrorInvalidNamespace
  end
end

#get(instance_reference, keys = nil) ⇒ Object

get instance by reference

call-seq

get Sfcc::Cim::ObjectPath -> Wbem::Instance
get ObjectPath-as-String -> Wbem::Instance
get "ClassName", "key" => "value", :namespace => "root/interop"


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/wbem/cimxml.rb', line 193

def get instance_reference, keys = nil
  if keys
    namespace = keys.delete(:namespace) || "root/cimv2"
    instance_reference = Sfcc::Cim::ObjectPath.new(namespace, instance_reference)
    keys.each do |k,v|
      instance_reference.add_key k, v
    end
  end
  puts "@client.get #{instance_reference.class}..." if Wbem.debug
  case instance_reference
  when Sfcc::Cim::ObjectPath
    get_by_objectpath instance_reference
  when String
    get_by_objectpath CimxmlClient.parse_object_path(instance_reference)
  else
    raise "Unsupported Wbem::get #{instance_reference.class}"
  end
end

#get_by_objectpath(objpath) ⇒ Object

get instance by objectpath



261
262
263
264
# File 'lib/wbem/cimxml.rb', line 261

def get_by_objectpath objpath
  STDERR.puts "#{self.class}.get_by_objectpath #{objpath}" if Wbem.debug
  @client.get_instance(objpath)
end

#get_class(namespace, classname = nil) ⇒ Object

get class



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/wbem/cimxml.rb', line 233

def get_class namespace, classname=nil
  objectpath = case namespace
               when Sfcc::Cim::ObjectPath
                 namespace
               when Sfcc::Cim::Instance
                 Sfcc::Cim::ObjectPath.new(namespace.namespace, namespace.classname)
               else
                 raise "Classname missing" unless classname
                 Sfcc::Cim::ObjectPath.new(namespace.to_s, classname.to_s)
               end
  @client.get_class objectpath
end

#get_instance(namespace, classname = nil, properties = {}) ⇒ Object

Return matching Wbem::Instance for first instance

matching namespace, classname, properties

Parameters:

  • namespace

    : String or Sfcc::Cim::ObjectPath

  • classname (defaults to: nil)

    : String (optional)

  • properties (defaults to: {})

    : Hash (optional)



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/wbem/cimxml.rb', line 218

def get_instance namespace, classname=nil, properties={}
  case namespace
  when Sfcc::Cim::ObjectPath
    objectpath = namespace
    namespace = objectpath.namespace
  else
    objectpath = objectpath namespace.to_s, classname, properties
  end
  ret = []
  @client.get_instance(objectpath)
end

#instance_names(namespace, classname = nil, properties = {}) ⇒ Object

Return list of Wbem::EndpointReference (object pathes) for instances

of namespace, classname

Parameters:

  • namespace

    : String or Sfcc::Cim::ObjectPath

  • classname (defaults to: nil)

    : String (optional)

  • properties (defaults to: {})

    : Hash (optional)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/wbem/cimxml.rb', line 164

def instance_names namespace, classname=nil, properties={}
  # if namespace is unset, sfcc will generate invalid XML
  namespace ||= "root/cimv2"
  case namespace
  when Sfcc::Cim::ObjectPath
    objectpath = namespace
    namespace = objectpath.namespace      
  else
    objectpath = objectpath namespace.to_s, classname, properties
  end
  ret = []
  begin
    @client.instance_names(objectpath).each do |path|
      path.namespace = namespace # add missing data
	ret << path
    end
  rescue Sfcc::Cim::ErrorInvalidClass, Sfcc::Cim::ErrorInvalidNamespace
  end
  ret
end

#namespacesObject

return list of namespaces



103
104
105
106
107
108
109
# File 'lib/wbem/cimxml.rb', line 103

def namespaces
  result = []
  each_instance( "root/interop", "CIM_Namespace" ) do |inst|
    result << inst.Name
  end
  result.uniq
end

#objectpath(namespace, classname = nil, properties = {}) ⇒ Object

Create ObjectPath from namespace, classname, and properties



114
115
116
117
118
119
120
# File 'lib/wbem/cimxml.rb', line 114

def objectpath namespace, classname = nil, properties = {}
  op = Sfcc::Cim::ObjectPath.new(namespace, classname, @client)
  properties.each do |k,v|
    op.add_key k,v
  end
  op
end