class Sfn::Callback::ParametersInfrastructure
Auto load stack parameters for infrastructure pattern
Constants
- VALID_EXTENSIONS
Valid file extensions for configuration file
Public Instance Methods
after_config_update(*_)
click to toggle source
Update configuration after configuration is loaded
@return [NilClass]
# File lib/sfn-parameters/infrastructure.rb, line 17 def after_config_update(*_) config[:parameters] ||= Smash.new config[:compile_parameters] ||= Smash.new config[:apply_stack] ||= [] config[:apply_mapping] ||= Smash.new config[:options][:tags] ||= Smash.new stack_name = arguments.first content = load_file_for(stack_name) process_information_hash(content, []) nil end
Also aliased as: after_config
Protected Instance Methods
extract_resolver_information(value)
click to toggle source
Extract resolver name and data from value object
@param value [Object] @return [Resolver, Object]
# File lib/sfn-parameters/infrastructure.rb, line 112 def extract_resolver_information(value) if value.is_a?(Hash) if value.size == 1 begin r_name, val = value.to_a.flatten resolver = load_resolver(Bogo::Utility.camel(r_name)) return resolver, val rescue NameError return nil, value end elsif value.to_smash.key?(:resolver) val = value.to_smash r_name = val.delete(:resolver) resolver = load_resolver(Bogo::Utility.camel(r_name)) return resolver, val else return nil, value end end return nil, value end
load_file_for(stack_name)
click to toggle source
Load the configuration file
@param stack_name [String] @return [Smash]
# File lib/sfn-parameters/infrastructure.rb, line 37 def load_file_for(stack_name) root_path = config.fetch(:sfn_parameters, :directory, "infrastructure") isolation_name = config.fetch( :sfn_parameters, :destination, ENV.fetch("SFN_PARAMETERS_DESTINATION", "default") ) paths = Dir.glob(File.join(root_path, "#{isolation_name}{#{VALID_EXTENSIONS.join(",")}}")).map(&:to_s) if paths.size > 1 raise ArgumentError.new "Multiple parameter file matches encountered! (#{paths.join(", ")})" elsif paths.empty? Smash.new else unlock_content(Bogo::Config.new(paths.first).data) end end
load_resolver(resolver_name)
click to toggle source
Load given resolver
@param resolver_name [String] @return [Resolver]
# File lib/sfn-parameters/infrastructure.rb, line 138 def load_resolver(resolver_name) memoize(resolver_name) do klass = SfnParameters::Resolver.detect_resolver(resolver_name) klass.new(config) end end
process_information_hash(hash, path = [])
click to toggle source
Process the given hash and set configuration values
@param hash [Hash] @param path [Array<String>] stack name hierarchy @return [TrueClass]
# File lib/sfn-parameters/infrastructure.rb, line 58 def process_information_hash(hash, path = []) if path.empty? && hash[:template] config[:file] = hash[:template] end hash.fetch(:parameters, {}).each do |key, value| key = [*path, key].compact.map(&:to_s).join("__") if current_value = config.get(:parameters, key) ui.debug "Not setting template parameter `#{key}`. Already set within config. (`#{current_value}`)" else config.set(:parameters, key, resolve(value)) end end hash.fetch(:compile_parameters, {}).each do |key, value| key = [*path, key].compact.map(&:to_s).join("__") if current_value = config.get(:compile_parameters, key) ui.debug "Not setting compile time parameter `#{key}`. Already set within config. (`#{current_value}`)" else config.set(:compile_parameters, key, resolve(value)) end end hash.fetch(:stacks, {}).each do |key, value| process_information_hash(value, [*path, key].compact) end hash.fetch(:mappings, {}).each do |key, value| value = [*path, Bogo::Utility.camel(value)].compact.map(&:to_s).join("__") config.set(:apply_mapping, key, value) end hash.fetch(:tags, {}).each do |key, value| config[:options].set(:tags, key, value) end hash.fetch(:apply_stacks, []).each do |s_name| config[:apply_stack] << s_name end config[:apply_stack].uniq! true end
resolve(value)
click to toggle source
Load value via resolver if defined
@param value [Object] @return [Object]
# File lib/sfn-parameters/infrastructure.rb, line 99 def resolve(value) resolver, value = extract_resolver_information(value) if resolver resolver.resolve(value) else value end end