add run list validator

This commit is contained in:
Anton Chuchkalov 2014-11-26 17:59:46 +04:00
parent 0cb4fe9214
commit 73f4d4d540
8 changed files with 66 additions and 9 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
devops-service/tests/features/support/config.yml
.devops_files/

View File

@ -20,8 +20,7 @@ class DeployEnvBase < MongoModel
def validate!
super
e = DeployEnvBase.validate_run_list(self.run_list)
raise InvalidRecord.new "Invalid run list elements: '#{e.join("', '")}'" unless e.empty?
Validators::DeployEnv::RunList.new(self).validate!
unless self.expires.nil?
check_expires!(self.expires)
@ -39,9 +38,4 @@ class DeployEnvBase < MongoModel
}
end
def self.validate_run_list list
rl = /\Arole|recipe\[[\w-]+(::[\w-]+)?\]\Z/
list.select {|l| (rl =~ l).nil?}
end
end

View File

@ -0,0 +1,8 @@
module Validators
class Helpers; end
class DeployEnv; end
end
require "db/validators/base"
require "db/validators/helpers/run_list"
require "db/validators/deploy_env/run_list"

View File

@ -0,0 +1,18 @@
class Validators::Base
def initialize(model)
@model = model
end
def validate!
raise InvalidRecord.new(message) unless valid?
end
def valid?
raise 'override me'
end
def message
raise 'override me'
end
end

View File

@ -0,0 +1,20 @@
# Use helper to avoid code duplication: we have run_list validation in server routes,
# not only in deploy env model
module Validators
class DeployEnv::RunList < Base
def initialize(model)
super(model)
@helper_validator = Helpers::RunList.new(@model.run_list)
end
def valid?
@helper_validator.valid?
end
def message
@helper_validator.message
end
end
end

View File

@ -0,0 +1,15 @@
module Validators
class Helpers::RunList < Base
def valid?
rl = /\Arole|recipe\[[\w-]+(::[\w-]+)?\]\Z/
@invalid_elements = @model.select {|l| (rl =~ l).nil?}
@invalid_elements.empty?
end
def message
invalid_elements_as_string = @invalid_elements.join("', '")
"Invalid run list elements: '#{invalid_elements_as_string}'. Each element should be role or recipe."
end
end
end

View File

@ -9,6 +9,7 @@ $:.push File.dirname(__FILE__)
require "db/exceptions/invalid_record"
require "db/exceptions/record_not_found"
require "db/mongo/mongo_connector"
require "db/validators/all"
require "providers/provider_factory"
require "routes/v2.0"

View File

@ -396,8 +396,8 @@ module Version2_0
name = check_string(body["name"], "Parameter 'name' should be a not empty string", true)
rl = check_array(body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true)
unless rl.nil?
e = DeployEnv.validate_run_list(rl)
halt_response("Invalid run list elements: '#{e.join("', '")}'") unless e.empty?
validator = Validators::Helpers::RunList.new(rl)
halt_response(validator.message) unless validator.valid?
end
t = check_string(body["bootstrap_template"], "Parameter 'bootstrap_template' should be a not empty string", true)
s = BaseRoutes.mongo.server_by_instance_id(id)