2015-08-27 20:59:58 +00:00
|
|
|
require 'vulcano/backend'
|
|
|
|
|
|
|
|
module Vulcano
|
|
|
|
|
|
|
|
class ProfileContext
|
|
|
|
|
|
|
|
attr_reader :rules, :only_ifs
|
|
|
|
def initialize profile_id, profile_registry, only_ifs
|
|
|
|
@profile_id = profile_id
|
|
|
|
@rules = profile_registry
|
|
|
|
@only_ifs = only_ifs
|
|
|
|
__CTX = self
|
|
|
|
backend = Vulcano::Backend::Mock::Runner.new
|
|
|
|
|
2015-08-28 17:10:03 +00:00
|
|
|
resource_classes = {}
|
|
|
|
Vulcano::Resource.registry.each do |name, cl|
|
|
|
|
resource_classes[name] = Class.new(cl) do
|
|
|
|
include Vulcano::ResourceCommon
|
|
|
|
def initialize(backend, *args)
|
|
|
|
@vulcano = backend
|
|
|
|
super(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-27 20:59:58 +00:00
|
|
|
# This is the heart of the profile context
|
|
|
|
# An instantiated object which has all resources registered to it
|
|
|
|
# and exposes them to the a test file.
|
|
|
|
ctx = Class.new do
|
|
|
|
include Serverspec::Helper::Type
|
|
|
|
extend Serverspec::Helper::Type
|
|
|
|
include Vulcano::DSL
|
2015-08-28 17:10:03 +00:00
|
|
|
|
2015-08-27 20:59:58 +00:00
|
|
|
define_method :__register_rule do |*args|
|
|
|
|
__CTX.register_rule(*args)
|
|
|
|
end
|
|
|
|
define_method :__unregister_rule do |*args|
|
|
|
|
__CTX.unregister_rule(*args)
|
|
|
|
end
|
2015-08-28 17:10:03 +00:00
|
|
|
|
|
|
|
resource_classes.each do |id,r|
|
2015-08-27 20:59:58 +00:00
|
|
|
define_method id.to_sym do |*args|
|
2015-08-28 17:10:03 +00:00
|
|
|
r.new(backend, *args)
|
2015-08-27 20:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-28 17:10:03 +00:00
|
|
|
|
2015-08-27 20:59:58 +00:00
|
|
|
def to_s
|
|
|
|
'Profile Context Run'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@profile_context = ctx.new
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def load(content, source, line)
|
|
|
|
@profile_context.instance_eval(content, source, line)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unregister_rule id
|
|
|
|
full_id = VulcanoBaseRule::full_id(@profile_id, id)
|
|
|
|
@rules[full_id] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_rule r
|
|
|
|
# get the full ID
|
|
|
|
full_id = VulcanoBaseRule::full_id(@profile_id, r)
|
|
|
|
if full_id.nil?
|
|
|
|
# TODO error
|
|
|
|
return
|
|
|
|
end
|
|
|
|
# add the rule to the registry
|
|
|
|
existing = @rules[full_id]
|
|
|
|
if existing.nil?
|
|
|
|
@rules[full_id] = r
|
|
|
|
else
|
|
|
|
VulcanoBaseRule::merge(existing, r)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|