lib/outliers/evaluation.rb in outliers-0.3.3 vs lib/outliers/evaluation.rb in outliers-0.5.0.beta1

- old
+ new

@@ -1,65 +1,94 @@ module Outliers class Evaluation - attr_reader :collection, :provider_name, :provider_name_array + attr_reader :resource_collection, :provider_name, :provider_name_array def initialize(args) @run = args[:run] @name = args[:name] end - def connect(name, options={}) - @provider_name = merged_credentials(name, options).fetch 'provider' + def connect(account_name, options={}) + @account_name = account_name + @provider_name = merged_account(account_name, options).fetch 'provider' - logger.info "Connecting via '#{name}' to '#{@provider_name}'." + 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_credentials(name, options) + @provider = Outliers::Provider.connect_to merged_account(account_name, options) end - def resources(name, targets=[]) - logger.info "Loading '#{name}' resource collection." - @collection = collection_object name + def resources(name, targets=nil) + logger.debug "Loading '#{name}' resource collection." - targets_array = Array(targets) + @resource_name = name + @resource_collection = collection_object name - if targets_array.any? - logger.info "Verifying '#{targets_array.join(', ')}' from '#{name}' collection." - collection.targets = targets_array - end - collection + load_targets targets + resource_collection end - def exclude(exclusions) - collection.exclude_by_key Array(exclusions) + def filter(action, args) + resource_collection.filter action, args.keys_to_s end - def filter(args) - collection.filter args.keys_to_s - end + def verify(verification_name, arguments=nil) + @resources_loaded ||= resource_collection.load_all - def verify(verification, arguments={}) - @resources_loaded ||= collection.load_all + args_to_send = convert_verification_arguments arguments - verification_result = collection.verify verification, arguments.keys_to_sym + verification_result = resource_collection.verify verification_name, args_to_send - result = Outliers::Result.new evaluation: @name, + result = Outliers::Result.new account_name: @account_name, failing_resources: verification_result.fetch(:failing_resources), + name: @name, passing_resources: verification_result.fetch(:passing_resources), - resource: @collection, - verification: verification + arguments: Array(args_to_send), + provider_name: @provider_name, + resource_name: @resource_name, + verification_name: verification_name - logger.info "Verification '#{verification}' #{result}." + logger.info "Verification '#{verification_name}' #{result.passed? ? 'passed' : 'failed'}." @run.results << result end private + 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 + + def convert_verification_arguments(arguments) + return Array(arguments) if arguments.is_a?(Array) || arguments.is_a?(String) + return nil if arguments.is_a?(NilClass) + raise Outliers::Exceptions::InvalidArguments.new "Verification arguments '#{arguments}' invalid. Must be a string or array." + end + 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 @@ -73,21 +102,20 @@ collection_array.inject(Object) {|o,c| o.const_get c}.new(@provider) rescue NameError raise Outliers::Exceptions::UnknownCollection.new "Unknown collection '#{name}'." end - def credentials(name) - @run.credentials.fetch name + def account(name) + @run.account.fetch name end - def merged_credentials(name, options) - credentials(name).merge! options.keys_to_s - credentials(name).merge :name => name + def merged_account(name, options) + account(name).merge! options.keys_to_s + account(name).merge :name => name end def logger @logger ||= Outliers.logger end end end -