Class: ConfigBuilder::Filter::Roles
- Inherits:
-
Object
- Object
- ConfigBuilder::Filter::Roles
- Defined in:
- lib/config_builder/filter/roles.rb
Overview
The 'roles' filter adds a mechanism for defining generic VM roles and applying them to VMs.
Defining roles
This filter adds support for a top level roles
key. It contains a hash of
role names that define a hash containing the role behavior.
@note: The 'vms' field is of type Array, while the 'roles' field is of type Hash. This is because order of declaration matters for the actual VMs, while the order of declaration of roles does not matter.
Instance Attribute Summary collapse
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
-
#filter_vm(vm) ⇒ Hash
The filtered VM.
-
#role(name) ⇒ Hash<String, Object>
Fetch the role associated with the given name.
- #roles_by_name(field) ⇒ Array
- #run ⇒ Object
- #set_config(root_config) ⇒ Object
Instance Attribute Details
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
42 43 44 |
# File 'lib/config_builder/filter/roles.rb', line 42 def roles @roles end |
Instance Method Details
#filter_vm(vm) ⇒ Hash
Returns The filtered VM.
60 61 62 63 64 65 66 |
# File 'lib/config_builder/filter/roles.rb', line 60 def filter_vm(vm) node_stack = roles_by_name(vm.delete('roles')) node_stack.inject(vm) do |accumulator, role| merge_nodes!(accumulator, role) end end |
#role(name) ⇒ Hash<String, Object>
Fetch the role associated with the given name
73 74 75 76 77 78 79 |
# File 'lib/config_builder/filter/roles.rb', line 73 def role(name) if (retval = @roles[name]) retval else raise ArgumentError, "Requested role #{name.inspect} is not defined, available roles: #{@roles.keys}." end end |
#roles_by_name(name) ⇒ Array<Hash> #roles_by_name(names) ⇒ Array<Hash> #roles_by_name(nothing) ⇒ Array<>
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/config_builder/filter/roles.rb', line 95 def roles_by_name(field) case field when Array then names = field when String then names = [field] when NilClass then names = [] end names.map { |name| role(name) } end |
#run ⇒ Object
50 51 52 53 54 55 |
# File 'lib/config_builder/filter/roles.rb', line 50 def run return @root_config if @vms.nil? @root_config['vms'] = @vms.map { |vm_hash| filter_vm(vm_hash) } @root_config end |
#set_config(root_config) ⇒ Object
44 45 46 47 48 |
# File 'lib/config_builder/filter/roles.rb', line 44 def set_config(root_config) @root_config = root_config @roles = @root_config.delete('roles') @vms = @root_config.delete('vms') end |