class Outliers::Evaluation

Attributes

provider_name[R]
provider_name_array[R]
resource_collection[R]

Public Class Methods

new(args) click to toggle source
# File lib/outliers/evaluation.rb, line 6
def initialize(args)
  @run  = args[:run]
  @name = args[:name]
end

Public Instance Methods

connect(account_name, options={}) click to toggle source
# File lib/outliers/evaluation.rb, line 11
def connect(account_name, options={})
  @account_name  = account_name
  @provider_name = merged_account(account_name, options).fetch 'provider'

  logger.info "Connecting via '#{account_name}' to '#{@provider_name}'."
  logger.info "Including connection options '#{options.map {|k,v| "#{k}=#{v}"}.join(',')}'." if options.any?

  set_provider_name_array

  @provider = Outliers::Provider.connect_to merged_account(account_name, options)
end
filter(action, args) click to toggle source
# File lib/outliers/evaluation.rb, line 33
def filter(action, args)
  resource_collection.filter action, args.keys_to_s
end
resources(name, targets=nil) click to toggle source
# File lib/outliers/evaluation.rb, line 23
def resources(name, targets=nil)
  logger.debug "Loading '#{name}' resource collection."

  @resource_name       = name
  @resource_collection = collection_object name

  load_targets targets
  resource_collection
end
verify(verification_name, arguments=nil) click to toggle source
# File lib/outliers/evaluation.rb, line 37
def verify(verification_name, arguments=nil)
  @resources_loaded ||= resource_collection.load_all

  args_to_send = convert_verification_arguments arguments

  verification_result = resource_collection.verify verification_name, args_to_send

  result = Outliers::Result.new account_name:      @account_name,
                                resources:         verification_result.fetch(:resources),
                                passing:           verification_result.fetch(:passing),
                                name:              @name,
                                arguments:         Array(args_to_send),
                                provider_name:     @provider_name,
                                resource_name:     @resource_name,
                                verification_name: verification_name

  logger.info "Verification '#{verification_name}' #{result.passed? ? 'passed' : 'failed'}."

  @run.results << result
end

Private Instance Methods

account(name) click to toggle source
# File lib/outliers/evaluation.rb, line 107
def account(name)
  account = @run.account.fetch name, nil
  raise Outliers::Exceptions::UnknownAccount.new "Unkown account '#{name}'." unless account
  account
end
collection_object(name) click to toggle source
# File lib/outliers/evaluation.rb, line 99
def collection_object(name)
  collection_object = name.split('_').map {|c| c.capitalize}.join('') + 'Collection'
  collection_array = ['Outliers', 'Resources'] + provider_name_array + [collection_object]
  collection_array.inject(Object) {|o,c| o.const_get c}.new(@provider)
rescue NameError
  raise Outliers::Exceptions::UnknownCollection.new "Unknown collection '#{name}'."
end
convert_verification_arguments(arguments) click to toggle source
# File lib/outliers/evaluation.rb, line 84
def convert_verification_arguments(arguments)
  return Array(arguments) if arguments.is_a?(Array) || arguments.is_a?(String) || arguments.is_a?(Integer)
  return nil if arguments.is_a?(NilClass)
  raise Outliers::Exceptions::InvalidArguments.new "Verification arguments '#{arguments}' invalid. Must be a string, integer or array."
end
load_targets(targets) click to toggle source
# File lib/outliers/evaluation.rb, line 60
def load_targets(targets)
  case targets.class.to_s
  when "Hash"
    t = targets.keys_to_sym
    if t.has_key? :include
      list = Array(t.fetch :include)
      logger.info "Targeting '#{list.join(', ')}' from '#{@resource_name}' collection."
      resource_collection.targets = list
    elsif t.has_key? :exclude
      list = Array(t.fetch :exclude)
      logger.info "Excluding '#{list.join(', ')}' from '#{@resource_name}' collection."
      resource_collection.exclude_by_key list
    else
      logger.info "Targeting all resources in '#{@resource_name}' collection."
    end
  when "String", "Array"
    list = Array(targets)
    logger.info "Targeting '#{list.join(', ')}' from '#{@resource_name}' collection."
    resource_collection.targets = list
  when "Nil"
    logger.info "Targeting all resources in '#{@resource_name}' collection."
  end
end
logger() click to toggle source
# File lib/outliers/evaluation.rb, line 118
def logger
  @logger ||= Outliers.logger 
end
merged_account(name, options) click to toggle source
# File lib/outliers/evaluation.rb, line 113
def merged_account(name, options)
  account(name).merge! options.keys_to_s
  account(name).merge :name => name
end
set_provider_name_array() click to toggle source
# File lib/outliers/evaluation.rb, line 90
def set_provider_name_array
  begin
    array = Outliers::Providers.name_map.fetch(provider_name).to_s.split('::')
    @provider_name_array = array[2..array.size]
  rescue KeyError
    raise Outliers::Exceptions::UnknownProvider.new "Unkown provider '#{provider_name}'"
  end
end