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



83
84
85
# File 'lib/sparkle_formation/resources.rb', line 83

def base_key
  Bogo::Utility.snake(self.name.split('::').last) # rubocop:disable Style/RedundantSelf
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)


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

def load(json_path_or_hash)
  if(json_path_or_hash.is_a?(String))
    content = AttributeStruct.hashish.new(MultiJson.load(File.read(json_path_or_hash)))
  else
    content = json_path_or_hash
  end
  content.each do |type, hash|
    register(type, hash)
  end
  true
end

.load!TrueClass

Load the builtin AWS resources

Returns:

  • (TrueClass)


134
135
136
# File 'lib/sparkle_formation/resources.rb', line 134

def load!
  true
end

.lookup(key) ⇒ Hashish, NilClass

Registry information for given type

Parameters:

  • key (String, Symbol)

Returns:

  • (Hashish, NilClass)


187
188
189
# File 'lib/sparkle_formation/resources.rb', line 187

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)


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

def register(type, hash)
  unless(class_variable_defined?(:@@registry))
    @@registry = AttributeStruct.hashish.new
  end
  @@registry[base_key] ||= AttributeStruct.hashish.new
  @@registry[base_key][type] = hash
  true
end

.registryHashish

Returns currently loaded AWS registry.

Returns:

  • (Hashish)

    currently loaded AWS registry



192
193
194
195
196
197
# File 'lib/sparkle_formation/resources.rb', line 192

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)


142
143
144
145
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
# File 'lib/sparkle_formation/resources.rb', line 142

def registry_key(key)
  if(registry[key])
    result = key
  else
    o_key = key
    key = key.to_s.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

Resource information

Parameters:

  • identifier (String, Symbol)

    resource identifier

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

    specific data

Returns:

  • (Hashish)


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

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:



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

def resource_customizer(struct, lookup_key)
  struct
end

.resource_lookup(type) ⇒ Resource

Information about specific resource type

Parameters:

  • type (String)

    resource type

Returns:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/sparkle_formation/resources.rb', line 213

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



175
176
177
178
179
180
181
# File 'lib/sparkle_formation/resources.rb', line 175

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