107 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require 'lib/string_helper'
 | |
| require 'db/mongo/models/stack_template/stack_template_factory'
 | |
| 
 | |
| module Devops
 | |
|   module StackTemplatePresets
 | |
|     class Base
 | |
| 
 | |
|       def id
 | |
|         StringHelper.underscore_class(self.class)
 | |
|       end
 | |
| 
 | |
|       def to_hash
 | |
|         {id: id, template_preset_body: template_preset_body}
 | |
|       end
 | |
| 
 | |
|       def build_template_from_preset(provider, stack_template_id, template_attrs)
 | |
|         stack_template_attrs = {
 | |
|           'id' => stack_template_id,
 | |
|           'provider' => provider,
 | |
|           'template_body' => serialize(actual_template_hash(template_attrs))
 | |
|         }
 | |
|         template_model = Model::StackTemplateFactory.create(provider, stack_template_attrs)
 | |
|       end
 | |
| 
 | |
|       def template_preset_body
 | |
|         @template_preset_body ||= begin
 | |
|           file_name = File.join("lib/stack_template_presets/#{id}.#{template_file_extension}")
 | |
|           File.read(file_name)
 | |
|         end
 | |
|       end
 | |
| 
 | |
| 
 | |
|       private
 | |
| 
 | |
|       def template_preset_hash
 | |
|         unserialize(template_preset_body)
 | |
|       end
 | |
| 
 | |
|       def actual_template_hash(template_attrs)
 | |
|         actual = template_preset_hash.dup
 | |
|         template_attrs.each do |key, value|
 | |
|           path = param_attrs_pathes_in_preset[key.to_s]
 | |
|           next unless path
 | |
|           insert_value_at(actual, value, path)
 | |
|         end
 | |
|         actual
 | |
|       end
 | |
| 
 | |
|       # returns hash with keys lists. Better to show an example:
 | |
|       # Imagine tha t template is the following:
 | |
|       # {
 | |
|       #   template: {
 | |
|       #     users_count: %users_count%
 | |
|       #   }
 | |
|       # }
 | |
|       #
 | |
|       # So param_attrs_pathes_in_preset will be
 | |
|       # {users_count: [:template, :users_count]}
 | |
|       def param_attrs_pathes_in_preset
 | |
|         {}
 | |
|       end
 | |
| 
 | |
|       # given hash = {t: {a: nil}}, value = 1, path = [:t, :a] returns
 | |
|       # {t: {a: 1}}
 | |
|       def insert_value_at(hash, value, path)
 | |
|         current = hash
 | |
|         path.each_with_index do |key, i|
 | |
|           if i == path.size - 1
 | |
|             current[key] = value
 | |
|           else
 | |
|             current = current[key]
 | |
|           end
 | |
|         end
 | |
|         hash
 | |
|       end
 | |
| 
 | |
|       # some templates may be YAML files
 | |
|       def template_file_extension
 | |
|         serialization_strategy.to_s
 | |
|       end
 | |
| 
 | |
|       # some templates may be YAML files
 | |
|       def serialize(hash)
 | |
|         case serialization_strategy
 | |
|         when :json
 | |
|           hash.to_json
 | |
|         else
 | |
|           raise
 | |
|         end
 | |
|       end
 | |
| 
 | |
|       def unserialize(text)
 | |
|         case serialization_strategy
 | |
|         when :json
 | |
|           JSON.parse(text)
 | |
|         else
 | |
|           raise
 | |
|         end
 | |
|       end
 | |
| 
 | |
|       def serialization_strategy
 | |
|         :json
 | |
|       end
 | |
| 
 | |
|     end
 | |
|   end
 | |
| end | 
