Class: RightScale::MetadataTreeClimber

Inherits:
Object
  • Object
show all
Defined in:
lib/clouds/metadata_tree_climber.rb

Overview

Provides rules for building a standard metadata tree from raw data and expanding multi-line leaf values into arrays. Details of querying raw metadata are external to this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MetadataTreeClimber

Initializer.

Parameters

options(Class)

Hash-like class to use when building

 tree (defaults to Hash)

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clouds/metadata_tree_climber.rb', line 39

def initialize(options = {})
  # parameters
  raise ArgumentError.new("options[:root_path] is required") unless @root_path = options[:root_path]
  raise ArgumentError.new("options[:user_metadata_root_path] is required") unless  = options[:user_metadata_root_path]
  @tree_class = options[:tree_class] || Hash
  @child_name_delimiter = options[:child_name_delimiter] || "\n"

  # callbacks (optional)
  @branch_key_override = options[:branch_key_override]
  @child_names_override = options[:child_names_override]
  @create_leaf_override = options[:create_leaf_override]
  @has_children_override = options[:has_children_override]
  @leaf_key_override = options[:leaf_key_override]
end

Instance Attribute Details

#child_name_delimiterObject

Returns the value of attribute child_name_delimiter.



32
33
34
# File 'lib/clouds/metadata_tree_climber.rb', line 32

def child_name_delimiter
  @child_name_delimiter
end

#root_pathObject

Returns the value of attribute root_path.



32
33
34
# File 'lib/clouds/metadata_tree_climber.rb', line 32

def root_path
  @root_path
end

#tree_classObject

Returns the value of attribute tree_class.



32
33
34
# File 'lib/clouds/metadata_tree_climber.rb', line 32

def tree_class
  @tree_class
end

Instance Method Details

#branch_key(branch_name) ⇒ Object

Looks for equals anywhere in the sub-value or trailing forward slash at the end of the sub-value.

Parameters

branch_name(String)

branch name

Returns

key(String)

branch key or nil



120
121
122
123
124
125
126
127
# File 'lib/clouds/metadata_tree_climber.rb', line 120

def branch_key(branch_name)
  return @branch_key_override.call(self, branch_name) if (branch_name && @branch_key_override)

  # replace any equals and subsequent text with forward slash and chomp any
  # trailing slash. note that chomp! returns nil if character (i.e. slash)
  # is not found at end.
  return branch_name.gsub(/=.*$/, '/').gsub('-', '_').chomp!('/')
end

#child_names(path, query_result) ⇒ Object

Determines if the given raw value contains branch/leaf names and returns them as pair of Hash-like objects of keys with nil values.

Parameters

path(String)

path to metadata

query_result(String)

raw data queried using path

Returns

result(Array)

array of child (leaf or branch) names or empty



81
82
83
84
85
86
87
88
89
90
# File 'lib/clouds/metadata_tree_climber.rb', line 81

def child_names(path, query_result)
  return @child_names_override.call(self, path, query_result) if @child_names_override
  child_names = []
  sub_values = query_result.gsub("\r\n", "\n").split(@child_name_delimiter)
  sub_values.each do |sub_value|
    sub_value.strip!
    (child_names << sub_value) unless sub_value.empty?
  end
  return child_names
end

#create_branchObject

Creates a new empty tree node based on the class specified in options.

Returns

result(Hash)

new Hash-like node



96
97
98
# File 'lib/clouds/metadata_tree_climber.rb', line 96

def create_branch
  @tree_class.new
end

#create_leaf(path, data) ⇒ Object

Creates a new leaf using the given information (from which leaf type can be inferred, etc.).

Parameters

path(String)

path to metadata

data(String)

raw data queried using path



107
108
109
110
# File 'lib/clouds/metadata_tree_climber.rb', line 107

def create_leaf(path, data)
  return @create_leaf_override.call(self, data) if @create_leaf_override
  return data.to_s.strip
end

#has_children?(path, query_result) ⇒ Boolean

Determines if the given path represents a parent branch. This information can be empirically determined from the path by the appearance of a trailing forward slash using the HTTP convention (even if metadata source is not an HTTP connection) except for the root which must be tested specially.

Parameters

path(String)

path to metadata

query_result(String)

raw data queried using path

Return

result(Boolean)

true if branch, false if leaf

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/clouds/metadata_tree_climber.rb', line 66

def has_children?(path, query_result)
  return @has_children_override.call(self, path, query_result) if @has_children_override
  # default behavior for user metadata is flat; currently no known use cases of hierarchical user metadata.
  return ( != @root_path) && ('/' == path[-1..-1] || path == @root_path)
end

#leaf_key(leaf_name) ⇒ Object

Leaf name is used as key by default.

Parameters

leaf_name(String)

leaf name

Returns

key(String)

leaf key or nil



136
137
138
139
140
# File 'lib/clouds/metadata_tree_climber.rb', line 136

def leaf_key(leaf_name)
  return @leaf_key_override.call(self, leaf_name) if @leaf_key_override
  # replace '-' and '/' with '_' so that metadata keys are Mash friendly
  return leaf_name.gsub('-', '_')
end