rename stack_template_presets to stack_presets

This commit is contained in:
Anton Chuchkalov 2015-07-15 20:21:23 +04:00
parent 9aad1253fe
commit 589fc6ff84
17 changed files with 98 additions and 91 deletions

View File

@ -47,9 +47,9 @@ class HandlerFactory
when "stack_template" when "stack_template"
require "devops-client/handler/stack_template" require "devops-client/handler/stack_template"
StackTemplate StackTemplate
when "stack_template_preset" when "stack_preset"
require "devops-client/handler/stack_template_preset" require "devops-client/handler/stack_preset"
StackTemplatePreset StackPreset
when "stack" when "stack"
require "devops-client/handler/stack" require "devops-client/handler/stack"
Stack Stack

View File

@ -1,15 +1,14 @@
require "devops-client/handler/handler" require "devops-client/handler/handler"
require "devops-client/options/stack_template_preset_options" require "devops-client/options/stack_preset_options"
require "devops-client/output/stack_template_preset" require "devops-client/output/stack_preset"
# require 'devops-client/helpers/select_available'
class StackTemplatePreset < Handler class StackPreset < Handler
output_with Output::StackTemplatePreset output_with Output::StackPreset
def initialize(host, def_options={}) def initialize(host, def_options={})
@host, @options = host, def_options @host, @options = host, def_options
@options_parser = StackTemplatePresetOptions.new(ARGV, def_options) @options_parser = StackPresetOptions.new(ARGV, def_options)
end end
def handle def handle
@ -21,8 +20,8 @@ class StackTemplatePreset < Handler
when :show when :show
show_handler show_handler
output output
when :build when :apply
build_handler apply_handler
end end
end end
@ -32,29 +31,29 @@ class StackTemplatePreset < Handler
@options_parser.invalid_show_command @options_parser.invalid_show_command
abort(wrong_params) abort(wrong_params)
end end
@show = get "/stack_template_presets/#{@args[2]}" @show = get "/stack_presets/#{@args[2]}"
end end
def list_handler def list_handler
@list = get('/stack_template_presets') @list = get('/stack_presets')
end end
def build_handler def apply_handler
wrong_params = inspect_parameters(@options_parser.build_params, @args[2]) wrong_params = inspect_parameters(@options_parser.apply_params, @args[2])
if wrong_params if wrong_params
@options_parser.invalid_build_command @options_parser.invalid_apply_command
abort(wrong_params) abort(wrong_params)
end end
params = {} params = {}
params[:id] = @args[2] params[:id] = @args[2]
params[:provider] = options[:provider] || resources_selector.select_available_provider params[:provider] = options[:provider] || resources_selector.select_available_provider
params[:stack] = options[:stack] || enter_parameter(I18n.t('handler.stack_template_preset.create.stack')) params[:stack] = options[:stack] || enter_parameter(I18n.t('handler.stack_preset.create.stack'))
filepath = options[:parameters_file] || enter_parameter(I18n.t('handler.stack_template_preset.create.parameters_file')) filepath = options[:parameters_file] || enter_parameter(I18n.t('handler.stack_preset.create.parameters_file'))
params[:parameters] = JSON.parse(File.read(filepath)) params[:parameters] = JSON.parse(File.read(filepath))
result = post_body("/stack_template_presets/#{params[:id]}/build_stack_template", JSON.pretty_generate(params)) result = post_body("/stack_presets/#{params[:id]}/apply", JSON.pretty_generate(params))
end end
end end

View File

@ -0,0 +1,33 @@
require "devops-client/options/common_options"
class StackPresetOptions < CommonOptions
commands :list, :show, :apply
def initialize args, def_options
super(args, def_options)
self.header = I18n.t("headers.stack_preset")
self.banner_header = "stack_preset"
self.list_params = ["[provider]", "[ec2|openstack]"]
self.show_params = ["STACK"]
self.apply_params = ["PRESET"]
end
def apply_options
self.options do |parser, options|
parser.banner << self.apply_banner
parser.recognize_option_value(:provider, 'stack_preset')
parser.recognize_option_value(:stack, 'stack_preset')
parser.recognize_option_value(:parameters_file, 'stack_preset')
end
end
extend_options_method :list_options do |options|
if args[2]
options[:given_provider] = args[2]
end
end
end

View File

