Class: RightScale::MetadataSources::SelectiveMetadataSource

Inherits:
RightScale::MetadataSource show all
Defined in:
lib/clouds/metadata_sources/selective_metadata_source.rb

Overview

Provides metadata by attempting to get metadata from one or more listed sources; first responding successfully wins.

Instance Attribute Summary collapse

Attributes inherited from RightScale::MetadataSource

#logger

Instance Method Summary collapse

Methods inherited from RightScale::MetadataSource

#append_branch_name, #append_leaf_name

Constructor Details

#initialize(options) ⇒ SelectiveMetadataSource

Returns a new instance of SelectiveMetadataSource.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/clouds/metadata_sources/selective_metadata_source.rb', line 33

def initialize(options)
  raise ArgumentError, "options[:metadata_sources] is required" unless  = options[:metadata_source_types]
  raise ArgumentError, "options[:cloud] is required" unless @cloud = options[:cloud]
   = options[:select_metadata_override]

  # keep types but create selective sources on demand in case not all are used.
   = []
  .each do ||
     << { :type => , :source => nil }
  end
end

Instance Attribute Details

#metadata_sourcesObject

Returns the value of attribute metadata_sources.



31
32
33
# File 'lib/clouds/metadata_sources/selective_metadata_source.rb', line 31

def 
  
end

Instance Method Details

#finishObject

Attempts to finish all child metadata sources.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/clouds/metadata_sources/selective_metadata_source.rb', line 103

def finish
  last_exception = nil
  .each do ||
    begin
      source = [:source]
      source.finish if source
    rescue Exception => e
      last_exception = e
    ensure
      [:source] = nil
    end
  end
  raise last_exception if last_exception
end

#query(path) ⇒ Object

Queries for metadata using the given path.

Parameters

path(String)

metadata path

Return

metadata(String)

query result

Raises

QueryFailed

on any failure to query



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
# File 'lib/clouds/metadata_sources/selective_metadata_source.rb', line 55

def query(path)
   = ""
  last_query_failed = nil
  .each do ||
    type = [:type]
    unless source = [:source]
      # note that sources are special in that they ignore cloud vs. user
      # specialization unlike other dependency types.
      kind = :cloud_metadata
      source = @cloud.create_dependency_type(kind, :metadata_source, type)
      [:source] = source
    end
    begin
      query_result = source.query(path)
      selected = (path, type, query_result, )
       = selected[:merged_metadata]
      last_query_failed = nil  # reset last failed query
      break unless selected[:query_next_metadata]
    rescue QueryFailed => e
      # temporarily ignore failed query in case next source query succeeds
      last_query_failed = e
    end
  end
  raise last_query_failed if last_query_failed
  return 
rescue Exception => e
  raise QueryFailed.new(e.message)
end

#select_metadata(path, metadata_source_type, query_result, previous_metadata) ⇒ Object

Selects metadata by determining if the metadata condition has been satisfied. Supports merging of metadata (potentially in different formats) from different sources.

Parameters

path(String)

metadata path

metadata_source_type(String)

metadata source type

query_result(String)

raw metadata from query

previous_metadata(String)

previously merged metadata or empty

Returns

result(Boolean)

when true indicates that the next metadata source should also be queried.

result(String)

result of merging any previous metadata with current query result.



97
98
99
100
# File 'lib/clouds/metadata_sources/selective_metadata_source.rb', line 97

def (path, , query_result, )
  return .call(self, path, , query_result, ) if 
  return {:query_next_metadata => query_result.strip.empty?, :merged_metadata => query_result}
end