Module: Beaker::Shared::HostRoleParser
- Included in:
- Beaker::Shared
- Defined in:
- lib/beaker/shared/host_role_parser.rb
Overview
Methods for selecting host or hosts that match roles.
Instance Method Summary collapse
-
#hosts_with_role(hosts, desired_role = nil) ⇒ Array<Host>
Find hosts from a given array of hosts that all have the desired role.
-
#only_host_with_role(hosts, role) ⇒ Host
Find a single host with the role provided.
Instance Method Details
#hosts_with_role(hosts, desired_role = nil) ⇒ Array<Host>
Find hosts from a given array of hosts that all have the desired role.
10 11 12 13 14 |
# File 'lib/beaker/shared/host_role_parser.rb', line 10 def hosts_with_role(hosts, desired_role = nil) hosts.select do |host| desired_role.nil? or host['roles'].include?(desired_role.to_s) end end |
#only_host_with_role(hosts, role) ⇒ Host
Find a single host with the role provided. Raise an error if more than one host is found to have the provided role.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/beaker/shared/host_role_parser.rb', line 23 def only_host_with_role(hosts, role) a_host = hosts_with_role(hosts, role) case when a_host.length == 0 raise ArgumentError, "There should be one host with #{role} defined!" when a_host.length > 1 host_string = ( a_host.map { |host| host.name } ).join( ', ') raise ArgumentError, "There should be only one host with #{role} defined, but I found #{a_host.length} (#{host_string})" end a_host.first end |