Sha256: 7ef238447c83da4b1d9ca3d9fd3887d8f855d9f72a547787655509574dbf22c0

Contents?: true

Size: 1.59 KB

Versions: 11

Compression:

Stored size: 1.59 KB

Contents

module Beaker
  module Shared
    #Methods for selecting host or hosts that match roles.
    module HostRoleParser

      #Find hosts from a given array of hosts that all have the desired role.
      #@param [Array<Host>] hosts The hosts to examine
      #@param [String] desired_role The hosts returned will have this role in their roles list
      #@return [Array<Host>] The hosts that have the desired role in their roles list
      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

      #Find a single host with the role provided.  Raise an error if more than one host is found to have the
      #provided role.
      #@param [Array<Host>] hosts The hosts to examine
      #@param [String] role The host returned will have this role in its role list
      #@return [Host] The single host with the desired role in its roles list
      #@raise [ArgumentError] Raised if more than one host has the given role defined, or if no host has the
      #                       role defined.
      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
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
beaker-1.16.0 lib/beaker/shared/host_role_parser.rb
beaker-1.15.0 lib/beaker/shared/host_role_parser.rb
beaker-1.14.1 lib/beaker/shared/host_role_parser.rb
beaker-1.14.0 lib/beaker/shared/host_role_parser.rb
beaker-1.13.1 lib/beaker/shared/host_role_parser.rb
beaker-1.13.0 lib/beaker/shared/host_role_parser.rb
beaker-1.12.2 lib/beaker/shared/host_role_parser.rb
beaker-1.12.1 lib/beaker/shared/host_role_parser.rb
beaker-1.12.0 lib/beaker/shared/host_role_parser.rb
beaker-1.11.2 lib/beaker/shared/host_role_parser.rb
beaker-1.11.1 lib/beaker/shared/host_role_parser.rb