2015-09-03 18:36:46 +00:00
|
|
|
# encoding: utf-8
|
2015-08-27 20:59:58 +00:00
|
|
|
require 'vulcano/backend'
|
|
|
|
|
|
|
|
module Vulcano
|
|
|
|
class ProfileContext
|
|
|
|
attr_reader :rules, :only_ifs
|
2015-08-30 02:33:15 +00:00
|
|
|
def initialize(profile_id, backend, profile_registry: {}, only_ifs: [])
|
2015-08-29 23:31:36 +00:00
|
|
|
if backend.nil?
|
2015-09-05 14:07:54 +00:00
|
|
|
fail 'ProfileContext is initiated with a backend == nil. ' \
|
2015-09-03 21:18:28 +00:00
|
|
|
'This is a backend error which must be fixed upstream.'
|
2015-08-29 23:31:36 +00:00
|
|
|
end
|
|
|
|
|
2015-08-27 20:59:58 +00:00
|
|
|
@profile_id = profile_id
|
|
|
|
@rules = profile_registry
|
|
|
|
@only_ifs = only_ifs
|
|
|
|
__CTX = self
|
|
|
|
|
|
|
|
# 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 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
|
|
|
|
2015-09-04 07:59:30 +00:00
|
|
|
Vulcano::Resource.registry.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
|
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def unregister_rule(id)
|
2015-09-05 14:07:54 +00:00
|
|
|
full_id = VulcanoBaseRule.full_id(@profile_id, id)
|
2015-08-27 20:59:58 +00:00
|
|
|
@rules[full_id] = nil
|
|
|
|
end
|
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def register_rule(r)
|
2015-08-27 20:59:58 +00:00
|
|
|
# get the full ID
|
2015-09-05 14:07:54 +00:00
|
|
|
full_id = VulcanoBaseRule.full_id(@profile_id, r)
|
2015-08-27 20:59:58 +00:00
|
|
|
if full_id.nil?
|
2015-09-05 14:07:54 +00:00
|
|
|
# TODO: error
|
2015-08-27 20:59:58 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
# add the rule to the registry
|
|
|
|
existing = @rules[full_id]
|
|
|
|
if existing.nil?
|
|
|
|
@rules[full_id] = r
|
|
|
|
else
|
2015-09-05 14:07:54 +00:00
|
|
|
VulcanoBaseRule.merge(existing, r)
|
2015-08-27 20:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|