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(old_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(old_vm) ⇒ Hash
Returns The filtered VM.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/config_builder/filter/roles.rb', line 60 def filter_vm(old_vm) role_list = old_vm.delete('roles') node_stack = roles_by_name(role_list) node_stack << old_vm new_vm = node_stack.inject({}) do |accumulator, role| merge_nodes(accumulator, role) end new_vm end |
#role(name) ⇒ Hash<String, Object>
Fetch the role associated with the given name
78 79 80 81 82 83 84 |
# File 'lib/config_builder/filter/roles.rb', line 78 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<>
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/config_builder/filter/roles.rb', line 100 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 |