@ -1,33 +0,0 @@
require "devops-client/options/common_options"
class StackTemplatePresetOptions < CommonOptions
commands :list, :show, :build
def initialize args, def_options
super(args, def_options)
self.header = I18n.t("headers.stack_template_preset")
self.banner_header = "stack_template_preset"
self.list_params = ["[provider]", "[ec2|openstack]"]
self.show_params = ["STACK"]
self.build_params = ["PRESET"]
end
def build_options
self.options do |parser, options|
parser.banner << self.build_banner
parser.recognize_option_value(:provider, 'stack_template_preset')
parser.recognize_option_value(:stack, 'stack_template_preset')
parser.recognize_option_value(:parameters_file, 'stack_template_preset')
end
end
extend_options_method :list_options do |options|
if args[2]
options[:given_provider] = args[2]
end
end
end

View File

@ -1,11 +1,11 @@
require "devops-client/output/base" require "devops-client/output/base"
module Output module Output
class StackTemplatePreset < Base class StackPreset < Base
def table def table
if outputting_list? if outputting_list?
title = I18n.t("output.title.stack_template_preset.list") title = I18n.t("output.title.stack_preset.list")
headers, rows = create_list headers, rows = create_list
create_table headers, rows, title, with_num? create_table headers, rows, title, with_num?
else else
@ -29,7 +29,7 @@ module Output
private private
def create_list def create_list
abort(I18n.t("output.not_found.stack_template_preset.list")) if @data.empty? abort(I18n.t("output.not_found.stack_preset.list")) if @data.empty?
fields_to_output = %w(id) fields_to_output = %w(id)

View File

@ -43,7 +43,7 @@ en:
user: "User" user: "User"
stack: "Stack" stack: "Stack"
stack_template: "Stack template" stack_template: "Stack template"
stack_template_preset: "Stack template preset" stack_preset: "Stack template preset"
handler: handler:
flavor: flavor:
list: list:
@ -118,10 +118,10 @@ en:
question: question:
create: "Are you sure to create stack?" create: "Are you sure to create stack?"
delete: "Are you sure to delete stack '%{name}'?" delete: "Are you sure to delete stack '%{name}'?"
stack_template_preset: stack_preset:
create: create:
parameters_file: 'Path to file with JSON parameters: ' parameters_file: 'Path to file with JSON parameters: '
stack_template: 'Name of stack template to build: ' stack: 'Name of stack to create: '
message: message:
choose_list_default: "Choose %{name} (comma separated), like 1,2,3 or empty for default value '%{default}': " choose_list_default: "Choose %{name} (comma separated), like 1,2,3 or empty for default value '%{default}': "
choose_list: "Choose %{name} (comma separated), like 1,2,3: " choose_list: "Choose %{name} (comma separated), like 1,2,3: "
@ -235,7 +235,7 @@ en:
stack_template: stack_template:
list: "Stack Templates" list: "Stack Templates"
show: "Stack Template" show: "Stack Template"
stack_template_preset: stack_preset:
list: "Stack Template Presets" list: "Stack Template Presets"
show: "Stack Template Preset" show: "Stack Template Preset"
stack: stack:
@ -272,7 +272,7 @@ en:
stack_template: stack_template:
list: "No stack templates found" list: "No stack templates found"
show: "There isn't such stack template" show: "There isn't such stack template"
stack_template_preset: stack_preset:
list: "No stack template presets found" list: "No stack template presets found"
show: "There isn't such stack template preset" show: "There isn't such stack template preset"
stack: stack:
@ -378,7 +378,7 @@ en:
provider: Stack template provider provider: Stack template provider
id: Stack template id id: Stack template id
template_file: Stack template file template_file: Stack template file
stack_template_preset: stack_preset:
parameters_file: Path to file with JSON parameters parameters_file: Path to file with JSON parameters
stack: 'Name of stack to build: ' stack: 'Name of stack to build: '
provider: Stack provider provider: Stack provider

View File

