Module: WsmanInstance

Defined in:
lib/wbem/wsman_instance.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

method_missing

needs to be here to access TYPEMAP


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

def method_missing name, *args
#      STDERR.puts "Wbem::WsmanInstance.method_missing #{name}"
  # http://stackoverflow.com/questions/8960685/ruby-why-does-puts-call-to-ary
  raise NoMethodError if name == :to_ary
  name = name.to_s
  assign = false
  if name[-1,1] == '=' # assignment
    name = name[0...-1]
    assign = true
  end
  type = _typemap[name]
  if type.is_a? Array
    invoke(name, type, args)
  else
    return nil unless type # unknown property
    if assign
      # property assignment
      self[name] = Wbem::Conversion.from_ruby type, args[0]
    else
      # property read
      value = self[name]
      Wbem::Conversion.to_ruby type, value
    end
  end
end

Instance Method Details

#[](name) ⇒ Object

access attribute by name



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wbem/wsman_instance.rb', line 43

def [] name
  name = name.to_s
  node = @node.find(nil, name) rescue nil
  return nil unless node
  begin
    type = _typemap()[name]
  rescue
    raise "Property #{name} of #{self.class} has unknown type"
  end
#      puts "#{self.class}[#{name}]: #{node.name}<#{type.inspect}>"
  Wbem::Conversion.to_ruby type, node
end

#attributesObject

attribute iterator



34
35
36
37
38
39
# File 'lib/wbem/wsman_instance.rb', line 34

def attributes
  @node.each do |node|
#        STDERR.puts "Wsman::Instance #{node}"
    yield [ node.name, node.text ]
  end
end

#classnameObject



28
29
30
# File 'lib/wbem/wsman_instance.rb', line 28

def classname
  @epr.classname
end

#invoke(name, type, args) ⇒ Object

Instance#invoke

name => String
type => [ :uint32, 
          [ "RequestedState", :uint16, :in ],
          [ "Job", :class, :out ],
          [ "TimeoutPeriod", :dateTime, :in ],
        ]
args => Array


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

def invoke name, type, args
#      STDERR.puts "#{__FILE__}: #{self.class}#invoke #{name}<#{type}>(#{args.inspect})"
  result_type = type.shift
  argsin = {}
  argsout = {}
  loop do
    break if args.empty?
    if type.empty?
      raise "Excess argument '#{args.shift}' at pos #{argsin.size + argsout.size + 1} in call to #{self.class}.#{name}"
    end
    argname, argtype, direction = type.shift
    value = args.shift
    case direction
    when :in
      argsin[argname] = Wbem::Conversion.from_ruby( argtype, value )
    when :out
      unless value.nil? || value.is_a?(:symbol)
        raise "Argument '#{argname}' of #{self.class}.#{name} is 'out', pass nil to symbol instead of value"
      end
      argsout[argname] = value
    else
      raise "Arg #{argname} of #{self.class}.#{name} has bad direction #{direction.inspect}"
    end
  end
  STDERR.puts "\tproperties #{argsin.inspect}" if Wbem.debug
  STDERR.puts "\targsout #{argsout.inspect}" if Wbem.debug
  options = Openwsman::ClientOptions.new
  options.set_dump_request if Wbem.debug
  options.properties = argsin
  @epr.each do |k,v|
    options.add_selector( k, v )
  end
  STDERR.puts "\tinvoke" if Wbem.debug
  res = @client.client.invoke(options, @epr.resource_uri, name.to_s)
  raise "Invoke failed with: #{@client.fault_string}" unless res
  raise Openwsman::Exception.new(res) if res.fault?
  STDERR.puts "\n\tresult #{res.to_xml}\n" if Wbem.debug
  result = res.find(uri, "#{name}_OUTPUT").find(uri, "ReturnValue").text
  Wbem::Conversion.to_ruby result_type, result
end

#to_sObject

to_s - stringify



58
59
60
61
62
63
64
65
66
67
# File 'lib/wbem/wsman_instance.rb', line 58

def to_s
  s = "#{@epr.classname}\n"
  @node.each do |child|
    s << "\t" << child.name << ": " 
    v = self[child.name]
    s << v.to_s if v
    s << "\n"
  end
  s
end

#wsman_initialize(client, epr_or_uri, node) ⇒ Object

Class constructor

client - client instance, used for method invocation
         either a Wbem::CimxmlClient
         or a Wbem::WsmanClient

instance_ref - instance reference
               either ObjectPath (Wbem::CimxmlClient) or
               EndPointReference (Wbem::WsmanClient)

instance_data - instance data
                Wbem::WsmanClient: Openwsman::XmlNode
                Wbem::CimxmlClient: nil (instance_ref has all information)


21
22
23
24
25
26
27
# File 'lib/wbem/wsman_instance.rb', line 21

def wsman_initialize client, epr_or_uri, node
#      STDERR.puts "Wbem::WsmanInstance.new epr_or_uri #{epr_or_uri}"
  @node = node.body.child rescue node
#      STDERR.puts "Wsman::Instance.new @node #{@node.class}"
  @epr = (epr_or_uri.is_a? Openwsman::EndPointReference) ? epr_or_uri : Openwsman::EndPointReference.new(epr_or_uri) if epr_or_uri
  @client = client
end