Class: VmodlHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/tasks/vmodl_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vmodl_path:, wsdl_path:) ⇒ VmodlHelper

Returns a new instance of VmodlHelper.



42
43
44
45
46
47
48
# File 'lib/tasks/vmodl_helper.rb', line 42

def initialize(vmodl_path:, wsdl_path:)
  @vmodl_path = Pathname.new(vmodl_path)
  @wsdl_path  = Pathname.new(wsdl_path)

  @vmodl = load_vmodl(@vmodl_path)
  @wsdl  = load_wsdl(@wsdl_path)
end

Class Method Details

.generate!Object



17
18
19
# File 'lib/tasks/vmodl_helper.rb', line 17

def generate!
  run!('generate')
end

.verify!Object



13
14
15
# File 'lib/tasks/vmodl_helper.rb', line 13

def verify!
  run!('verify')
end

Instance Method Details

#generate!Object



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
# File 'lib/tasks/vmodl_helper.rb', line 98

def generate!
  wsdl_classes_by_name.each_value do |type|
    type_name = type.name.name

    # Update the existing vmodl object if it exists otherwise instantiate a
    # new one.
    vmodl_data = @vmodl[type_name] || build_wsdl_class!(type)
    props_by_name = vmodl_data['props'].index_by { |prop| prop['name'] }

    vmodl_data['props'] = wsdl_properties(type).map do |element|
      # NOTE we should prioritize the existing property hash over building a
      # new one because the generic ManagedObjectReferences are manually
      # replaced with their specific counterparts (e.g. Datastore) which
      # cannot be computed programmatically.
      props_by_name[element.name.name] || build_vmodl_property(element)
    end

    # Some types (e.g. ManagedObjectReference) have extra properties which are
    # not present in the WSDL but are required for proper function of RbVmomi.
    vmodl_data['props'] += extra_props_for_type(type_name)
  end

  wsdl_enums_by_name.each_value do |enum|
    enum_name = enum.name.name

    vmodl_data = @vmodl[enum_name] || build_wsdl_enum(enum)
    vmodl_data['values'] = enum.restriction.enumeration
  end

  wsdl_classes_by_name.each_value do |type|
    type_name = type.name.name
    vmodl_data = @vmodl[type_name]

    elements_by_name = type.elements.index_by { |e| e.name.name }

    # Loop through the properties defined in the vmodl.db for this type and
    # compare the type to that property as defined in the wsdl.
    vmodl_data['props'].each do |vmodl_prop|
      wsdl_prop = elements_by_name[vmodl_prop['name']]
      next if wsdl_prop.nil?

      vmodl_klass = wsdl_constantize(vmodl_prop['wsdl_type'])
      wsdl_klass  = wsdl_constantize(wsdl_prop.type.source)

      vmodl_prop['wsdl_type'] = wsdl_klass.wsdl_name unless vmodl_klass <= wsdl_klass
    end
  end

  dump_vmodl!
end

#verify!Object



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
# File 'lib/tasks/vmodl_helper.rb', line 50

def verify!
  # Loop through the ComplexTypes in the WSDL and compare their types
  # to the types which are defined in the vmodl.db
  wsdl_classes_by_name.each_value do |type|
    type_name  = type.name.name
    vmodl_data = @vmodl[type_name]

    # If a type exists in the WSDL but not in the vmodl.db this usually
    # indicates that it was added in a newer version than the current
    # vmodl.db supports.
    #
    # Print a warning that the type is missing and skip it.
    if vmodl_data.nil?
      puts " class #{type_name} is missing"
      next
    end

    # Index the properties by name to make it simpler to find later
    elements_by_name = type.elements.index_by { |e| e.name.name }

    # Loop through the properties defined in the vmodl.db for this type and
    # compare the type to that property as defined in the wsdl.
    vmodl_data['props'].each do |vmodl_prop|
      wsdl_prop = elements_by_name[vmodl_prop['name']]
      next if wsdl_prop.nil?

      vmodl_klass = wsdl_constantize(vmodl_prop['wsdl_type'])
      wsdl_klass  = wsdl_constantize(wsdl_prop.type.source)

      # The vmodl class should be equal to or a subclass of the one in the wsdl.
      # Example of a subclass is e.g. VirtualMachine.host is defined as a HostSystem
      # in the vmodl.db but it is a ManagedObjectReference in the wsdl.
      puts "#{type_name}.#{vmodl_prop["name"]} #{wsdl_klass.wsdl_name} doesn't match #{vmodl_klass.wsdl_name}" unless vmodl_klass <= wsdl_klass
    end
  end

  # Loop through the SimpleTypes (enums) in the WSDL
  wsdl_enums_by_name.each_value do |enum|
    enum_name = enum.name.name
    vmodl_data = @vmodl[enum_name]

    if vmodl_data.nil?
      puts " enum #{enum_name} is missing"
      next
    end
  end
end