@ -21,7 +21,7 @@ module Devops
require "routes/v2.0/handlers/server" require "routes/v2.0/handlers/server"
require "routes/v2.0/handlers/stack" require "routes/v2.0/handlers/stack"
require "routes/v2.0/handlers/stack_template" require "routes/v2.0/handlers/stack_template"
require "routes/v2.0/stack_template_presets" require "routes/v2.0/stack_presets"
require "routes/v2.0/handlers/report" require "routes/v2.0/handlers/report"
require 'lib/stubber' require 'lib/stubber'
@ -60,7 +60,7 @@ module Devops
require "routes/v2.0/bootstrap_templates" require "routes/v2.0/bootstrap_templates"
require "routes/v2.0/stack" require "routes/v2.0/stack"
require "routes/v2.0/stack_template" require "routes/v2.0/stack_template"
require "routes/v2.0/handlers/stack_template_preset" require "routes/v2.0/handlers/stack_preset"
require "routes/v2.0/report" require "routes/v2.0/report"
routes = Devops::Version2_0::Routes.constants.collect{|s| Devops::Version2_0::Routes.const_get(s)}.select {|const| const.class == Module} routes = Devops::Version2_0::Routes.constants.collect{|s| Devops::Version2_0::Routes.const_get(s)}.select {|const| const.class == Module}

View File

@ -2,7 +2,7 @@ require 'lib/string_helper'
require 'db/mongo/models/stack_template/stack_template_factory' require 'db/mongo/models/stack_template/stack_template_factory'
module Devops module Devops
module StackTemplatePresets module StackPresets
class Base class Base
def id def id
@ -25,7 +25,7 @@ module Devops
end end
def template_body def template_body
@template_body ||= File.read("lib/stack_template_presets/#{id}.#{template_file_extension}") @template_body ||= File.read("lib/stack_presets/#{id}.#{template_file_extension}")
end end
# some templates may be YAML files # some templates may be YAML files

View File

@ -2,15 +2,15 @@ require_relative 'base'
require_relative 'postgres_cluster' require_relative 'postgres_cluster'
require_relative 'preset_not_found' require_relative 'preset_not_found'
class Devops::StackTemplatePresetsFactory class Devops::StackPresetsFactory
# find all classes in Devops::StackTemplatePresets modules excluding Base and factory. # find all classes in Devops::StackPresets modules excluding Base and factory.
# This list can be extended in external gems via defining new classes in Devops::StackTemplatePresets module. # This list can be extended in external gems via defining new classes in Devops::StackPresets module.
def self.list def self.list
@list ||= Devops::StackTemplatePresets.constants.select do |class_name| @list ||= Devops::StackPresets.constants.select do |class_name|
class_name != :Base class_name != :Base
end.map do |class_name| end.map do |class_name|
Devops::StackTemplatePresets.const_get(class_name).new Devops::StackPresets.const_get(class_name).new
end end
end end

View File

@ -1,4 +1,4 @@
module Devops::StackTemplatePresets module Devops::StackPresets
class PostgresCluster < Base class PostgresCluster < Base
def template_file_extension def template_file_extension
:yml :yml

View File

