lib/wbem/wsman.rb in wbem-0.3.0 vs lib/wbem/wsman.rb in wbem-0.5.0

- old
+ new

@@ -6,168 +6,17 @@ # Written by Klaus Kämpf <kkaempf@suse.de> # # Licensed under the MIT license # require "wbem/wbem" +require "wbem/wsman_instance" require "openwsman" +require "wbem/openwsman" +require "cim" -class AuthError < StandardError -end - -module Openwsman - class Transport - def Transport.auth_request_callback( client, auth_type ) - STDERR.puts "\t *** Transport.auth_request_callback" - return nil - end - end - # - # Capture Fault as Exception - # - class Exception < ::RuntimeError - def initialize fault - unless fault.is_a? Openwsman::Fault - raise "#{fault} is not a fault" unless fault.fault? - fault = Openwsman::Fault.new fault - end - @fault = fault - end - def to_s - "Fault code #{@fault.code}, subcode #{@fault.subcode}" + - "\n\treason #{@fault.reason}" + - "\n\tdetail #{@fault.detail}" - end - end - # - # Capture namespace, classname, and properties as ObjectPath - # - class ObjectPath - attr_reader :namespace, :classname, :properties - def initialize namespace, classname = nil, properties = {} - @namespace = namespace - @classname = classname - @properties = properties - end - end - # - # Provide Cim::ObjectPath like accessors for EndPointReference - # - class EndPointReference - alias :keys :selector_names - alias :key_count :selector_count - alias :add_key :add_selector - def each_key - keys.each { |key| yield key } - end - end - # - # Capture XmlDoc + WsmanClient as Instance - # - class Instance - def initialize node, client, epr_or_uri - @node = (node.is_a? Openwsman::XmlDoc) ? node.body : node - @epr = (epr_or_uri.is_a? Openwsman::EndPointReference) ? epr_or_uri : Openwsman::EndPointReference.new(epr_or_uri) - @client = client - end - # - # - # - def to_s - "Instance #{@client}\n\t#{@epr}\n\t#{@node.to_xml}" - end - # - # Instance#<property> - # Instance#<method>(<args>) - # - def method_missing name, *args - if args.empty? - # try property first - res = @node.send name - return res.text if res - end - # try as method invocation - options = Openwsman::ClientOptions.new - options.set_dump_request - selectors = {} - @epr.each do |k,v| - selectors[k] = v - end - options.selectors = selectors # instance key properties - uri = @epr.resource_uri - - # - # get method input parameter information - # - classname = @epr.classname - s = "mof/#{classname}" - begin - require s - rescue LoadError - raise RuntimeError.new "Cannot load #{s} for type information" - end - methods = MOF.class_eval "#{classname}::METHODS" - method = methods[name.to_s] - raise RuntimeError.new("Unknown method #{name} for #{classname}") unless method - result_type = method[:type] - parameters = method[:parameters] || {} - input = parameters[:in] - output = parameters[:out] - argsin = {} - i = 0 - if input - while i < input.size - value = args.shift - raise "Argument for #{input[i]} is nil, not allowed !" unless value - argsin[input[i]] = value - # FIXME more typecheck of args ? - i += 2 - end - end - STDERR.puts "\n\tproperties #{argsin.inspect}\n" - options.properties = argsin - # - # output parameters ? - # - argsout = nil - if output - if args.empty? - raise "Function #{name} has output arguments, please add an empty hash to the call" - end - argsout = args.shift - unless argsout.kind_of? Hash - raise "Function #{name} has output arguments, last argument must be a Hash" - end - unless args.empty? - raise "Function call to #{name} has excess arguments" - end - end - STDERR.puts "\n\targsout #{argsout.inspect}\n" -Openwsman.debug = -1 - STDERR.puts "\n\tinvoke #{uri}.#{name}\n" - res = @client.client.invoke(options, uri, name.to_s) - raise Openwsman::Exception.new(res) if res.fault? - STDERR.puts "\n\tresult #{res.to_xml}\n" - result = res.find(uri, "#{name}_OUTPUT").find(uri, "ReturnValue").text - case result_type - when :boolean - result == "true" ? true : false - when :uint8, :uint16, :uint32, :uint64 - result.to_i - when :string - result - when :float - result.to_f - when :datetime - else - raise "Unsupported result_type #{result_type.inspect}" - end - end - end -end - - module Wbem + class WsmanClient < WbemClient private # # create end point reference URI # @@ -219,17 +68,36 @@ raise Openwsman::Exception.new fault end # STDERR.puts "Return #{doc.to_xml}" doc end + # + # Get Wbem::Instance by EndPointReference + # + def get_by_epr epr + options = Openwsman::ClientOptions.new + options.set_dump_request if Wbem.debug +# puts "***\t@client.get_by_epr #{epr.GroupComponent}\n" + doc = @client.get_from_epr( options, epr ) + unless doc + raise RuntimeError.new "Identify failed: HTTP #{@client.response_code}, Err #{@client.last_error}:#{@client.fault_string}" + end + puts doc.to_xml if Wbem.debug + if doc.fault? + raise Openwsman::Fault.new(doc).to_s + end + klass = factory.class_for epr.classname + klass.new self, epr, doc + end + public attr_reader :client def initialize uri, auth_scheme = nil super uri, auth_scheme @url.path = "/wsman" if @url.path.nil? || @url.path.empty? -# Openwsman::debug = -1 + Openwsman::debug = -1 if Wbem.debug STDERR.puts "WsmanClient connecting to #{uri}, auth #{@auth_scheme.inspect}" if Wbem.debug @client = Openwsman::Client.new @url.to_s raise "Cannot create Openwsman client" unless @client @client.transport.timeout = 5 @@ -252,11 +120,11 @@ @client.transport.auth_method = Openwsman::GSSNEGOTIATE_AUTH_STR else raise "Unknown auth_scheme #{@auth_scheme.inspect}" end @options = Openwsman::ClientOptions.new - + # STDERR.puts "auth #{@auth_scheme.inspect} -> #{@client.transport.auth_method}" doc = _identify # STDERR.puts doc.to_xml @protocol_version = doc.ProtocolVersion.text @@ -354,11 +222,11 @@ break end items = result.Items rescue nil if items items.each do |inst| - yield Openwsman::Instance.new(inst, self, uri) + yield Wbem::Instance.new(self, uri, inst) end end context = result.context break unless context result = @client.pull( @options, nil, uri, context ) @@ -448,18 +316,17 @@ uri = namespace.resource_uri namespace = namespace.namespace else uri = epr_uri_for(namespace, classname) end - @options.flags = Openwsman::FLAG_ENUMERATION_ENUM_EPR # get EPRs - @options.flags = Openwsman::FLAG_ENUMERATION_OPTIMIZATION + @options.flags = Openwsman::FLAG_ENUMERATION_ENUM_EPR | Openwsman::FLAG_ENUMERATION_OPTIMIZATION @options.max_elements = 999 @options.cim_namespace = namespace if @product == :openwsman - @options.set_dump_request + @options.set_dump_request if Wbem.debug @options.selectors = properties unless properties.empty? start = Time.now - STDERR.puts "instance_names enumerate (#{uri})" + STDERR.puts "instance_names enumerate (#{uri})" if Wbem.debug result = @client.enumerate( @options, nil, uri ) names = [] loop do if _handle_fault @client, result break @@ -473,15 +340,38 @@ end context = result.context break unless context result = @client.pull( @options, nil, uri, context ) end - STDERR.puts "Enumerated #{names.size} items in #{Time.now-start} seconds" + STDERR.puts "Enumerated #{names.size} items in #{Time.now-start} seconds" if Wbem.debug return names end # + # get instance by reference + # + # call-seq + # get Openwsman::EndPointReference -> Wbem::Instance + # get EndPointReference-as-String -> Wbem::Instance + # get "ClassName", "key" => "value", :namespace => "root/interop" + # + def get instance_reference, keys = nil + if keys + uri = Openwsman.epr_uri_for "", instance_reference + instance_reference = Openwsman::EndPointReference.new(uri, nil, keys) + end + puts "@client.get #{instance_reference.class}..." if Wbem.debug + case instance_reference + when Openwsman::EndPointReference + get_by_epr instance_reference + when String + get_by_epr Openwsman::EndPointReference.new(instance_reference) + else + raise "Unsupported Wbem::get #{instance_reference.class}" + end + end + # # Return matching Wbem::Instance for first instance # matching namespace, classname, properties # @param namespace : String or Sfcc::Cim::ObjectPath # @param classname : String (optional) # @param properties : Hash (optional) @@ -501,16 +391,16 @@ uri = namespace.resource_uri namespace = namespace.namespace else uri = epr_uri_for(namespace, classname) end - @options.set_dump_request + @options.set_dump_request if Wbem.debug @options.cim_namespace = namespace if @product == :openwsman @options.selectors = properties unless properties.empty? - STDERR.puts "@client.get(namepace '#{@options.cim_namespace}', props #{properties.inspect}, uri #{uri}" + STDERR.puts "@client.get(namepace '#{@options.cim_namespace}', props #{properties.inspect}, uri #{uri}" if Wbem.debug res = @client.get(@options, uri) raise Openwsman::Exception.new res if res.fault? - Openwsman::Instance.new res, self, Openwsman::EndPointReference.new(uri, "", properties) + Wbem::Instance.new self, Openwsman::EndPointReference.new(uri, "", properties), res end -end -end # module +end # class WsmanClient +end # module Wbem