add methods for building template from preset on serverside

This commit is contained in:
Anton Chuchkalov 2015-06-18 18:53:03 +04:00
parent 61a607d298
commit dfb8c82469
3 changed files with 88 additions and 5 deletions

View File

@ -1,4 +1,5 @@
require 'lib/string_helper'
require 'db/mongo/models/stack_template/stack_template_factory'
module Devops
module StackTemplatePresets
@ -12,13 +13,59 @@ module Devops
{id: id, template_preset_body: template_preset_body}
end
def template_preset_body
file_name = File.join("lib/stack_template_presets/#{id}.json")
File.read(file_name)
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 build_template_from_preset(provider, options={})
# do smth
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

View File

@ -20,6 +20,21 @@ module Devops
}
end
def build_stack_template
lamda {
# check_privileges("stack_template_presets", "r")
check_privileges('stack_template', 'w')
attrs = create_object_from_json_body
provider = attrs.delete('provider')
stack_template_id = attrs.delete('stack_template_id')
stack_template = build_template_from_preset(provider, stack_template_id, attrs)
settings.mongo.stack_template_insert(template_model)
create_response 'Created', template_model.to_hash, 201
}
end
end
end
end

View File

@ -29,6 +29,27 @@ module Devops
#
app.get_with_headers "/stack_template_presets/:id", :headers => [:accept], &Devops::Version2_0::Handler::StackTemplatePreset.get_preset
# Build stack template from preset
#
# * *Request*
# - method : POST
# - headers :
# - Accept: application/json
# - params :
# - provider: string
# - stack_template_id: id of stack template to create
# - template_attrs: hash with template attributes
#
# * *Returns* : created stack template model
# {
# id: 'template id',
# provider: 'provider',
# template_body: 'long body'
# }
#
app.post_with_headers "/stack_template_presets/:id/build_stack_template", :headers => [:accept], &Devops::Version2_0::Handler::StackTemplatePreset.build_stack_template
puts "Stack template presets routes initialized"
end