@ -1,35 +1,35 @@
require 'json' require 'json'
require 'lib/stack_template_presets/factory' require 'lib/stack_presets/factory'
module Devops module Devops
module Version2_0 module Version2_0
module Handler module Handler
class StackTemplatePreset class StackPreset
def self.get_presets def self.get_presets
lambda { lambda {
# check_privileges("stack_template_presets", "r") # check_privileges("stack_presets", "r")
json Devops::StackTemplatePresetsFactory.list.map(&:to_hash) json Devops::StackPresetsFactory.list.map(&:to_hash)
} }
end end
def self.get_preset def self.get_preset
lambda { lambda {
# check_privileges("stack_template_presets", "r") # check_privileges("stack_presets", "r")
json Devops::StackTemplatePresetsFactory.get(params['id']).to_hash json Devops::StackPresetsFactory.get(params['id']).to_hash
} }
end end
def self.build_stack_template def self.apply
lambda { lambda {
# check_privileges("stack_template_presets", "r") # check_privileges("stack_presets", "r")
check_privileges('stack_template', 'w') check_privileges('stack_template', 'w')
attrs = create_object_from_json_body attrs = create_object_from_json_body
provider, stack_id, parameters = attrs.fetch('provider'), attrs.fetch('stack'), attrs.fetch('parameters') provider, stack_id, parameters = attrs.fetch('provider'), attrs.fetch('stack'), attrs.fetch('parameters')
preset = Devops::StackTemplatePresetsFactory.get(attrs.fetch('id')) preset = Devops::StackPresetsFactory.get(attrs.fetch('id'))
stack = preset.create_stack_from_preset(provider, stack_id, parameters) stack = preset.create_stack_from_preset(provider, stack_id, parameters)
settings.mongo.stack_insert(stack) settings.mongo.stack_insert(stack)

View File

@ -1,10 +1,10 @@
module Devops module Devops
module Version2_0 module Version2_0
module Routes module Routes
module StackTemplatePresetRoutes module StackPresetRoutes
def self.registered(app) def self.registered(app)
# Get list of available stack_template_presets # Get list of available stack_presets
# #
# * *Request* # * *Request*
# - method : GET # - method : GET
@ -14,10 +14,10 @@ module Devops
# * *Returns* : array of hashes # * *Returns* : array of hashes
# [ {id: 'preset id', template_preset_body: 'long body'} ] # [ {id: 'preset id', template_preset_body: 'long body'} ]
# #
app.get_with_headers "/stack_template_presets", :headers => [:accept], &Devops::Version2_0::Handler::StackTemplatePreset.get_presets app.get_with_headers "/stack_presets", :headers => [:accept], &Devops::Version2_0::Handler::StackPreset.get_presets
# Get information about stack_template_preset # Get information about stack_preset
# #
# * *Request* # * *Request*
# - method : GET # - method : GET
@ -27,7 +27,7 @@ module Devops
# * *Returns* : hash # * *Returns* : hash
# {id: 'preset id', template_preset_body: 'long body'} # {id: 'preset id', template_preset_body: 'long body'}
# #
app.get_with_headers "/stack_template_presets/:id", :headers => [:accept], &Devops::Version2_0::Handler::StackTemplatePreset.get_preset app.get_with_headers "/stack_presets/:id", :headers => [:accept], &Devops::Version2_0::Handler::StackPreset.get_preset
# Build stack template from preset # Build stack template from preset
@ -38,7 +38,7 @@ module Devops
# - Accept: application/json # - Accept: application/json
# - params : # - params :
# - provider: string # - provider: string
# - stack_template_id: id of stack template to create # - stack_id: id of stack template to create
# - template_attrs: hash with template attributes # - template_attrs: hash with template attributes
# #
# TODO: not stack template, but stack itself # TODO: not stack template, but stack itself
@ -50,7 +50,7 @@ module Devops
# template_body: 'long body' # 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 app.post_with_headers "/stack_presets/:id/apply", :headers => [:accept], &Devops::Version2_0::Handler::StackPreset.apply
puts "Stack template presets routes initialized" puts "Stack template presets routes initialized"
end end

View File

@ -77,7 +77,7 @@ templates = {
#list #list
"templates/api_v2/00_list/flavor.feature.erb" => "features/api_v2/00_list/flavor.feature", "templates/api_v2/00_list/flavor.feature.erb" => "features/api_v2/00_list/flavor.feature",
"templates/api_v2/00_list/stack_template_preset.feature.erb" => "features/api_v2/00_list/stack_template_preset.feature", "templates/api_v2/00_list/stack_preset.feature.erb" => "features/api_v2/00_list/stack_preset.feature",
"templates/api_v2/00_list/10_user.feature.erb" => "features/api_v2/00_list/10_user.feature", "templates/api_v2/00_list/10_user.feature.erb" => "features/api_v2/00_list/10_user.feature",
#create #create

View File

@ -71,4 +71,12 @@ class Provider::Openstack
true true
end end
def stack_details
{stack_status: 'stubbed'}
end
def stack_resources
['stubbed']
end
end end

View File

@ -1,8 +1,8 @@
@stack_template_preset @stack_preset
Feature: stack template preset list Feature: stack template preset list
Scenario: Get list of all stack template presets Scenario: Get list of all stack template presets
When I send GET '/v2.0/stack_template_presets' query When I send GET '/v2.0/stack_presets' query
Then response should be '200' Then response should be '200'
And the Content-Type header should include 'application/json' And the Content-Type header should include 'application/json'
And the JSON response should be an array And the JSON response should be an array
@ -17,7 +17,7 @@ Feature: stack template preset list
""" """
Scenario: Get information about particular stack template preset Scenario: Get information about particular stack template preset
When I send GET '/v2.0/stack_template_presets/postgres_cluster' query When I send GET '/v2.0/stack_presets/postgres_cluster' query
Then response should be '200' Then response should be '200'
And the Content-Type header should include 'application/json' And the Content-Type header should include 'application/json'
And response should be JSON object like: And response should be JSON object like: