File: basic_types.rb

package info (click to toggle)
ruby-rbvmomi 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,756 kB
  • sloc: ruby: 5,590; sh: 36; makefile: 7
file content (375 lines) | stat: -rw-r--r-- 6,943 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
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
213
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Copyright (c) 2010 VMware, Inc.  All Rights Reserved.
require 'pp'
require 'set'

module RbVmomi
module BasicTypes

BUILTIN = Set.new %w(ManagedObject DataObject TypeName PropertyPath ManagedObjectReference MethodName MethodFault LocalizedMethodFault KeyValue)

class Base
  class << self
    attr_accessor :wsdl_name

    def init wsdl_name=self.name
      @wsdl_name = wsdl_name
    end

    def to_s
      @wsdl_name
    end
  end

  init
end

class ObjectWithProperties < Base
  class << self
    attr_accessor :props_desc

    def init name=self.name, props=[]
      super name
      @props_desc = props
      @props_desc.each do |d|
        sym = d['name'].to_sym
        define_method(sym) { _get_property sym }
        define_method(:"#{sym}=") { |x| _set_property sym, x }
      end
    end

    def full_props_set
      @full_props_set ||= Set.new(full_props_desc.map { |x| x['name'] })
    end

    def full_props_desc
      @full_props_desc ||= (self == ObjectWithProperties ? [] : superclass.full_props_desc) + props_desc
    end

    def find_prop_desc name
      full_props_desc.find { |x| x['name'] == name.to_s }
    end
  end

  def _get_property sym
    fail 'unimplemented'
  end

  def _set_property sym, val
    fail 'unimplemented'
  end

  init
end

class ObjectWithMethods < ObjectWithProperties
  class << self
    attr_accessor :methods_desc

    def init name=self.name, props=[], methods={}
      super name, props
      @methods_desc = methods

      @methods_desc.each do |k,d|
        sym = k.to_sym
        define_method(sym) { |*args| _call sym, *args }
        define_method(:"#{sym}!") { |*args| _call sym, *args }
      end
    end

    # XXX cache
    def full_methods_desc
      (self == ObjectWithMethods ? {} : superclass.full_methods_desc).merge methods_desc
    end
  end

  init
end

class DataObject < ObjectWithProperties
  attr_reader :props

  def self.kind; :data end

  def initialize props={}
    # Deserialization fast path
    if props == nil
      @props = {}
      return
    end

    @props = Hash[props.map { |k,v| [k.to_sym, v] }]
    #self.class.full_props_desc.each do |desc|
      #fail "missing required property #{desc['name'].inspect} of #{self.class.wsdl_name}" if @props[desc['name'].to_sym].nil? and not desc['is-optional']
    #end
    @props.each do |k,v|
      fail "unexpected property name #{k}" unless self.class.find_prop_desc(k)
    end
  end

  def initialize_copy(source)  
    super  
    @props = @props.dup  
  end 

  def _get_property sym
    @props[sym]
  end

  def [] sym
    _get_property sym
  end

  def _set_property sym, val
    @props[sym] = val
  end

  def []= sym, val
    _set_property sym, val
  end

  def == o
    return false unless o.class == self.class
    keys = (props.keys + o.props.keys).uniq
    keys.all? { |k| props[k] == o.props[k] }
  end

  alias eql? ==

  def hash
    props.hash
  end

  def pretty_print q
    q.text self.class.wsdl_name
    q.group 2 do
      q.text '('
      q.breakable
      props = @props.sort_by { |k,v| k.to_s }
      q.seplist props, nil, :each do |e|
        k, v = e
        q.group do
          q.text k.to_s
          q.text ': '
          q.pp v
        end
      end
    end
    q.breakable
    q.text ')'
  end

  init
end

