Class: Openwsman::Instance

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

Overview

Capture XmlDoc + WsmanClient as Instance

Instance Method Summary collapse

Constructor Details

#initialize(node, client, epr_or_uri) ⇒ Instance

Returns a new instance of Instance.



66
67
68
69
70
# File 'lib/wbem/wsman.rb', line 66

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Instance#<property> Instance#<method>(<args>)

Raises:

  • (RuntimeError)


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

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

Instance Method Details

#to_sObject



74
75
76
# File 'lib/wbem/wsman.rb', line 74

def to_s
  "Instance #{@client}\n\t#{@epr}\n\t#{@node.to_xml}"
end