parse stack template available parameters

This commit is contained in:
Anton Chuchkalov 2016-01-28 18:15:06 +03:00
parent 53ce4e266b
commit 9026fdf704
9 changed files with 97 additions and 24 deletions

View File

@ -28,6 +28,8 @@ class StackTemplate < Handler
delete_handler delete_handler
when :update_url when :update_url
update_url_handler update_url_handler
when :update_available_parameters
update_available_parameters_handler
end end
end end
@ -87,6 +89,16 @@ class StackTemplate < Handler
puts stack_template['template_url'] puts stack_template['template_url']
end end
def update_available_parameters_handler
r = inspect_parameters @options_parser.update_url_params, @args[2]
unless r.nil?
@options_parser.invalid_update_url_command
abort(r)
end
stack_template = post "/stack_template/#{@args[2]}/update_available_parameters"
puts JSON.pretty_generate(stack_template['available_parameters'])
end
def provider_stack_templates(provider) def provider_stack_templates(provider)
if Providers.has_functionality?(provider, :stack_templates) if Providers.has_functionality?(provider, :stack_templates)
@provider = true @provider = true

View File

@ -2,7 +2,7 @@ require "devops-client/options/common_options"
class StackTemplateOptions < CommonOptions class StackTemplateOptions < CommonOptions
commands :create, :delete, :list, :show, :update_url commands :create, :delete, :list, :show, :update_url, :update_available_parameters
def initialize args, def_options def initialize args, def_options
super(args, def_options) super(args, def_options)
@ -12,6 +12,7 @@ class StackTemplateOptions < CommonOptions
self.show_params = ["STACK_TEMPLATE"] self.show_params = ["STACK_TEMPLATE"]
self.delete_params = ["STACK_TEMPLATE"] self.delete_params = ["STACK_TEMPLATE"]
self.update_url_params = ["STACK_TEMPLATE"] self.update_url_params = ["STACK_TEMPLATE"]
self.update_available_parameters_params = ["STACK_TEMPLATE"]
end end
def create_options def create_options

View File

@ -43,12 +43,11 @@ module Output
end end
def create_show def create_show
rows = [ [ @data["id"], @data["provider"], @data["template_url"], @data["template_body"] ] ] rows = [ [ @data["id"], @data["provider"], @data["template_url"] ] ]
headers = [ headers = [
I18n.t("output.table_header.id"), I18n.t("output.table_header.id"),
I18n.t("output.table_header.provider"), I18n.t("output.table_header.provider"),
I18n.t("output.table_header.template_url"), I18n.t("output.table_header.template_url")
I18n.t("output.table_header.template_body")
] ]
return headers, rows return headers, rows
end end

View File

@ -47,6 +47,13 @@ module Devops
template template
end end
def update_available_parameters(id)
template = Devops::Db.connector.stack_template(id)
template.available_parameters = template.parse_parameters
Devops::Db.connector.stack_template_update(template)
template
end
private private
# returns: # returns:

View File

