Class: SparkleFormation::Resources

Inherits:
Object
  • Object
show all
Extended by:
Utils::AnimalStrings
Defined in:
lib/sparkle_formation/resources.rb,
lib/sparkle_formation/resources/aws.rb,
lib/sparkle_formation/resources/heat.rb,
lib/sparkle_formation/resources/azure.rb,
lib/sparkle_formation/resources/google.rb,
lib/sparkle_formation/resources/rackspace.rb,
lib/sparkle_formation/resources/terraform.rb

Overview

Resources helper

Direct Known Subclasses

Aws, Azure, Google, Heat, Rackspace, Terraform

Defined Under Namespace

Classes: Aws, Azure, Google, Heat, Property, Rackspace, Resource, Terraform, UpdateCausesConditional

Constant Summary collapse

RESOURCE_TYPE_TR =

Characters to be removed from supplied key on matching

"_:"
RESOURCE_TYPE_NAMESPACE_SPLITTER =

String to split for resource namespacing

"::"
PROPERTY_UPDATE_CONDITIONALS =

Property update conditionals Format: Smash.new(RESOURCE_TYPE => => [PropertyConditional])

Smash.new

Class Method Summary collapse

Methods included from Utils::AnimalStrings

camel, snake

Class Method Details

.base_keyString

Returns base registry key.

Returns:

  • (String)

    base registry key



81
82
83
# File 'lib/sparkle_formation/resources.rb', line 81

def base_key
  Bogo::Utility.snake(self.name.split("::").last) # rubocop:disable Style/RedundantSelf
end

.key_loader(key) ⇒ Object

Called before key lookup to perform any required actions for customized setup/modifications



190
191
192
# File 'lib/sparkle_formation/resources.rb', line 190

def key_loader(key)
  true
end

.load(json_path_or_hash) ⇒ TrueClass

Register all discovered resources

Parameters:

  • json_path_or_hash (String, Hashish)

    path to files or hash

Returns:

  • (TrueClass)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sparkle_formation/resources.rb', line 120

def load(json_path_or_hash)
  case json_path_or_hash
  when String
    content = AttributeStruct.hashish.new(MultiJson.load(File.read(json_path_or_hash)))
  when Hash
    content = json_path_or_hash
  else
    raise TypeError.new("Expecting `String` or `Hash` type but received `#{json_path_or_hash.class}`")
  end
  content.each do |type, hash|
    register(type, hash)
  end
  true
end

.load!TrueClass

Load the builtin AWS resources

Returns:

  • (TrueClass)


138
139
140
# File 'lib/sparkle_formation/resources.rb', line 138

def load!
  true
end

.lookup(key) ⇒ Hashish, NilClass

Registry information for given type

Parameters:

  • key (String, Symbol)

Returns:

  • (Hashish, NilClass)


198
199
200
# File 'lib/sparkle_formation/resources.rb', line 198

def lookup(key)
  @@registry[base_key][key] || @@registry[base_key][registry_key(key)]
end

.register(type, hash) ⇒ TrueClass

Register resource

Parameters:

  • type (String)

    Orchestration resource type

  • hash (Hash)

    metadata information

Returns:

  • (TrueClass)


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sparkle_formation/resources.rb', line 90

def register(type, hash)
  unless hash.is_a?(Hash)
    raise TypeError.new("Expecting `Hash` type but received `#{hash.class}`")
  end
  unless class_variable_defined?(:@@registry)
    @@registry = AttributeStruct.hashish.new
  end
  @@registry[base_key] ||= AttributeStruct.hashish.new
  @@registry[base_key][type.to_s] = hash
  true
end

.registryHashish

Returns currently loaded AWS registry.

Returns:

  • (Hashish)

    currently loaded AWS registry



203
204
205
206
207
208
# File 'lib/sparkle_formation/resources.rb', line 203

def registry
  unless class_variable_defined?(:@@registry)
    @@registry = AttributeStruct.hashish.new
  end
  @@registry[base_key]
end

.registry_key(key) ⇒ String, NilClass

Discover registry key via part searching

Parameters:

  • key (String, Symbol)

Returns:

  • (String, NilClass)


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
# File 'lib/sparkle_formation/resources.rb', line 146

def registry_key(key)
  key_loader(key)
  if registry[key]
    result = key
  else
    o_key = key
    key = key.to_s.downcase.tr(self.const_get(:RESOURCE_TYPE_TR), "") # rubocop:disable Style/RedundantSelf
    snake_parts = nil
    result = @@registry[base_key].keys.detect do |ref|
      ref = ref.downcase
      snake_parts = ref.split(resource_type_splitter)
      until snake_parts.empty?
        break if snake_parts.join("") == key
        snake_parts.shift
      end
      !snake_parts.empty?
    end
    if result
      collisions = @@registry[base_key].keys.find_all do |ref|
        split_ref = ref.downcase.split(resource_type_splitter)
        ref = Array(split_ref.slice(split_ref.size - snake_parts.size, split_ref.size)).join("")
        key == ref
      end
      if collisions.size > 1
        raise ArgumentError.new "Ambiguous dynamic name returned multiple matches! " \
                                "`#{o_key.inspect}` -> #{collisions.sort.join(", ")}"
      end
    end
  end
  result
end

.resource(identifier, key = nil) ⇒ Hashish, NilClass

Resource information

Parameters:

  • identifier (String, Symbol)

    resource identifier

  • key (String, Symbol) (defaults to: nil)

    specific data

Returns:

  • (Hashish, NilClass)


107
108
109
110
111
112
113
114
# File 'lib/sparkle_formation/resources.rb', line 107

def resource(identifier, key = nil)
  res = lookup(identifier)
  if key && res
    res[key.to_sym]
  else
    res
  end
end

.resource_customizer(struct, lookup_key) ⇒ SparkleStruct

Simple hook method to allow resource customization if the specific provider requires/offers extra setup

Parameters:

Returns:



216
217
218
# File 'lib/sparkle_formation/resources.rb', line 216

def resource_customizer(struct, lookup_key)
  struct
end

.resource_lookup(type) ⇒ Resource

Information about specific resource type

Parameters:

  • type (String)

    resource type

Returns:



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sparkle_formation/resources.rb', line 224

def resource_lookup(type)
  result = registry[type]
  if result
    properties = result.fetch("full_properties", {}).map do |p_name, p_info|
      Property.new(p_name,
                   p_info[:description],
                   p_info[:type],
                   p_info[:required],
                   p_info[:update_causes],
                   self.const_get(:PROPERTY_UPDATE_CONDITIONALS).get(type, p_name))
    end
    Resource.new(type, properties, result)
  else
    raise KeyError.new "Failed to locate requested resource type: `#{type}`"
  end
end

.resource_type_splitterRegexp

rubocop:disable Style/RedundantSelf

Returns:

  • (Regexp)

    value for resource splitting



180
181
182
183
184
185
186
# File 'lib/sparkle_formation/resources.rb', line 180

def resource_type_splitter
  Regexp.new(
    [self.const_get(:RESOURCE_TYPE_NAMESPACE_SPLITTER)].flatten.compact.map { |value|
      Regexp.escape(value)
    }.join("|")
  )
end