Class: VSphereCloud::CloudSearcher

Inherits:
Object
  • Object
show all
Includes:
VimSdk
Defined in:
lib/cloud/vsphere/cloud_searcher.rb

Defined Under Namespace

Classes: MissingPropertiesException

Constant Summary collapse

PC =
Vmodl::Query::PropertyCollector

Constants included from VimSdk

VimSdk::BASE_VERSION, VimSdk::DYNAMIC_TYPES, VimSdk::SOAP_BODY_END, VimSdk::SOAP_BODY_START, VimSdk::SOAP_BODY_TAG, VimSdk::SOAP_END, VimSdk::SOAP_ENVELOPE_END, VimSdk::SOAP_ENVELOPE_START, VimSdk::SOAP_ENVELOPE_TAG, VimSdk::SOAP_FAULT_TAG, VimSdk::SOAP_HEADER_END, VimSdk::SOAP_HEADER_START, VimSdk::SOAP_HEADER_TAG, VimSdk::SOAP_NAMESPACE_MAP, VimSdk::SOAP_START, VimSdk::VERSION1, VimSdk::XMLNS_SOAPENC, VimSdk::XMLNS_SOAPENV, VimSdk::XMLNS_VMODL_BASE, VimSdk::XMLNS_XSD, VimSdk::XMLNS_XSI, VimSdk::XML_ENCODING, VimSdk::XML_HEADER

Instance Method Summary collapse

Constructor Details

#initialize(service_content, logger) ⇒ CloudSearcher

Returns a new instance of CloudSearcher.



11
12
13
14
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 11

def initialize(service_content, logger)
  @service_content = service_content
  @logger = logger
end

Instance Method Details

#get_all_properties(filter_spec) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 65

def get_all_properties(filter_spec)
  result = []
  retrieve_result = @service_content.property_collector.retrieve_properties_ex([filter_spec],
    PC::RetrieveOptions.new)
  until retrieve_result.nil?
    retrieve_result.objects.each { |object_content| result << object_content }
    break if retrieve_result.token.nil?
    retrieve_result = @service_content.property_collector.continue_retrieve_properties_ex(retrieve_result.token)
  end
  result
end

#get_managed_object(type, options) ⇒ Object



94
95
96
97
98
99
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 94

def get_managed_object(type, options)
  result = get_managed_objects(type, options)
  raise "Could not find #{type}: #{options.pretty_inspect}" if result.length == 0
  raise "Found more than one #{type}: #{options.pretty_inspect}" if result.length > 1
  result.first
end

#get_managed_objects(type, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 77

def get_managed_objects(type, options={})
  object_specs = get_object_specs(type, options[:root], 'name')

  result = []
  object_specs.each do |object_spec|
    name = object_spec.prop_set.first.val
    if options[:name].nil? || name == options[:name]
      if options[:include_name]
        result << [name , object_spec.obj]
      else
        result << object_spec.obj
      end
    end
  end
  result
end

#get_managed_objects_with_attribute(type, custom_field_key, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 101

def get_managed_objects_with_attribute(type, custom_field_key, options = {})
  object_specs = get_object_specs(type, options[:root], 'customValue')

  results = []
  object_specs.each do |object|

    catch(:found_object) do
      object.prop_set.each do |property|
        property.val.each do |property_value|

          if property_value.key == custom_field_key
            if options[:value].nil? || property_value.value == options[:value]
              results << object.obj
              throw :found_object
            end
          end

        end
      end
    end
  end

  results
end

#get_properties(obj, type, properties, options = {}) ⇒ Object



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
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 20

def get_properties(obj, type, properties, options = {})
  properties = [properties] if properties.kind_of?(String)
  property_specs = [PC::PropertySpec.new(:type => type, :all => false, :path_set => properties)]

  if obj.is_a?(Vmodl::ManagedObject)
    object_spec = PC::ObjectSpec.new(:obj => obj, :skip => false)
  else
    object_spec = obj.collect { |o| PC::ObjectSpec.new(:obj => o, :skip => false) }
  end

  filter_spec = PC::FilterSpec.new(:prop_set => property_specs, :object_set => object_spec)
  
  # Bosh::Common.retryable default has an exponential sleeper,
  # with 30 tries the timeout will be ~265s
  errors = [MissingPropertiesException]
  Bosh::Common.retryable(tries: 30, on: errors) do |tries, error|
    properties_response = get_all_properties(filter_spec)
    result = {}

    properties_response.each do |object_content|
      object_properties = {:obj => object_content.obj}
      if options[:ensure_all]
        remaining_properties = Set.new(properties)
      else
        remaining_properties = Set.new(options[:ensure])
      end
      if object_content.prop_set
        object_content.prop_set.each do |property|
          object_properties[property.name] = property.val
          remaining_properties.delete(property.name)
        end
      end
      unless remaining_properties.empty?
        raise MissingPropertiesException.new("The object[s] #{obj} " +
          "should have the following properties: #{properties.pretty_inspect}, " +
          "but they were missing these: #{remaining_properties.pretty_inspect}.")
      end
      result[object_content.obj] = object_properties
    end

    result = result.values.first if obj.is_a?(Vmodl::ManagedObject)
    result
  end
end

#get_property(obj, type, property, options = {}) ⇒ Object



16
17
18
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 16

def get_property(obj, type, property, options = {})
  get_properties(obj, type, property, options)[property]
end

#has_managed_object_with_attribute?(type, custom_field_key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cloud/vsphere/cloud_searcher.rb', line 126

def has_managed_object_with_attribute?(type, custom_field_key, options = {})
  object_specs = get_object_specs(type, options[:root], 'customValue')

  object_specs.each do |object|
    object.prop_set.each do |property|
      property.val.each do |property_value|
        if property_value.key == custom_field_key
          if options[:value].nil? || property_value.value == options[:value]
            return true
          end
        end
      end
    end
  end

  false
end