@ -22,11 +22,18 @@ module Devops
create_response 'Created', model.to_hash, 201 create_response 'Created', model.to_hash, 201
end end
# Temp route just to process migration. Could be deleted safely
app.post_with_headers "/stack_template/:id/update_template_url", :headers => [:accept] do |template_id| app.post_with_headers "/stack_template/:id/update_template_url", :headers => [:accept] do |template_id|
check_privileges('stack_template', 'w') check_privileges('stack_template', 'w')
json Devops::API2_0::Handler::StackTemplate.new(request).update_template_url(template_id).to_hash json Devops::API2_0::Handler::StackTemplate.new(request).update_template_url(template_id).to_hash
end end
# Temp route just to process migration
app.post_with_headers "/stack_template/:id/update_available_parameters", :headers => [:accept] do |template_id|
check_privileges('stack_template', 'w')
json Devops::API2_0::Handler::StackTemplate.new(request).update_available_parameters(template_id).to_hash
end
hash = {} hash = {}
hash['GET'] = lambda {|stack_template_id| hash['GET'] = lambda {|stack_template_id|

View File

@ -1,5 +1,3 @@
require 'tempfile'
require 'securerandom'
require "db/mongo/models/model_with_provider" require "db/mongo/models/model_with_provider"
require "db/validators/stack_template/template_content" require "db/validators/stack_template/template_content"
@ -10,6 +8,8 @@ module Devops
include ModelWithProvider include ModelWithProvider
attr_accessor :id, :template_body, :provider, :owner attr_accessor :id, :template_body, :provider, :owner
# should be a hash like {'param_name' => {type: 'String', description: 'Description'}}
attr_accessor :available_parameters
types id: {type: String, empty: false}, types id: {type: String, empty: false},
provider: {type: String, empty: false}, provider: {type: String, empty: false},
@ -40,23 +40,31 @@ module Devops
self.template_body = attrs['template_body'] self.template_body = attrs['template_body']
self.provider = attrs['provider'] self.provider = attrs['provider']
self.owner = attrs['owner'] self.owner = attrs['owner']
self.available_parameters = attrs['available_parameters']
self self
end end
def to_hash_without_id def to_hash_without_id
{ {
provider: provider, 'provider' => provider,
template_body: template_body, 'template_body' => template_body,
owner: owner 'owner' => owner,
'available_parameters' => available_parameters
} }
end end
def parse_parameters
{}
end
# attrs should include: # attrs should include:
# - id (String) # - id (String)
# - provider (String) # - provider (String)
# - template_body (String) # - template_body (String)
def self.create(attrs) def self.create(attrs)
new(attrs) template = new(attrs)
template.available_parameters = template.parse_parameters
template
end end
def self.build_from_bson(attrs) def self.build_from_bson(attrs)

View File

@ -1,3 +1,5 @@
require 'securerandom'
module Devops module Devops
module Model module Model
class StackTemplateEc2 < StackTemplateBase class StackTemplateEc2 < StackTemplateBase
@ -5,8 +7,6 @@ module Devops
# In Amazon Cloudformation the template file must be stored on an Amazon S3 bucket. # In Amazon Cloudformation the template file must be stored on an Amazon S3 bucket.
attr_accessor :template_url attr_accessor :template_url
# types template_url: {type: String, empty: false}
def initialize(attrs) def initialize(attrs)
self.template_url = attrs['template_url'] self.template_url = attrs['template_url']
super(attrs) super(attrs)
@ -16,12 +16,24 @@ module Devops
super.merge(template_url: template_url) super.merge(template_url: template_url)
end end
def delete_template_file_from_storage def update_template_url
raise 'Implement me' uniq_filename = "#{id}-#{SecureRandom.hex}.template"
s3_file = provider_instance.store_stack_template(uniq_filename, template_body)
self.template_url = s3_file['url']
end end
def update_template_url def parse_parameters
self.template_url = generate_template_file_and_upload_to_storage(id, template_body) template_hash = JSON.parse(template_body)
return {} unless template_hash['Parameters']
parameters = {}
template_hash['Parameters'].each do |param_name, param_info|
# use String always for now
parameters[param_name] = {'type' => 'String', 'description' => param_info['Description']}
end
parameters
rescue
{}
end end
class << self class << self
@ -32,13 +44,6 @@ module Devops
end end
end end
private
def generate_template_file_and_upload_to_storage(id, json)
uniq_filename = "#{id}-#{SecureRandom.hex}.template"
provider_instance.store_stack_template(uniq_filename, json)['url']
end
end end
end end
end end

View File

@ -14,7 +14,7 @@ RSpec.shared_examples 'stack template' do
describe '#to_hash_without_id' do describe '#to_hash_without_id' do
it 'returns hash with provider, template_body and owner' do it 'returns hash with provider, template_body and owner' do
expect(stack_template.to_hash_without_id).to include(:provider, :template_body, :owner) expect(stack_template.to_hash_without_id).to include('provider', 'template_body', 'owner')
end end
end end

View File

@ -23,4 +23,38 @@ RSpec.describe Devops::Model::StackTemplateEc2, type: :model do
expect(described_class.create(params)).to be_an_instance_of(described_class) expect(described_class.create(params)).to be_an_instance_of(described_class)
end end
describe 'available parameters fetching' do
let(:template_body) { %q(
{
"Description" : "Example",
"Parameters" : {
"KeyName": {
"Description" : "Name of KeyPair",
"Type": "AWS::EC2::KeyPair::KeyName"
},
"SSHLocation" : {
"Type": "String",
"MinLength": "9"
}
}
}
) }
let(:template_attributes) { FactoryGirl.attributes_for(:stack_template_ec2, template_body: template_body) }
let(:template) { described_class.create(template_attributes.stringify_keys) }
it 'fetches parameters from template body' do
expect(template.available_parameters).to be_a(Hash)
expect(template.available_parameters).to include('KeyName', 'SSHLocation')
end
it 'parses description' do
expect(template.available_parameters['KeyName']['description']).to eq 'Name of KeyPair'
expect(template.available_parameters['SSHLocation']['description']).to be nil
end
end
end end