class ManagedObject < ObjectWithMethods
  def self.kind; :managed end

  def initialize connection, ref
    super()
    @connection = connection
    @soap = @connection # XXX deprecated
    @ref = ref
  end

  def _connection
    @connection
  end

  def _ref
    @ref
  end

  def _get_property sym
    ret = @connection.propertyCollector.RetrieveProperties(:specSet => [{
      :propSet => [{ :type => self.class.wsdl_name, :pathSet => [sym.to_s] }],
      :objectSet => [{ :obj => self }],
    }])[0]

    if !ret
      return nil
    elsif ret.propSet.empty?
      return nil if ret.missingSet.empty?
      raise ret.missingSet[0].fault
    else
      ret.propSet[0].val
    end
  end

  def _set_property sym, val
    fail 'unimplemented'
  end

  def _call method, o={}
    fail "parameters must be passed as a hash" unless o.is_a? Hash
    desc = self.class.full_methods_desc[method.to_s] or fail "unknown method"
    @connection.call method, desc, self, o
  end

  def to_s
    "#{self.class.wsdl_name}(#{@ref.inspect})"
  end

  def pretty_print pp
    pp.text to_s
  end

  def [] k
    _get_property k
  end

  def == x
    out = (x.class == self.class && x._ref == @ref) 
    out = (x._connection.instanceUuid == self._connection.instanceUuid) if out && x._connection.host
    out
  end

  alias eql? ==

  def hash
    [self.class, @ref].hash
  end

  init 'ManagedObject'
end

class Enum < Base
  class << self
    attr_accessor :values

    def init name=self.name, values=[]
      super name
      @values = values
    end
  end

  def self.kind; :enum end

  attr_reader :value

  def initialize value
    @value = value
  end

  init
end

class MethodFault < DataObject
  init 'MethodFault', [
    {
      'name' => 'faultCause',
      'wsdl_type' => 'LocalizedMethodFault',
      'is-array' => false,
      'is-optional' => true,
    }, {
      'name' => 'faultMessage',
      'wsdl_type' => 'LocalizableMessage',
      'is-array' => true,
      'is-optional' => true,
    },
  ]

  def self.=== exn
    exn.class == RbVmomi::Fault and self <= exn.fault.class
  end
end

class LocalizedMethodFault < DataObject
  init 'LocalizedMethodFault', [
    {
      'name' => 'fault',
      'wsdl_type' => 'MethodFault',
      'is-array' => false,
      'is-optional' => false,
    }, {
      'name' => 'localizedMessage',
      'wsdl_type' => 'xsd:string',
      'is-array' => false,
      'is-optional' => true,
    },
  ]

  def exception
    RbVmomi::Fault.new(self.localizedMessage, self.fault)
  end
end

class RuntimeFault < MethodFault
  init
end

class MethodName < String
  def self.wsdl_name; 'MethodName' end
end

class PropertyPath < String
  def self.wsdl_name; 'PropertyPath' end
end

class TypeName < String
  def self.wsdl_name; 'TypeName' end
end

class ManagedObjectReference
  def self.wsdl_name; 'ManagedObjectReference' end
end

class Boolean
  def self.wsdl_name; 'xsd:boolean' end
end

class AnyType
  def self.wsdl_name; 'xsd:anyType' end
end

class Binary
  def self.wsdl_name; 'xsd:base64Binary' end
end

class ::Class
  def wsdl_name; self.class.name end
end

class ::String
  def self.wsdl_name; 'xsd:string' end
end

class ::Integer
  def self.wsdl_name; 'xsd:int' end
end

class ::Float
  def self.wsdl_name; 'xsd:float' end
end

class Int
  def self.wsdl_name; 'xsd:int' end
  
  def initialize x
    @val = x
  end
  
  def to_s
    @val.to_s
  end
end

class KeyValue
  def self.wsdl_name; 'KeyValue' end
  attr_accessor :key, :value

  def initialize k, v
    @key = k
    @value = v
  end

  def [] i
    if i == 0 then @key
    elsif i == 1 then @value
    else fail "invalid index #{i.inspect}"
    end
  end
end


end
end