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_preset_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_preset_hash, params) 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