diff --git a/Rakefile b/Rakefile index bd5af7c6f..362d2d464 100644 --- a/Rakefile +++ b/Rakefile @@ -6,7 +6,8 @@ begin APP_RAKEFILE = File.expand_path('spec/manageiq/Rakefile', __dir__) load 'rails/tasks/engine.rake' load 'rails/tasks/statistics.rake' -rescue LoadError +rescue LoadError => err + warn err.message end require 'bundler/gem_tasks' diff --git a/app/models/miq_ae_class_copy.rb b/app/models/miq_ae_class_copy.rb index a9aa5da93..e86885ca6 100644 --- a/app/models/miq_ae_class_copy.rb +++ b/app/models/miq_ae_class_copy.rb @@ -10,28 +10,28 @@ def initialize(class_fqname) raise "Source class not found #{@class_fqname}" unless @src_class end - def to_domain(domain, ns = nil, overwrite = false) - check_duplicity(domain, ns, @src_class.name) + def to_domain(domain, namespace = nil, overwrite = false) + check_duplicity(domain, namespace, @src_class.name) @overwrite = overwrite - @target_ns_fqname = target_ns(domain, ns) + @target_ns_fqname = target_ns(domain, namespace) @target_name = @src_class.name copy end - def as(new_name, ns = nil, overwrite = false) - check_duplicity(@src_domain, ns, new_name) + def as(new_name, namespace = nil, overwrite = false) + check_duplicity(@src_domain, namespace, new_name) @overwrite = overwrite - @target_ns_fqname = target_ns(@src_domain, ns) + @target_ns_fqname = target_ns(@src_domain, namespace) @target_name = new_name copy end - def self.copy_multiple(ids, domain, ns = nil, overwrite = false) + def self.copy_multiple(ids, domain, namespace = nil, overwrite = false) new_ids = [] MiqAeClass.transaction do ids.each do |id| class_obj = MiqAeClass.find(id) - new_class = new(class_obj.fqname).to_domain(domain, ns, overwrite) + new_class = new(class_obj.fqname).to_domain(domain, namespace, overwrite) new_ids << new_class.id if new_class end end @@ -40,11 +40,11 @@ def self.copy_multiple(ids, domain, ns = nil, overwrite = false) private - def target_ns(domain, ns) - return "#{domain}/#{@partial_ns}" if ns.nil? + def target_ns(domain, namespace) + return "#{domain}/#{@partial_ns}" if namespace.nil? - ns_obj = MiqAeNamespace.lookup_by_fqname(ns, false) - ns_obj && !ns_obj.domain? ? ns : "#{domain}/#{ns}" + ns_obj = MiqAeNamespace.lookup_by_fqname(namespace, false) + ns_obj && !ns_obj.domain? ? namespace : "#{domain}/#{namespace}" end def copy @@ -91,10 +91,10 @@ def validate end end - def check_duplicity(domain, ns, classname) - $log.info("Domain: #{domain}, namespace: #{ns}, classname: #{classname}") + def check_duplicity(domain, namespace, classname) + $log.info("Domain: #{domain}, namespace: #{namespace}, classname: #{classname}") if domain.downcase == @src_domain.downcase && classname.downcase == @ae_class.downcase - raise "Cannot copy class onto itself" if ns.nil? || ns.downcase == @partial_ns.downcase + raise "Cannot copy class onto itself" if namespace.nil? || namespace.downcase == @partial_ns.downcase end end end diff --git a/app/models/miq_ae_datastore.rb b/app/models/miq_ae_datastore.rb index f7c4f7307..16ffefe91 100644 --- a/app/models/miq_ae_datastore.rb +++ b/app/models/miq_ae_datastore.rb @@ -49,16 +49,16 @@ def self.convert(filename, domain_name = temp_domain, export_options = {}) end end - def self.upload(fd, name = nil, domain_name = ALL_DOMAINS) - name ||= fd.original_filename + def self.upload(file_data, name = nil, domain_name = ALL_DOMAINS) + name ||= file_data.original_filename name = Pathname(name).basename.sub_ext('.zip') upload_to = TMP_DIR.join(name) TMP_DIR.mkpath _log.info("Uploading Datastore Import to file <#{upload_to}>") - IO.copy_stream(fd, upload_to) - fd.close + IO.copy_stream(file_data, upload_to) + file_data.close _log.info("Upload complete (size=#{upload_to.size})") @@ -110,12 +110,12 @@ def self.export(tenant) temp_export.unlink end - def self.export_class(ns, class_name) - XmlExport.class_to_xml(ns, class_name) + def self.export_class(namespace, class_name) + XmlExport.class_to_xml(namespace, class_name) end - def self.export_namespace(ns) - XmlExport.namespace_to_xml(ns) + def self.export_namespace(namespace) + XmlExport.namespace_to_xml(namespace) end def self.reset @@ -124,15 +124,15 @@ def self.reset end def self.reset_default_namespace - ns = MiqAeNamespace.lookup_by_fqname(DEFAULT_OBJECT_NAMESPACE) - ns&.destroy + namespace = MiqAeNamespace.lookup_by_fqname(DEFAULT_OBJECT_NAMESPACE) + namespace&.destroy seed_default_namespace end private_class_method def self.reset_domain(datastore_dir, domain_name, tenant) _log.info("Resetting domain #{domain_name} from #{datastore_dir}") ns = MiqAeDomain.lookup_by_fqname(domain_name) - ns.destroy if ns + ns&.destroy import_yaml_dir(datastore_dir, domain_name, tenant) if domain_name.downcase == MANAGEIQ_DOMAIN.downcase ns = MiqAeDomain.lookup_by_fqname(MANAGEIQ_DOMAIN) @@ -239,18 +239,18 @@ def self.get_homonymic_across_domains(user, arclass, fqname, enabled = nil) MiqAeDatastore.get_sorted_matching_objects(user, arclass, ns, klass, name, enabled) end - def self.get_sorted_matching_objects(user, arclass, ns, klass, name, enabled) + def self.get_sorted_matching_objects(user, arclass, namespace, klass, name, enabled) options = arclass == ::MiqAeClass ? {:has_instance_name => false} : {} domains = user.current_tenant.visible_domains matches = arclass.where("lower(name) = ?", name.downcase).collect do |obj| - get_domain_index_object(domains, obj, klass, ns, enabled, options) + get_domain_index_object(domains, obj, klass, namespace, enabled, options) end.compact matches.sort_by { |a| a[:index] }.collect { |v| v[:obj] } end - def self.get_domain_index_object(domains, obj, klass, ns, enabled, options) + def self.get_domain_index_object(domains, obj, klass, namespace, enabled, options) domain, nsd, klass_name, = ::MiqAeEngine::MiqAePath.get_domain_ns_klass_inst(obj.fqname, options) - return if !klass_name.casecmp(klass).zero? || !nsd.casecmp(ns).zero? + return if !klass_name.casecmp(klass).zero? || !nsd.casecmp(namespace).zero? domain_index = get_domain_index(domains, domain, enabled) {:obj => obj, :index => domain_index} if domain_index diff --git a/app/models/miq_ae_datastore/xml_export.rb b/app/models/miq_ae_datastore/xml_export.rb index 0fc339b3d..b986c8cc8 100644 --- a/app/models/miq_ae_datastore/xml_export.rb +++ b/app/models/miq_ae_datastore/xml_export.rb @@ -17,18 +17,18 @@ def self.to_xml end end - def self.class_to_xml(ns, class_name) + def self.class_to_xml(namespace, class_name) _log.info("Exporting class: #{class_name} to XML") xml = Builder::XmlMarkup.new(:indent => 2) xml.instruct! xml.MiqAeDatastore(:version => '1.0') do - c = MiqAeClass.lookup_by_namespace_and_name(ns, class_name) + c = MiqAeClass.lookup_by_namespace_and_name(namespace, class_name) c.to_export_xml(:builder => xml, :skip_instruct => true, :indent => 2) end end - def self.export_sub_namespaces(ns, xml) - ns.ae_namespaces.each do |n| + def self.export_sub_namespaces(namespace, xml) + namespace.ae_namespaces.each do |n| sn = MiqAeNamespace.lookup_by_fqname(n.fqname) export_all_classes_for_namespace(sn, xml) export_sub_namespaces(sn, xml) @@ -51,8 +51,8 @@ def self.namespace_to_xml(namespace) end end - def self.export_all_classes_for_namespace(ns, xml) - MiqAeClass.where(:namespace_id => ns.id.to_i).sort_by(&:fqname).each do |c| + def self.export_all_classes_for_namespace(namespace, xml) + MiqAeClass.where(:namespace_id => namespace.id.to_i).sort_by(&:fqname).each do |c| c.to_export_xml(:builder => xml, :skip_instruct => true, :indent => 2) end end diff --git a/app/models/miq_ae_datastore/xml_import.rb b/app/models/miq_ae_datastore/xml_import.rb index 03c50596b..3aaa822bd 100644 --- a/app/models/miq_ae_datastore/xml_import.rb +++ b/app/models/miq_ae_datastore/xml_import.rb @@ -116,16 +116,16 @@ def self.process_button(input) CustomButton.create_or_update_from_hash(input) end - def self.check_version(v) - v.to_s >= MiqAeDatastore::XML_VERSION_MIN_SUPPORTED + def self.check_version(version) + version.to_s >= MiqAeDatastore::XML_VERSION_MIN_SUPPORTED end - def self.load_xml(f, domain_name = nil) + def self.load_xml(filename, domain_name = nil) _, t = Benchmark.realtime_block(:total_load_xml_time) do classes = buttons = nil Benchmark.realtime_block(:xml_load_time) do require 'xml/xml_hash' - doc = XmlHash.load(f) + doc = XmlHash.load(filename) version = doc.children[0].attributes[:version] _log.info(" with version '#{version}'") raise "Unsupported version '#{version}'. Must be at least '#{MiqAeDatastore::XML_VERSION_MIN_SUPPORTED}'." unless check_version(version) @@ -165,12 +165,12 @@ def self.load_xml_file(filename, domain) File.open(filename, 'r') { |handle| load_xml(handle, domain) } end - def self.load_file(f, domain) - _log.info("Importing file '#{f}'") - ext = File.extname(f).downcase + def self.load_file(filename, domain) + _log.info("Importing file '#{filename}'") + ext = File.extname(filename).downcase case ext - when ".xml" then load_xml_file(f, domain) - else raise "Unhandled File Extension [#{ext}] when trying to load #{f}" + when ".xml" then load_xml_file(filename, domain) + else raise "Unhandled File Extension [#{ext}] when trying to load #{filename}" end _log.info("Import complete") end diff --git a/app/models/miq_ae_domain.rb b/app/models/miq_ae_domain.rb index 393cbbf3b..208f82260 100644 --- a/app/models/miq_ae_domain.rb +++ b/app/models/miq_ae_domain.rb @@ -192,13 +192,6 @@ def self.display_name(number = 1) n_('Automate Domain', 'Automate Domains', number) end - private - - def squeeze_priorities - ids = MiqAeDomain.where('priority > 0', :tenant => tenant).order('priority ASC').collect(&:id) - MiqAeDomain.reset_priority_by_ordered_ids(ids) - end - def self.any_enabled? MiqAeDomain.enabled.count.positive? end @@ -211,6 +204,13 @@ def self.all_unlocked MiqAeDomain.where(:source => USER_SOURCE).order('priority DESC') end + private + + def squeeze_priorities + ids = MiqAeDomain.where('priority > 0', :tenant => tenant).order('priority ASC').collect(&:id) + MiqAeDomain.reset_priority_by_ordered_ids(ids) + end + def about_class ns = MiqAeNamespace.where(:parent_id => id).find_by("lower(name) = ?", "system") MiqAeClass.where(:namespace_id => ns.id).find_by("lower(name) = ?", "about") if ns diff --git a/app/models/miq_ae_instance_copy.rb b/app/models/miq_ae_instance_copy.rb index 04c5f2191..5df904d75 100644 --- a/app/models/miq_ae_instance_copy.rb +++ b/app/models/miq_ae_instance_copy.rb @@ -18,31 +18,31 @@ def initialize(instance_fqname, validate_schema = true) @validate_schema = validate_schema end - def to_domain(domain, ns = nil, overwrite = false) - check_duplicity(domain, ns, @instance_name) + def to_domain(domain, namespace = nil, overwrite = false) + check_duplicity(domain, namespace, @instance_name) @overwrite = overwrite - @target_ns = ns.nil? ? @partial_ns : ns + @target_ns = namespace.nil? ? @partial_ns : namespace @target_name = @instance_name @target_domain = domain copy end - def as(new_name, ns = nil, overwrite = false) - check_duplicity(@src_domain, ns, new_name) + def as(new_name, namespace = nil, overwrite = false) + check_duplicity(@src_domain, namespace, new_name) @overwrite = overwrite - @target_ns = ns.nil? ? @partial_ns : ns + @target_ns = namespace.nil? ? @partial_ns : namespace @target_name = new_name @target_domain = @src_domain copy end - def self.copy_multiple(ids, domain, ns = nil, overwrite = false) + def self.copy_multiple(ids, domain, namespace = nil, overwrite = false) validate_flag = true nids = [] MiqAeInstance.transaction do ids.each do |id| instance_obj = MiqAeInstance.find(id) - new_instance = new(instance_obj.fqname, validate_flag).to_domain(domain, ns, overwrite) + new_instance = new(instance_obj.fqname, validate_flag).to_domain(domain, namespace, overwrite) nids << new_instance.id if new_instance validate_flag = false end @@ -109,9 +109,9 @@ def validate raise "Instance cannot be copied, automation class schema mismatch" if (@flags & @class_schema_status).zero? end - def check_duplicity(domain, ns, instance_name) + def check_duplicity(domain, namespace, instance_name) if domain.downcase == @src_domain.downcase && instance_name.downcase == @instance_name.downcase - raise "Cannot copy instance onto itself" if ns.nil? || ns.downcase == @partial_ns.downcase + raise "Cannot copy instance onto itself" if namespace.nil? || namespace.downcase == @partial_ns.downcase end end end diff --git a/app/models/miq_ae_method_copy.rb b/app/models/miq_ae_method_copy.rb index d2edddc71..c51b2dd74 100644 --- a/app/models/miq_ae_method_copy.rb +++ b/app/models/miq_ae_method_copy.rb @@ -15,30 +15,30 @@ def initialize(method_fqname) @target_class_name = @ae_class end - def to_domain(domain, ns = nil, overwrite = false) - check_duplicity(domain, ns, @method_name) + def to_domain(domain, namespace = nil, overwrite = false) + check_duplicity(domain, namespace, @method_name) @overwrite = overwrite - @target_ns = ns.nil? ? @partial_ns : ns + @target_ns = namespace.nil? ? @partial_ns : namespace @target_name = @method_name @target_domain = domain copy end - def as(new_name, ns = nil, overwrite = false) - check_duplicity(@src_domain, ns, new_name) + def as(new_name, namespace = nil, overwrite = false) + check_duplicity(@src_domain, namespace, new_name) @overwrite = overwrite - @target_ns = ns.nil? ? @partial_ns : ns + @target_ns = namespace.nil? ? @partial_ns : namespace @target_name = new_name @target_domain = @src_domain copy end - def self.copy_multiple(ids, domain, ns = nil, overwrite = false) + def self.copy_multiple(ids, domain, namespace = nil, overwrite = false) nids = [] MiqAeMethod.transaction do ids.each do |id| method_obj = MiqAeMethod.find(id) - new_method = new(method_obj.fqname).to_domain(domain, ns, overwrite) + new_method = new(method_obj.fqname).to_domain(domain, namespace, overwrite) nids << new_method.id if new_method end end @@ -91,9 +91,9 @@ def validate find_or_create_class end - def check_duplicity(domain, ns, method_name) + def check_duplicity(domain, namespace, method_name) if domain.downcase == @src_domain.downcase && method_name.downcase == @method_name.downcase - raise "Cannot copy method onto itself" if ns.nil? || ns.downcase == @partial_ns.downcase + raise "Cannot copy method onto itself" if namespace.nil? || namespace.downcase == @partial_ns.downcase end end end diff --git a/app/models/miq_ae_yaml_import_consolidated.rb b/app/models/miq_ae_yaml_import_consolidated.rb index bd76f1b55..b13d18e71 100644 --- a/app/models/miq_ae_yaml_import_consolidated.rb +++ b/app/models/miq_ae_yaml_import_consolidated.rb @@ -41,7 +41,7 @@ def domain_files(domain) return [] if keys.empty? keys.select! do |key| - File.fnmatch(domain, key, @fn_flags) && @yaml_model.has_key_path?(*[key, DOMAIN_YAML_FILENAME]) + File.fnmatch(domain, key, @fn_flags) && @yaml_model.has_key_path?(key, DOMAIN_YAML_FILENAME) end keys.collect { |key| "#{key}/#{DOMAIN_YAML_FILENAME}" } end @@ -83,7 +83,7 @@ def load_class_schema(class_folder) def get_filenames(parent_path, file_name) paths = get_filenames_with_proc(parent_path) do |key, hash| - hash.has_key_path?(*[key, file_name]) + hash.has_key_path?(key, file_name) end paths.collect { |path| "#{path}/#{file_name}" } end diff --git a/lib/miq_automation_engine/engine/miq_ae_engine.rb b/lib/miq_automation_engine/engine/miq_ae_engine.rb index 70fdded92..76acce8e8 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine.rb @@ -56,15 +56,15 @@ def self.deliver_queue(args, options = {}) automation_object_options end - private_class_method def self.change_options_by_ws(options, ws) + private_class_method def self.change_options_by_ws(options, workspace) options.delete(:ae_state_data) options.delete(:ae_state_previous) - options[:state] = ws.root['ae_state'] || options[:state] - options[:ae_fsm_started] = ws.root['ae_fsm_started'] - options[:ae_state_started] = ws.root['ae_state_started'] - options[:ae_state_retries] = ws.root['ae_state_retries'] - options[:ae_state_data] = YAML.dump(ws.persist_state_hash) unless ws.persist_state_hash.empty? - options[:ae_state_previous] = YAML.dump(ws.current_state_info) unless ws.current_state_info.empty? + options[:state] = workspace.root['ae_state'] || options[:state] + options[:ae_fsm_started] = workspace.root['ae_fsm_started'] + options[:ae_state_started] = workspace.root['ae_state_started'] + options[:ae_state_retries] = workspace.root['ae_state_retries'] + options[:ae_state_data] = YAML.dump(workspace.persist_state_hash) unless workspace.persist_state_hash.empty? + options[:ae_state_previous] = YAML.dump(workspace.current_state_info) unless workspace.current_state_info.empty? end def self.deliver(*args) @@ -142,27 +142,27 @@ def self.return_result(workspace, options) end end - def self.format_benchmark_counts(bm) + def self.format_benchmark_counts(benchmark) formatted = '' - bm.keys.select { |k| k.to_s.downcase =~ /_count$/ }.sort_by(&:to_s).each do |k| + benchmark.keys.select { |k| k.to_s.downcase =~ /_count$/ }.sort_by(&:to_s).each do |k| formatted << ', ' unless formatted.blank? - formatted << "#{k}=>#{bm[k]}" + formatted << "#{k}=>#{benchmark[k]}" end "{#{formatted}}" end BENCHMARK_TIME_THRESHOLD_PERCENT = 5.0 / 100 - def self.format_benchmark_times(bm) + def self.format_benchmark_times(benchmark) formatted = '' - total_time = bm[:total_time] + total_time = benchmark[:total_time] threshold = 0 # show everything threshold = (total_time * BENCHMARK_TIME_THRESHOLD_PERCENT) if total_time.kind_of?(Numeric) # only show times > threshold of the total - bm.keys.select { |k| k.to_s.downcase =~ /_time$/ }.sort_by(&:to_s).each do |k| - next unless bm[k] >= threshold + benchmark.keys.select { |k| k.to_s.downcase =~ /_time$/ }.sort_by(&:to_s).each do |k| + next unless benchmark[k] >= threshold formatted << ', ' unless formatted.blank? - formatted << "#{k}=>#{bm[k]}" + formatted << "#{k}=>#{benchmark[k]}" end "{#{formatted}}" end diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_ansible_method_base.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_ansible_method_base.rb index 8911f3832..1af814cba 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_ansible_method_base.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_ansible_method_base.rb @@ -161,7 +161,7 @@ def method_manageiq_env end def method_key - @method_key_value ||= "#{@aem.name}#{METHOD_KEY_SUFFIX}".gsub(/\s+/, "") + @method_key ||= "#{@aem.name}#{METHOD_KEY_SUFFIX}".gsub(/\s+/, "") end def serialize_workspace diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_domain_search.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_domain_search.rb index 4e7663f27..e06f768ee 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_domain_search.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_domain_search.rb @@ -7,51 +7,53 @@ def initialize @prepend_namespace = nil end - def prepend_namespace=(ns) - @prepend_namespace = ns.chomp('/').sub(%r{^/}, '') + def prepend_namespace=(namespace) + @prepend_namespace = namespace.chomp('/').sub(%r{^/}, '') $miq_ae_logger.info("Prepend namespace [#{@prepend_namespace}] during domain search") end def ae_user=(obj) - @sorted_domains ||= obj.current_tenant.enabled_domains.collect(&:name) + @sorted_domains ||= obj.current_tenant.enabled_domains.collect(&:name) # rubocop:disable Naming/MemoizedInstanceVariableName end - def get_alternate_domain(scheme, uri, ns, klass, instance) - return ns if ns.nil? || klass.nil? - return ns if scheme != "miqaedb" - return ns if @fqns_id_cache.key?(ns) + def get_alternate_domain(scheme, uri, namespace, klass, instance) + return namespace if namespace.nil? || klass.nil? + return namespace if scheme != "miqaedb" + return namespace if @fqns_id_cache.key?(namespace) - search(uri, ns, klass, instance, nil) + search(uri, namespace, klass, instance, nil) end - def get_alternate_domain_method(scheme, uri, ns, klass, method) - return ns if ns.nil? || klass.nil? - return ns if scheme != "miqaedb" - return ns if @fqns_id_cache.key?(ns) + def get_alternate_domain_method(scheme, uri, namespace, klass, method) + return namespace if namespace.nil? || klass.nil? - search(uri, ns, klass, nil, method) + return namespace if scheme != "miqaedb" + + return namespace if @fqns_id_cache.key?(namespace) + + search(uri, namespace, klass, nil, method) end private - def search(uri, ns, klass, instance, method) - unless @partial_ns.include?(ns) - fqns = MiqAeNamespace.lookup_by_fqname(ns, false) + def search(uri, namespace, klass, instance, method) + unless @partial_ns.include?(namespace) + fqns = MiqAeNamespace.lookup_by_fqname(namespace, false) if fqns && !fqns.domain? - @fqns_id_cache[ns] = fqns.id - return ns + @fqns_id_cache[namespace] = fqns.id + return namespace end end - @partial_ns << ns unless @partial_ns.include?(ns) - updated_ns = find_first_fq_domain(uri, "#{@prepend_namespace}/#{ns}", klass, instance, method) if @prepend_namespace - updated_ns ||= find_first_fq_domain(uri, ns, klass, instance, method) - updated_ns || ns + @partial_ns << namespace unless @partial_ns.include?(namespace) + updated_ns = find_first_fq_domain(uri, "#{@prepend_namespace}/#{namespace}", klass, instance, method) if @prepend_namespace + updated_ns ||= find_first_fq_domain(uri, namespace, klass, instance, method) + updated_ns || namespace end - def find_first_fq_domain(uri, ns, klass, instance, method) + def find_first_fq_domain(uri, namespace, klass, instance, method) # Check if the namespace, klass and instance exist if it does # swap out the namespace - parts = ns.split('/') + parts = namespace.split('/') parts.unshift("") matching_domain = get_matching_domain(parts, klass, instance, method) matching_domain ||= get_matching_domain(parts, klass, MiqAeObject::MISSING_INSTANCE, method) diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_method.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_method.rb index 08fa8915d..c5362dfa0 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_method.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_method.rb @@ -129,13 +129,13 @@ def self.open_transactions_threshold end private_class_method :open_transactions_threshold - def self.verbose_rc(rc) - case rc + def self.verbose_rc(return_code) + case return_code when MIQ_OK then 'MIQ_OK' when MIQ_WARN then 'MIQ_WARN' when MIQ_STOP then 'MIQ_STOP' when MIQ_ABORT then 'MIQ_ABORT' - else "Unknown RC: [#{rc}]" + else "Unknown RC: [#{return_code}]" end end private_class_method :verbose_rc @@ -167,8 +167,8 @@ def self.with_automation_env end private_class_method :with_automation_env - def self.process_ruby_method_results(rc, msg) - case rc + def self.process_ruby_method_results(return_code, msg) + case return_code when MIQ_OK $miq_ae_logger.info(msg) when MIQ_WARN @@ -180,7 +180,7 @@ def self.process_ruby_method_results(rc, msg) else raise MiqAeException::UnknownMethodRc, msg end - rc + return_code end private_class_method :process_ruby_method_results diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb index 7d0444f0b..071a93d87 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb @@ -42,11 +42,11 @@ class MiqAeObject attr_accessor :node_parent attr_reader :node_children - def initialize(workspace, ns, klass, instance, object_name = nil) + def initialize(workspace, namespace, klass, instance, object_name = nil) Benchmark.current_realtime[:object_count] += 1 @workspace = workspace - @namespace = ns + @namespace = namespace @klass = klass @instance = instance @attributes = {} @@ -148,11 +148,11 @@ def initialize(workspace, ns, klass, instance, object_name = nil) end end - def fetch_namespace(ns = @namespace) + def fetch_namespace(namespace = @namespace) Benchmark.current_realtime[:fetch_namespace_count] += 1 Benchmark.realtime_block(:fetch_namespace_time) do - @workspace.datastore(ns.downcase, :namespace) do - MiqAeNamespace.lookup_by_fqname(ns) + @workspace.datastore(namespace.downcase, :namespace) do + MiqAeNamespace.lookup_by_fqname(namespace) end end.first end @@ -179,10 +179,10 @@ def fetch_instance(iname) end.first end - def fetch_field_value(f) + def fetch_field_value(field) Benchmark.current_realtime[:fetch_field_value_count] += 1 Benchmark.realtime_block(:fetch_field_value_time) do - @aei&.get_field_value(f, false) + @aei&.get_field_value(field, false) end.first end @@ -350,12 +350,12 @@ def children(name = nil) @rels[name] end - def self.fqname(ns, klass, instance) - MiqAePath.new(:ae_namespace => ns, :ae_class => klass, :ae_instance => instance).to_s + def self.fqname(namespace, klass, instance) + MiqAePath.new(:ae_namespace => namespace, :ae_class => klass, :ae_instance => instance).to_s end - def process_relationship(f, message, args) - process_relationship_raw(get_value(f, :aetype_relationship), message, args, f['name'], f['collect']) + def process_relationship(field, message, args) + process_relationship_raw(get_value(field, :aetype_relationship), message, args, field['name'], field['collect']) end def process_method_raw(method, collect = nil) @@ -374,12 +374,12 @@ def process_method_raw(method, collect = nil) end end - def process_method(f, _message, _args) - process_method_raw(get_value(f), f['collect']) + def process_method(field, _message, _args) + process_method_raw(get_value(field), field['collect']) end def process_method_via_uri(uri) - scheme, userinfo, host, port, registry, path, opaque, query, fragment = MiqAeUri.split(uri) + _scheme, _userinfo, _host, _port, _registry, path, _opaque, query, _fragment = MiqAeUri.split(uri) parts = path.split(PATH_SEPARATOR) parts.shift # Remove the leading blank piece method_name = parts.pop @@ -410,7 +410,7 @@ def fetch_object_attribute(path, name, required = false) end def uri2value(uri, required = false) - scheme, userinfo, host, port, registry, path, opaque, query, fragment = MiqAeUri.split(uri) + scheme, _userinfo, _host, _port, _registry, path, _opaque, _query, fragment = MiqAeUri.split(uri) if scheme == 'miqaedb' ns, klass, instance, attribute_name = MiqAePath.split(path, :has_attribute_name => true) @@ -473,6 +473,38 @@ def get_value(field, type = nil, required = false) field['datatype'] == MiqAeField::NULL_COALESCING_DATATYPE ? get_null_coalesced_value(field) : get_field_value(field, type, required) end + def self.convert_boolean_value(value) + return true if value.to_s.downcase == 'true' || value == '1' + + return false if value.to_s.downcase == 'false' || value == '0' + + value + end + + def self.convert_value_based_on_datatype(value, datatype) + return value if value.blank? || datatype.nil? + + # Basic Types + return convert_boolean_value(value) if datatype == 'boolean' + return true if datatype == 'TrueClass' + return false if datatype == 'FalseClass' + return Time.parse(value).getlocal if 'time'.casecmp?(datatype) + return value.to_sym if 'symbol'.casecmp?(datatype) + return value.to_i if 'integer'.casecmp?(datatype) || datatype == 'Fixnum' + return value.to_f if 'float'.casecmp?(datatype) + return value.gsub(/[\[\]]/, '').strip.split(/\s*,\s*/) if datatype == 'array' && value.class == String + return decrypt_password(value) if datatype == 'password' + + if (service_model = "MiqAeMethodService::MiqAeService#{SM_LOOKUP[datatype]}".safe_constantize) + return service_model.find(value) + end + + raise MiqAeException::InvalidClass unless MiqAeField.available_datatypes.include?(datatype) + + # default datatype => 'string' + value + end + private def call_method(obj, method) @@ -495,7 +527,6 @@ def invoke_method(namespace, klass, method_name, args) aem = @instance_methods[method_name.downcase] if klass.nil? # If not found in instance methods, look in class methods - namespace_provided = namespace namespace ||= @namespace klass ||= @klass fq = MiqAeClass.fqname(namespace, klass) @@ -531,15 +562,15 @@ def method_override(namespace, klass, method_name, aem) aem end - def get_field_value(f, type = nil, required = false) - value = f['value'] - value = f['default_value'] if value.blank? - value = substitute_value(value, type, required) if f['substitute'] + def get_field_value(field, type = nil, required = false) + value = field['value'] + value = field['default_value'] if value.blank? + value = substitute_value(value, type, required) if field['substitute'] value end - def get_null_coalesced_value(f, type = nil) - initial_value = f['value'] || f['default_value'] + def get_null_coalesced_value(field, type = nil) + initial_value = field['value'] || field['default_value'] return nil unless initial_value result = nil @@ -558,37 +589,6 @@ def resolve_value(value, type) nil end - def self.convert_boolean_value(value) - return true if value.to_s.downcase == 'true' || value == '1' - return false if value.to_s.downcase == 'false' || value == '0' - - value - end - - def self.convert_value_based_on_datatype(value, datatype) - return value if value.blank? || datatype.nil? - - # Basic Types - return convert_boolean_value(value) if datatype == 'boolean' - return true if datatype == 'TrueClass' - return false if datatype == 'FalseClass' - return Time.parse(value).getlocal if 'time'.casecmp?(datatype) - return value.to_sym if 'symbol'.casecmp?(datatype) - return value.to_i if 'integer'.casecmp?(datatype) || datatype == 'Fixnum' - return value.to_f if 'float'.casecmp?(datatype) - return value.gsub(/[\[\]]/, '').strip.split(/\s*,\s*/) if datatype == 'array' && value.class == String - return decrypt_password(value) if datatype == 'password' - - if (service_model = "MiqAeMethodService::MiqAeService#{SM_LOOKUP[datatype]}".safe_constantize) - return service_model.find(value) - end - - raise MiqAeException::InvalidClass unless MiqAeField.available_datatypes.include?(datatype) - - # default datatype => 'string' - value - end - def self.decrypt_password(value) MiqAePassword.new(MiqAePassword.decrypt(value)) rescue ManageIQ::Password::PasswordError => err @@ -597,10 +597,10 @@ def self.decrypt_password(value) end private_class_method :decrypt_password - def process_assertion(f, message, args) + def process_assertion(field, message, args) Benchmark.current_realtime[:assertion_count] += 1 Benchmark.realtime_block(:assertion_time) do - assertion = get_value(f, :aetype_assertion, true) + assertion = get_value(field, :aetype_assertion, true) return if assertion.blank? $miq_ae_logger.info("Evaluating substituted assertion [#{assertion}]") @@ -620,13 +620,13 @@ def process_assertion(f, message, args) end end - def process_attribute(f, _message, _args, value = nil) + def process_attribute(field, _message, _args, value = nil) Benchmark.current_realtime[:attribute_count] += 1 Benchmark.realtime_block(:attribute_time) do - value = get_value(f) if value.nil? - value = MiqAeObject.convert_value_based_on_datatype(value, f['datatype']) - @attributes[f['name'].downcase] = value unless value.nil? - process_collect(f['collect'], nil) unless f['collect'].blank? + value = get_value(field) if value.nil? + value = MiqAeObject.convert_value_based_on_datatype(value, field['datatype']) + @attributes[field['name'].downcase] = value unless value.nil? + process_collect(field['collect'], nil) if field['collect'].present? end end @@ -665,11 +665,11 @@ def process_collects(what, rels) def process_collect(expr, rels) Benchmark.current_realtime[:collect_count] += 1 Benchmark.realtime_block(:collect_time) do - if result = RE_COLLECT_ARRAY.match(expr) + if (result = RE_COLLECT_ARRAY.match(expr)) process_collect_array(expr, rels, result) - elsif result = RE_COLLECT_HASH.match(expr) + elsif (result = RE_COLLECT_HASH.match(expr)) process_collect_hash(expr, rels, result) - elsif result = RE_COLLECT_STRING.match(expr) + elsif (result = RE_COLLECT_STRING.match(expr)) process_collect_string(expr, rels, result) else raise MiqAeException::InvalidCollection, "invalid collect item: <#{expr}>" @@ -677,11 +677,11 @@ def process_collect(expr, rels) end.first end - def process_collect_set_attribute(k, v) - k = @current_field['name'] if k.nil? && @current_field.kind_of?(Hash) - return v if k.nil? + def process_collect_set_attribute(key, value) + key = @current_field['name'] if key.nil? && @current_field.kind_of?(Hash) + return value if key.nil? - parts = k.split(PATH_SEPARATOR) + parts = key.split(PATH_SEPARATOR) left = parts.pop if parts.empty? obj = self @@ -689,7 +689,7 @@ def process_collect_set_attribute(k, v) path = parts.first.blank? ? PATH_SEPARATOR : parts.join(PATH_SEPARATOR) obj = @workspace.get_obj_from_path(path) end - obj.attributes[left.downcase] = v unless obj.nil? + obj.attributes[left.downcase] = value unless obj.nil? end def process_collect_array(_expr, rels, result) @@ -738,7 +738,6 @@ def array_value(array, method) def process_collect_hash(expr, rels, result) lh = result[1].strip unless result[1].nil? contents = result[2].strip - method = result[3].strip.downcase unless result[3].nil? hash = {} hashes = contents.split(ENUM_SEPARATOR) @@ -784,8 +783,7 @@ def process_collect_hash(expr, rels, result) def process_collect_string(_expr, rels, result) cattr = result[1].strip unless result[1].nil? name = result[2].strip - method = result[3].strip.downcase unless result[3].nil? - cattr ||= name unless rels.nil? # Set cattr to name ONLY if coming from relationship + cattr ||= name unless rels.nil? # Set cattr to name ONLY if coming from relationship value = if rels.kind_of?(Array) rels.collect { |r| r[name] } @@ -838,7 +836,7 @@ def fqmethod2components(str) def method_parms_to_hash(str) h = {} - while result = RE_HASH.match(str) + while (result = RE_HASH.match(str)) key = result[1] value = result[2] h[key] = classify_value(value) diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object_lookup.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object_lookup.rb index 89b8e94b6..2a08e26c5 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object_lookup.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object_lookup.rb @@ -31,12 +31,12 @@ def find_best_match(ientries, query) match[:object] if match end - def ns_match?(ns, entry) - ns == entry[:namespace] + def ns_match?(namespace, entry) + namespace == entry[:namespace] end - def fq_match?(domain, ns, entry) - domain == entry[:domain] && ns == entry[:namespace] + def fq_match?(domain, namespace, entry) + domain == entry[:domain] && namespace == entry[:namespace] end def instance_match?(instance, entry) diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_path.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_path.rb index eb2cb951b..016e9a809 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_path.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_path.rb @@ -31,10 +31,10 @@ def self.parse(path, options = {}) new(*split(path, options)) end - def self.join(ns, klass, instance, attribute_name = nil) - return [nil, ns, klass, instance].join("/") if attribute_name.nil? + def self.join(namespace, klass, instance, attribute_name = nil) + return [nil, namespace, klass, instance].join("/") if attribute_name.nil? - [nil, ns, klass, instance, attribute_name].join("/") + [nil, namespace, klass, instance, attribute_name].join("/") end def self.split(path, options = {}) diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_state_machine.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_state_machine.rb index 26fbc3c22..d3d0fe897 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_state_machine.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_state_machine.rb @@ -2,8 +2,8 @@ module MiqAeEngine module MiqAeStateMachine STATE_METHOD_REGEX = Regexp.new(/^METHOD::(.*$)/i) - def state_runnable?(f) - return false unless @workspace.root['ae_state'] == f['name'] + def state_runnable?(field) + return false unless @workspace.root['ae_state'] == field['name'] return false unless @workspace.root['ae_result'] == 'ok' true @@ -25,28 +25,29 @@ def increment_state_retries @workspace.root['ae_state_retries'] = @workspace.root['ae_state_retries'].to_i + 1 end - def enforce_state_maxima(f) - enforce_max_retries(f) - enforce_max_time(f) + def enforce_state_maxima(field) + enforce_max_retries(field) + enforce_max_time(field) end - def enforce_max_retries(f) - return if f['max_retries'].blank? - return if f['max_retries'].to_i >= @workspace.root['ae_state_retries'].to_i + def enforce_max_retries(field) + return if field['max_retries'].blank? - raise "number of retries <#{@workspace.root['ae_state_retries']}> exceeded maximum of <#{f['max_retries']}>" + return if field['max_retries'].to_i >= @workspace.root['ae_state_retries'].to_i + + raise "number of retries <#{@workspace.root['ae_state_retries']}> exceeded maximum of <#{field['max_retries']}>" end - def enforce_max_time(f) - return if f['max_time'].blank? + def enforce_max_time(field) + return if field['max_time'].blank? lapsed = Time.zone.now.utc - Time.zone.parse(@workspace.root['ae_state_started']) - return if f['max_time'].to_i_with_method > lapsed + return if field['max_time'].to_i_with_method > lapsed - raise "time in state <#{lapsed} seconds> exceeded maximum of <#{f['max_time']}>" + raise "time in state <#{lapsed} seconds> exceeded maximum of <#{field['max_time']}>" end - def process_state_step_with_error_handling(f, step = nil) + def process_state_step_with_error_handling(field, step = nil) current_state = @workspace.root['ae_state'] yield if @workspace.root['ae_next_state'].present? && current_state != @workspace.root['ae_next_state'] @@ -54,54 +55,54 @@ def process_state_step_with_error_handling(f, step = nil) @workspace.root['ae_result'] = 'skip' if step == 'on_entry' end rescue MiqAeException::StopInstantiation => e - $miq_ae_logger.error("State=<#{f['name']}> running #{step} raised exception: <#{e.message}>") + $miq_ae_logger.error("State=<#{field['name']}> running #{step} raised exception: <#{e.message}>") raise rescue StandardError => e - error_message = "State=<#{f['name']}> running #{step} raised exception: <#{e.message}>" + error_message = "State=<#{field['name']}> running #{step} raised exception: <#{e.message}>" $miq_ae_logger.error(error_message) @workspace.root['ae_reason'] = error_message @workspace.root['ae_result'] = 'error' end - def process_state(f, message, args) + def process_state(field, message, args) Benchmark.current_realtime[:state_count] += 1 Benchmark.realtime_block(:state_time) do # Initialize the ae_state and ae_result variables, if blank - @workspace.root['ae_state'] = f['name'] if @workspace.root['ae_state'].blank? + @workspace.root['ae_state'] = field['name'] if @workspace.root['ae_state'].blank? @workspace.root['ae_result'] = 'ok' if @workspace.root['ae_result'].blank? @workspace.root['ae_next_state'] = '' @workspace.root['ae_retry_server_affinity'] = false # Do not proceed further unless this state is runnable - return unless state_runnable?(f) + return unless state_runnable?(field) # Ensure the metadata to deal with retries and timeouts is initialized - initialize_state_maxima_metadata(f) + initialize_state_maxima_metadata(field) # Process on_entry method - process_state_step_with_error_handling(f, 'on_entry') { process_state_method(f, 'on_entry') } + process_state_step_with_error_handling(field, 'on_entry') { process_state_method(field, 'on_entry') } # Re-verify (in case on-entry method changed anything) - process_state_step_with_error_handling(f) { process_state_relationship(f, message, args) } if state_runnable?(f) + process_state_step_with_error_handling(field) { process_state_relationship(field, message, args) } if state_runnable?(field) # Check the ae_result and set the next state appropriately if @workspace.root['ae_result'] == 'ok' - $miq_ae_logger.info("Processed State=[#{f['name']}]") + $miq_ae_logger.info("Processed State=[#{field['name']}]") elsif @workspace.root['ae_result'] == 'skip' - $miq_ae_logger.warn("Skipping State=[#{f['name']}]") - return set_next_state(f, message) + $miq_ae_logger.warn("Skipping State=[#{field['name']}]") + return set_next_state(field, message) elsif %w[retry restart async_launch].include?(@workspace.root['ae_result']) increment_state_retries elsif @workspace.root['ae_result'] == 'error' - $miq_ae_logger.warn("Error in State=[#{f['name']}]") + $miq_ae_logger.warn("Error in State=[#{field['name']}]") # Process on_error method - return process_state_step_with_error_handling(f, 'on_error') do - process_state_method(f, 'on_error') + return process_state_step_with_error_handling(field, 'on_error') do + process_state_method(field, 'on_error') if @workspace.root['ae_result'] == 'continue' - $miq_ae_logger.warn("Resetting Error in State=[#{f['name']}]") + $miq_ae_logger.warn("Resetting Error in State=[#{field['name']}]") @workspace.root['ae_result'] = 'ok' - set_next_state(f, message) + set_next_state(field, message) end end end @@ -111,49 +112,49 @@ def process_state(f, message, args) if @workspace.root['ae_result'] == 'async_launch' @workspace.root['ae_result'] = 'retry' else - process_state_step_with_error_handling(f, 'on_exit') do - process_state_method(f, 'on_exit') + process_state_step_with_error_handling(field, 'on_exit') do + process_state_method(field, 'on_exit') if @workspace.root['ae_result'] == 'retry' && !state_in_retry increment_state_retries end end end - set_next_state(f, message) + set_next_state(field, message) end end - def process_state_relationship(f, message, args) - relationship = get_value(f, :aetype_relationship) + def process_state_relationship(field, message, args) + relationship = get_value(field, :aetype_relationship) unless relationship.blank? || relationship.lstrip[0, 1] == '#' - $miq_ae_logger.info("Processing State=[#{f['name']}]") + $miq_ae_logger.info("Processing State=[#{field['name']}]") @workspace.root['ae_state_step'] = 'main' - enforce_state_maxima(f) + enforce_state_maxima(field) match_data = STATE_METHOD_REGEX.match(relationship) if match_data process_method_raw(match_data[1]) else - process_relationship_raw(relationship, message, args, f['name'], f['collect']) - raise MiqAeException::MiqAeDatastoreError, "empty relationship" unless @rels[f['name']] + process_relationship_raw(relationship, message, args, field['name'], field['collect']) + raise MiqAeException::MiqAeDatastoreError, "empty relationship" unless @rels[field['name']] end - $miq_ae_logger.info("Processed State=[#{f['name']}] with Result=[#{@workspace.root['ae_result']}]") + $miq_ae_logger.info("Processed State=[#{field['name']}] with Result=[#{@workspace.root['ae_result']}]") end end - def process_state_method(f, method_name) - if f[method_name].present? - f[method_name].split(";").each do |method| - method = substitute_value(method.strip) - next if method.blank? || method.lstrip[0, 1] == '#' - - $miq_ae_logger.info("In State=[#{f['name']}], invoking [#{method_name}] method=[#{method}]") - @workspace.root['ae_status_state'] = method_name - @workspace.root['ae_state'] = f['name'] - @workspace.root['ae_state_step'] = method_name - process_method_raw(method) - end + def process_state_method(field, method_name) + return if field[method_name].blank? + + field[method_name].split(";").each do |method| + method = substitute_value(method.strip) + next if method.blank? || method.lstrip[0, 1] == '#' + + $miq_ae_logger.info("In State=[#{field['name']}], invoking [#{method_name}] method=[#{method}]") + @workspace.root['ae_status_state'] = method_name + @workspace.root['ae_state'] = field['name'] + @workspace.root['ae_state_step'] = method_name + process_method_raw(method) end rescue MiqAeException::MethodNotFound => err - raise MiqAeException::MethodNotFound, "In State=[#{f['name']}], #{method_name} #{err.message}" + raise MiqAeException::MethodNotFound, "In State=[#{field['name']}], #{method_name} #{err.message}" end def next_state(current, message) @@ -166,14 +167,14 @@ def next_state(current, message) @workspace.root['ae_next_state'].blank? ? states[index + 1] : states[index] end - def set_next_state(f, message) + def set_next_state(field, message) if %w[skip ok].include?(@workspace.root['ae_result']) - @workspace.root['ae_state'] = next_state(f['name'], message).to_s + @workspace.root['ae_state'] = next_state(field['name'], message).to_s reset_state_maxima_metadata $miq_ae_logger.info("Next State=[#{@workspace.root['ae_state']}]") @workspace.root['ae_result'] = 'ok' elsif @workspace.root['ae_result'] == 'restart' - $miq_ae_logger.info("State=[#{f['name']}] has requested a restart") + $miq_ae_logger.info("State=[#{field['name']}] has requested a restart") @workspace.root['ae_result'] = 'retry' @workspace.root['ae_state'] = restart_state(message).to_s $miq_ae_logger.info("Will restart at State=[#{@workspace.root['ae_state']}]") diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb index 074eea60f..b5a6af58c 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb @@ -32,8 +32,8 @@ def readonly? @readonly end - def self.current=(ws) - Thread.current[:current_workspace] = ws + def self.current=(workspace) + Thread.current[:current_workspace] = workspace end def self.current @@ -53,7 +53,8 @@ def self.instantiate_with_user(uri, user, attrs) self.current = workspace workspace.instantiate(uri, user, nil) workspace - rescue MiqAeException + rescue MiqAeException => err + $miq_ae_logger.error(err.message) ensure clear_stored_workspace end @@ -89,7 +90,7 @@ def varget(uri) end def varset(uri, value) - scheme, userinfo, host, port, registry, path, opaque, query, fragment = MiqAeUri.split(uri) + scheme, _userinfo, _host, _port, _registry, path, _opaque, _query, fragment = MiqAeUri.split(uri) if scheme == "miqaews" o = get_obj_from_path(path) raise MiqAeException::ObjectNotFound, "Object Not Found for path=[#{path}]" if o.nil? @@ -104,7 +105,7 @@ def instantiate(uri, user, root = nil) $miq_ae_logger.info("Instantiating [#{ManageIQ::Password.sanitize_string(uri)}]") if root.nil? @ae_user = user @dom_search.ae_user = user - scheme, userinfo, host, port, registry, path, opaque, query, fragment = MiqAeUri.split(uri, "miqaedb") + scheme, _userinfo, _host, _port, _registry, path, _opaque, query, fragment = MiqAeUri.split(uri, "miqaedb") raise MiqAeException::InvalidPathFormat, "Unsupported Scheme [#{scheme}]" unless MiqAeUri.scheme_supported?(scheme) raise MiqAeException::InvalidPathFormat, "Invalid URI <#{uri}>" if path.nil? @@ -217,20 +218,19 @@ def to_dot(path = nil) g = GraphViz.new("MiqAeWorkspace", :type => "digraph", :output => "dot") objs.each { |obj| obj_to_dot(g, obj) } - s = g.output(:output => "none") + g.output(:output => "none") end - def obj_to_dot(g, obj) + def obj_to_dot(graph, obj) return nil if obj.nil? - - o = g.add_node(obj.object_name) + o = graph.add_node(obj.object_name) # o["MiqAeClass"] = obj.klass # o["MiqAeNamespace"] = obj.namespace # o["MiqAeInstance"] = obj.instance # obj.attributes obj.children.each do |child| - c = obj_to_dot(g, child) - g.add_edge(o, c) unless c.nil? + c = obj_to_dot(graph, child) + graph.add_edge(o, c) unless c.nil? end o end @@ -246,10 +246,10 @@ def to_hash(obj) result.delete_if { |_k, v| v.nil? } end - def cyclical?(ns, klass, instance, message) + def cyclical?(namespace, klass, instance, message) # check for cyclical references @current.each do |c| - hash = {:ns => ns, :klass => klass, :instance => instance, :message => message} + hash = {:ns => namespace, :klass => klass, :instance => instance, :message => message} return true if hash.all? do |key, value| begin value.casecmp(c[key]).zero? @@ -366,12 +366,12 @@ def find_named_ancestor(path) obj end - def overlay_namespace(scheme, uri, ns, klass, instance) - @dom_search.get_alternate_domain(scheme, uri, ns, klass, instance) + def overlay_namespace(scheme, uri, namespace, klass, instance) + @dom_search.get_alternate_domain(scheme, uri, namespace, klass, instance) end - def overlay_method(ns, klass, method) - @dom_search.get_alternate_domain_method('miqaedb', "#{ns}/#{klass}/#{method}", ns, klass, method) + def overlay_method(namespace, klass, method) + @dom_search.get_alternate_domain_method('miqaedb', "#{namespace}/#{klass}/#{method}", namespace, klass, method) end private diff --git a/lib/miq_automation_engine/engine/miq_ae_event.rb b/lib/miq_automation_engine/engine/miq_ae_event.rb index 75bcf6665..8d654f661 100644 --- a/lib/miq_automation_engine/engine/miq_ae_event.rb +++ b/lib/miq_automation_engine/engine/miq_ae_event.rb @@ -7,13 +7,28 @@ def self.raise_ems_event(event) } if event.source == 'VC' - aevent.merge!('ExtManagementSystem::ems' => event.ext_management_system.id, :ems_id => event.ext_management_system.id) unless event.ext_management_system.nil? + unless event.ext_management_system.nil? + aevent['ExtManagementSystem::ems'] = event.ext_management_system.id + aevent[:ems_id] = event.ext_management_system.id + end end - aevent.merge!('VmOrTemplate::vm' => event.src_vm_or_template.id, :vm_id => event.src_vm_or_template.id) unless event.src_vm_or_template.nil? - aevent.merge!('VmOrTemplate::dest_vm' => event.dest_vm_or_template.id, :dest_vm_id => event.dest_vm_or_template.id) unless event.dest_vm_or_template.nil? - aevent.merge!('Host::host' => event.src_host.id, :host_id => event.src_host.id) unless event.src_host.nil? - aevent.merge!('Host::dest_host' => event.dest_host.id, :dest_host_id => event.dest_host.id) unless event.dest_host.nil? + unless event.src_vm_or_template.nil? + aevent['VmOrTemplate::vm'] = event.src_vm_or_template.id + aevent[:vm_id] = event.src_vm_or_template.id + end + unless event.dest_vm_or_template.nil? + aevent['VmOrTemplate::dest_vm'] = event.dest_vm_or_template.id + aevent[:dest_vm_id] = event.dest_vm_or_template.id + end + unless event.src_host.nil? + aevent['Host::host'] = event.src_host.id + aevent[:host_id] = event.src_host.id + end + unless event.dest_host.nil? + aevent['Host::dest_host'] = event.dest_host.id + aevent[:dest_host_id] = event.dest_host.id + end call_automate(event, aevent, 'Event') end @@ -91,7 +106,7 @@ def self.build_evm_event(event, passed_inputs = {}) end def self.process_result(ae_result, aevent) - scheme, userinfo, host, port, registry, path, opaque, query, fragment = MiqAeEngine::MiqAeUri.split(ae_result) + scheme, _userinfo, _host, _port, _registry, _path, _opaque, query, _fragment = MiqAeEngine::MiqAeUri.split(ae_result) args = MiqAeEngine::MiqAeUri.query2hash(query) if scheme.casecmp('miqpeca').zero? @@ -114,6 +129,7 @@ def self.process_result(ae_result, aevent) MiqPolicy.enforce_policy(target, event_name, inputs) unless target.nil? end rescue URI::InvalidURIError => err + $miq_ae_logger.error(err.message) end def self.call_automate(obj, attrs, instance_name, options = {}) diff --git a/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service.rb b/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service.rb index b96c45f32..d50065758 100644 --- a/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service.rb +++ b/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service.rb @@ -32,15 +32,15 @@ def self.destroy(obj) @@current.delete(obj) end - def initialize(ws, inputs = {}, logger = $miq_ae_logger) + def initialize(workspace, inputs = {}, logger = $miq_ae_logger) @tracking_label = Thread.current["tracking_label"] @drb_server_references = [] @inputs = inputs - @workspace = ws - @persist_state_hash = ws.persist_state_hash + @workspace = workspace + @persist_state_hash = workspace.persist_state_hash @logger = logger self.class.add(self) - ws.disable_rbac + workspace.disable_rbac end delegate :enable_rbac, :disable_rbac, :rbac_enabled?, :to => :@workspace @@ -123,8 +123,8 @@ def delete_service_var(name) service_object.root_service.delete_service_vars_option(name) end - def prepend_namespace=(ns) - @workspace.prepend_namespace = ns + def prepend_namespace=(namespace) + @workspace.prepend_namespace = namespace end def instantiate(uri) @@ -181,11 +181,11 @@ def current end def root - @root_object ||= object("/") + @root ||= object("/") end def parent - @parent_object ||= object("..") + @parent ||= object("..") end def objects(aobj) @@ -201,14 +201,14 @@ def datastore def ldap end - def execute(m, *args) - User.with_user(@workspace.ae_user) { execute_with_user(m, *args) } + def execute(method_name, *args) + User.with_user(@workspace.ae_user) { execute_with_user(method_name, *args) } end - def execute_with_user(m, *args) + def execute_with_user(method_name, *args) # Since each request from DRb client could run in a separate thread # We have to set the current_user in every thread. - MiqAeServiceMethods.send(m, *args) + MiqAeServiceMethods.send(method_name, *args) rescue NoMethodError => err raise MiqAeException::MethodNotFound, err.message end diff --git a/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_methods.rb b/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_methods.rb index f2bf32b95..3dc6b5bd2 100644 --- a/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_methods.rb +++ b/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_methods.rb @@ -7,7 +7,7 @@ class MiqAeServiceMethods SYNCHRONOUS = Rails.env.test? - def self.send_email(to, from, subject, body, content_type: nil, cc: nil, bcc: nil) + def self.send_email(to, from, subject, body, content_type: nil, cc: nil, bcc: nil) # rubocop:disable Naming/UncommunicativeMethodParamName ar_method do meth = SYNCHRONOUS ? :deliver : :deliver_queue options = { @@ -75,7 +75,7 @@ def self.category_create(options = {}) ar_method do ar_options = {} options.each { |k, v| ar_options[k.to_sym] = v if Classification.column_names.include?(k.to_s) || k.to_s == 'name' } - cat = Classification.create_category!(ar_options) + Classification.create_category!(ar_options) true end end @@ -114,7 +114,7 @@ def self.tag_create(category, options = {}) ar_options = {} options.each { |k, v| ar_options[k.to_sym] = v if Classification.column_names.include?(k.to_s) || k.to_s == 'name' } - entry = cat.add_entry(ar_options) + cat.add_entry(ar_options) true end end diff --git a/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_model_base.rb b/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_model_base.rb index 9332deae7..9f6d6c10b 100644 --- a/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_model_base.rb +++ b/lib/miq_automation_engine/engine/miq_ae_method_service/miq_ae_service_model_base.rb @@ -4,6 +4,7 @@ module MiqAeMethodService class MiqAeServiceModelBase SERVICE_MODEL_PATH = ManageIQ::AutomationEngine::Engine.root.join("lib", "miq_automation_engine", "service_models") EXPOSED_ATTR_BLACK_LIST = [/password/, /^auth_key$/].freeze + NORMALIZED_PREFIX = 'normalized_'.freeze class << self include DRbUndumped # Ensure that Automate Method can get at the class itself over DRb end @@ -13,8 +14,8 @@ class << self include Vmdb::Logging include MiqAeMethodService::MiqAeServiceRbac - def self.method_missing(m, *args) - return wrap_results(filter_objects(model.send(m, *args))) if class_method_exposed?(m) + def self.method_missing(method_name, *args) + return wrap_results(filter_objects(model.send(method_name, *args))) if class_method_exposed?(method_name) super rescue ActiveRecord::RecordNotFound @@ -25,15 +26,15 @@ def self.respond_to_missing?(method_name, include_private = false) class_method_exposed?(method_name.to_sym) || super end - def self.allowed_find_method?(m) - return false if m.starts_with?('find_or_create') || m.starts_with?('find_or_initialize') + def self.allowed_find_method?(method_name) + return false if method_name.starts_with?('find_or_create') || method_name.starts_with?('find_or_initialize') - m.starts_with?('find', 'lookup_by') + method_name.starts_with?('find', 'lookup_by') end # Expose the ActiveRecord find, all, count, and first - def self.class_method_exposed?(m) - allowed_find_method?(m.to_s) || [:where, :find].include?(m) + def self.class_method_exposed?(method_name) + allowed_find_method?(method_name.to_s) || [:where, :find].include?(method_name) end private_class_method :class_method_exposed? @@ -246,14 +247,13 @@ def self.normalize(str) arr.join end - def method_missing(m, *args) + def method_missing(method_name, *args) # # Normalize result of any method call # e.g. normalized_ldap_group, will call ldap_group method and normalize the result # - prefix = 'normalized_' - if m.to_s.starts_with?(prefix) - method = m.to_s[prefix.length..-1] + if method_name.to_s.starts_with?(NORMALIZED_PREFIX) + method = method_name.to_s[NORMALIZED_PREFIX.length..-1] result = MiqAeServiceModelBase.wrap_results(object_send(method, *args)) return MiqAeServiceModelBase.normalize(result) end @@ -261,6 +261,15 @@ def method_missing(m, *args) super end + def respond_to_missing?(method_name, include_private = false) + if method_name.to_s.start_with?(NORMALIZED_PREFIX) + method_n = method_name.to_s[NORMALIZED_PREFIX.length..-1] + return object_send(:respond_to?, method_n, include_private) + end + + super + end + # @param obj [Integer,ActiveRecord::Base] The object id or ActiveRecord instance to wrap # in a service model def initialize(obj) diff --git a/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb b/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb index 4f08d0df9..31d937f88 100644 --- a/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb +++ b/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb @@ -30,7 +30,7 @@ def ae_user_identity ar_method { @object.ae_user_identity(@ae_user, @ae_user.current_group, @ae_user.current_tenant) } end - def method_missing(method_name, *args) + def method_missing(method_name, *args) # rubocop:disable Style/MethodMissingSuper ae_user_identity unless @ae_user args = convert_params_to_ar_model(args) results = object_send(method_name, *args) diff --git a/spec/engine/miq_ae_method_service/miq_ae_service_methods_spec.rb b/spec/engine/miq_ae_method_service/miq_ae_service_methods_spec.rb index 67384e8ce..008d769e6 100644 --- a/spec/engine/miq_ae_method_service/miq_ae_service_methods_spec.rb +++ b/spec/engine/miq_ae_method_service/miq_ae_service_methods_spec.rb @@ -114,7 +114,7 @@ def invoke_ae expect(invoke_ae.root(@ae_result_key)).to be_empty - v1 = FactoryBot.create(:vm_vmware, :ems_id => 42, :vendor => 'vmware') + FactoryBot.create(:vm_vmware, :ems_id => 42, :vendor => 'vmware') t1 = FactoryBot.create(:template_vmware, :ems_id => 42) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(Array) diff --git a/spec/miq_ae_path_spec.rb b/spec/miq_ae_path_spec.rb index 5854e9048..0c304ddf4 100644 --- a/spec/miq_ae_path_spec.rb +++ b/spec/miq_ae_path_spec.rb @@ -101,7 +101,7 @@ def assert_split(parts, assertions = parts, method_options = {}) end it "with embedded blanks in instance name" do - @parts[:ae_instance] == "test3%20with%20blank" + @parts[:ae_instance] = "test3%20with%20blank" assert_split(@parts) end diff --git a/spec/miq_ae_serialize_workspace_spec.rb b/spec/miq_ae_serialize_workspace_spec.rb index 2b2c9b7e2..cf701cd89 100644 --- a/spec/miq_ae_serialize_workspace_spec.rb +++ b/spec/miq_ae_serialize_workspace_spec.rb @@ -6,7 +6,7 @@ def initialize(workspace) @workspace = workspace end - def get_value(_f, type) + def get_value(_f, type) # rubocop:disable Naming/UncommunicativeMethodParamName @workspace.root[type] end diff --git a/spec/miq_ae_service_model_spec.rb b/spec/miq_ae_service_model_spec.rb index 102c6bfc4..1113e23ca 100644 --- a/spec/miq_ae_service_model_spec.rb +++ b/spec/miq_ae_service_model_spec.rb @@ -99,6 +99,16 @@ expect(account_provider.taggable?).to be_falsey end end + + describe 'missing methods' do + it `recognize missing methods name which starts with normalize_` do + expect(@ae_vm.respond_to?(:normalized_tags)).to be true + end + + it `does not recognize arbitraray missing methods name which starts with normalize_` do + expect(@ae_vm.respond_to?(:normalized_not_exist_method)).to be false + end + end end describe MiqAeMethodService::MiqAeServiceMiqAeDomain do diff --git a/spec/miq_ae_state_machine_new_spec.rb b/spec/miq_ae_state_machine_new_spec.rb index 98af7310c..3acb0f38e 100644 --- a/spec/miq_ae_state_machine_new_spec.rb +++ b/spec/miq_ae_state_machine_new_spec.rb @@ -8,7 +8,7 @@ def initialize(workspace) @workspace = workspace end - def get_value(_f, type) + def get_value(_f, type) # rubocop:disable Naming/UncommunicativeMethodParamName @workspace.root[type] end end diff --git a/spec/miq_ae_state_machine_spec.rb b/spec/miq_ae_state_machine_spec.rb index d889f5faf..f49fc4e7c 100644 --- a/spec/miq_ae_state_machine_spec.rb +++ b/spec/miq_ae_state_machine_spec.rb @@ -13,9 +13,9 @@ it "resolves a provision request (old-style)" do EvmSpecHelper.import_yaml_model(File.join(@model_data_dir, "state_machine"), @domain) - t0 = Time.now + # t0 = Time.now ws = MiqAeEngine.instantiate("/SYSTEM/EVENT/VM_PROVISION_REQUESTED", @user) - t1 = Time.now + # t1 = Time.now expect(ws).not_to be_nil expect(ws.root['ae_result']).to eq('ok') @@ -29,9 +29,9 @@ EvmSpecHelper.import_yaml_model(File.join(@model_data_dir, "state_machine"), @domain) MiqAeDatastore.reset_default_namespace - t0 = Time.now + # t0 = Time.now ws = MiqAeEngine.instantiate("/SYSTEM/EVENT/VM_PROVISION_REQUESTED_NEW", @user) - t1 = Time.now + # t1 = Time.now expect(ws).not_to be_nil expect(ws.root['ae_result']).to eq('ok') @@ -73,7 +73,7 @@ it "properly overrides class values with instance values, when they are present" do EvmSpecHelper.import_yaml_model(File.join(@model_data_dir, "state_machine"), @domain) - t0 = Time.now + # t0 = Time.now c1 = MiqAeClass.lookup_by_namespace_and_name("#{@domain}/Factory", "StateMachine") i1 = c1.ae_instances.detect { |i| i.name == "Provisioning" } @@ -81,7 +81,7 @@ i1.set_field_attribute(f1, "log_object", :on_exit) ws = MiqAeEngine.instantiate("/SYSTEM/EVENT/VM_PROVISION_REQUESTED_NEW", @user) - t1 = Time.now + # t1 = Time.now expect(ws).not_to be_nil # puts ws.to_xml diff --git a/spec/models/miq_ae_class_copy_spec.rb b/spec/models/miq_ae_class_copy_spec.rb index 5661a253c..56d808c87 100644 --- a/spec/models/miq_ae_class_copy_spec.rb +++ b/spec/models/miq_ae_class_copy_spec.rb @@ -103,10 +103,10 @@ miq_ae_class_copy = double(MiqAeClassCopy) miq_ae_class = double(MiqAeClass, :id => 1) new_ids = [miq_ae_class.id] * ids.length - expect(miq_ae_class_copy).to receive(:to_domain).with(domain, nil, false).exactly(ids.length).times { miq_ae_class } - expect(miq_ae_class).to receive(:fqname).with(no_args).exactly(ids.length).times { fqname } - expect(MiqAeClass).to receive(:find).with(an_instance_of(Integer)).exactly(ids.length).times { miq_ae_class } - expect(MiqAeClassCopy).to receive(:new).with(anything).exactly(ids.length).times { miq_ae_class_copy } + expect(miq_ae_class_copy).to receive(:to_domain).with(domain, nil, false).exactly(ids.length).times.and_return(miq_ae_class) + expect(miq_ae_class).to receive(:fqname).with(no_args).exactly(ids.length).times.and_return(fqname) + expect(MiqAeClass).to receive(:find).with(an_instance_of(Integer)).exactly(ids.length).times.and_return(miq_ae_class) + expect(MiqAeClassCopy).to receive(:new).with(anything).exactly(ids.length).times.and_return(miq_ae_class_copy) expect(MiqAeClassCopy.copy_multiple(ids, domain)).to match_array(new_ids) end end diff --git a/spec/models/miq_ae_datastore_spec.rb b/spec/models/miq_ae_datastore_spec.rb index 95134503a..9b96b9712 100644 --- a/spec/models/miq_ae_datastore_spec.rb +++ b/spec/models/miq_ae_datastore_spec.rb @@ -40,10 +40,10 @@ File.delete(@ver_fname) if File.exist?(@ver_fname) end - def setup_version_xml(v) - v = v.nil? ? "" : "version='#{v}'" + def setup_version_xml(version) + version = version.nil? ? "" : "version='#{version}'" xml = <<-XML - + diff --git a/spec/models/miq_ae_instance_copy_spec.rb b/spec/models/miq_ae_instance_copy_spec.rb index 4994f0c4f..ed0ea37d5 100644 --- a/spec/models/miq_ae_instance_copy_spec.rb +++ b/spec/models/miq_ae_instance_copy_spec.rb @@ -99,12 +99,12 @@ ids = [1, 2, 3] ins_copy = double(MiqAeInstanceCopy) ins = double(MiqAeInstance, :id => 1) - expect(ins_copy).to receive(:to_domain).with(domain, nil, false).exactly(ids.length).times { ins } + expect(ins_copy).to receive(:to_domain).with(domain, nil, false).exactly(ids.length).times.and_return(ins) new_ids = [ins.id] * ids.length - expect(ins).to receive(:fqname).with(no_args).exactly(ids.length).times { fqname } - expect(MiqAeInstance).to receive(:find).with(an_instance_of(Integer)).exactly(ids.length).times { ins } - expect(MiqAeInstanceCopy).to receive(:new).with(fqname, true).exactly(1).times { ins_copy } - expect(MiqAeInstanceCopy).to receive(:new).with(fqname, false).exactly(ids.length - 1).times { ins_copy } + expect(ins).to receive(:fqname).with(no_args).exactly(ids.length).times.and_return(fqname) + expect(MiqAeInstance).to receive(:find).with(an_instance_of(Integer)).exactly(ids.length).times.and_return(ins) + expect(MiqAeInstanceCopy).to receive(:new).with(fqname, true).exactly(1).times.and_return(ins_copy) + expect(MiqAeInstanceCopy).to receive(:new).with(fqname, false).exactly(ids.length - 1).times.and_return(ins_copy) expect(MiqAeInstanceCopy.copy_multiple(ids, domain)).to match_array(new_ids) end end diff --git a/spec/models/miq_ae_method_copy_spec.rb b/spec/models/miq_ae_method_copy_spec.rb index a634a259c..331180c2c 100644 --- a/spec/models/miq_ae_method_copy_spec.rb +++ b/spec/models/miq_ae_method_copy_spec.rb @@ -129,11 +129,11 @@ ids = [1, 2, 3] miq_ae_method_copy = double(MiqAeMethodCopy) miq_ae_method = double(MiqAeMethod, :id => 1) - expect(miq_ae_method_copy).to receive(:to_domain).with(domain, nil, false).exactly(ids.length).times { miq_ae_method } + expect(miq_ae_method_copy).to receive(:to_domain).with(domain, nil, false).exactly(ids.length).times.and_return(miq_ae_method) new_ids = [miq_ae_method.id] * ids.length - expect(miq_ae_method).to receive(:fqname).with(no_args).exactly(ids.length).times { fqname } - expect(MiqAeMethod).to receive(:find).with(an_instance_of(Integer)).exactly(ids.length).times { miq_ae_method } - expect(MiqAeMethodCopy).to receive(:new).with(fqname).exactly(ids.length).times { miq_ae_method_copy } + expect(miq_ae_method).to receive(:fqname).with(no_args).exactly(ids.length).times.and_return(fqname) + expect(MiqAeMethod).to receive(:find).with(an_instance_of(Integer)).exactly(ids.length).times.and_return(miq_ae_method) + expect(MiqAeMethodCopy).to receive(:new).with(fqname).exactly(ids.length).times.and_return(miq_ae_method_copy) expect(MiqAeMethodCopy.copy_multiple(ids, domain)).to match_array(new_ids) end end diff --git a/spec/models/miq_ae_yaml_import_export_spec.rb b/spec/models/miq_ae_yaml_import_export_spec.rb index 8514d2255..e7d9f48d2 100644 --- a/spec/models/miq_ae_yaml_import_export_spec.rb +++ b/spec/models/miq_ae_yaml_import_export_spec.rb @@ -861,7 +861,7 @@ def create_factory_data(domain_name, priority, source = MiqAeDomain::USER_SOURCE domain = FactoryBot.create(:miq_ae_domain_enabled, :name => domain_name, :source => source, :priority => priority) n1 = FactoryBot.create(:miq_ae_namespace, :name => "#{domain_name}_namespace_1", :parent_id => domain.id, :description => "test", :display_name => "test") n1_c1 = FactoryBot.create(:miq_ae_class, :name => "#{domain_name}_test_class_1", :namespace_id => n1.id) - n1_1 = FactoryBot.create(:miq_ae_namespace, :name => "#{domain_name}_namespace_1_1", :parent_id => n1.id) + n1_1 = FactoryBot.create(:miq_ae_namespace, :name => "#{domain_name}_namespace_1_1", :parent_id => n1.id) # rubocop:disable Naming/VariableNumber n1_1_c1 = FactoryBot.create(:miq_ae_class, :name => "#{domain_name}_test_class_4", :namespace_id => n1_1.id) n1_c1_i1 = FactoryBot.create(:miq_ae_instance, :name => "#{domain_name}_test_instance1", :class_id => n1_c1.id) n1_c1_m1 = FactoryBot.create(:miq_ae_method, @@ -905,9 +905,9 @@ def create_factory_data(domain_name, priority, source = MiqAeDomain::USER_SOURCE end def set_manageiq_values - @manageiq_domain = MiqAeDomain.find_by_name("manageiq") - @aen1 = MiqAeNamespace.find_by_name('manageiq_namespace_1') - @aen1_1 = MiqAeNamespace.find_by_name('manageiq_namespace_1_1') + @manageiq_domain = MiqAeDomain.find_by(:name => "manageiq") + @aen1 = MiqAeNamespace.find_by(:name => 'manageiq_namespace_1') + @aen1_1 = MiqAeNamespace.find_by(:name => 'manageiq_namespace_1_1') # rubocop:disable Naming/VariableNumber @aen1_aec1 = MiqAeClass.lookup_by_name('manageiq_test_class_1') @aen1_aec2 = MiqAeClass.lookup_by_name('manageiq_test_class_2') @class_dir = "#{@aen1_aec1.fqname}.class" @@ -917,9 +917,9 @@ def set_manageiq_values end def set_customer_values - @customer_domain = MiqAeDomain.find_by_name("customer") - @customer_aen1 = MiqAeNamespace.find_by_name('customer_namespace_1') - @customer_aen1_1 = MiqAeNamespace.find_by_name('customer_namespace_1_1') + @customer_domain = MiqAeDomain.find_by(:name => "customer") + @customer_aen1 = MiqAeNamespace.find_by(:name => 'customer_namespace_1') + @customer_aen1_1 = MiqAeNamespace.find_by(:name => 'customer_namespace_1_1') # rubocop:disable Naming/VariableNumber @customer_aen1_aec1 = MiqAeClass.lookup_by_name('customer_test_class_1') @customer_aen1_aec2 = MiqAeClass.lookup_by_name('customer_test_class_2') @class_name = @customer_aen1_aec1.name @@ -947,9 +947,9 @@ def check_counts(counts) validate_additional_columns end - def child_namespace_count(ns) + def child_namespace_count(namespace) count = 1 - ns.ae_namespaces.each { |n| count += child_namespace_count(n) } + namespace.ae_namespaces.each { |n| count += child_namespace_count(n) } count end @@ -985,7 +985,7 @@ def create_bogus_zip_file end def create_bogus_yaml_file - open(@yaml_file, 'w') do |fd| + File.open(@yaml_file, 'w') do |fd| a = {'A' => 1, 'B' => '2', 'C' => 3} fd.write(a.to_yaml) end diff --git a/spec/models/miq_ae_yaml_import_gitfs_spec.rb b/spec/models/miq_ae_yaml_import_gitfs_spec.rb index 46dccebae..335b54909 100644 --- a/spec/models/miq_ae_yaml_import_gitfs_spec.rb +++ b/spec/models/miq_ae_yaml_import_gitfs_spec.rb @@ -34,38 +34,38 @@ def add_files_to_repo @ae_db.send(:commit, "files_added").tap { |cid| @ae_db.send(:merge, cid) } end - def add_file(f) - @ae_db.add(f, YAML.dump(@default_hash.merge(:fname => f))) + def add_file(file_name) + @ae_db.add(file_name, YAML.dump(@default_hash.merge(:fname => file_name))) end - def add_namespace(ns) - add_file(namespace_file(ns)) - @classes.each { |klass| add_class(ns, klass) } + def add_namespace(namespace) + add_file(namespace_file(namespace)) + @classes.each { |klass| add_class(namespace, klass) } end - def namespace_file(ns) + def namespace_file(namespace) if @domain_dir - "#{@domain_dir}/#{ns}/#{MiqAeYamlImportExportMixin::NAMESPACE_YAML_FILENAME}" + "#{@domain_dir}/#{namespace}/#{MiqAeYamlImportExportMixin::NAMESPACE_YAML_FILENAME}" else - "#{ns}/#{MiqAeYamlImportExportMixin::NAMESPACE_YAML_FILENAME}" + "#{namespace}/#{MiqAeYamlImportExportMixin::NAMESPACE_YAML_FILENAME}" end end - def class_dir(ns, klass) + def class_dir(namespace, klass) if @domain_dir - "#{@domain_dir}/#{ns}/#{klass}#{MiqAeYamlImportExportMixin::CLASS_DIR_SUFFIX}" + "#{@domain_dir}/#{namespace}/#{klass}#{MiqAeYamlImportExportMixin::CLASS_DIR_SUFFIX}" else - "#{ns}/#{klass}#{MiqAeYamlImportExportMixin::CLASS_DIR_SUFFIX}" + "#{namespace}/#{klass}#{MiqAeYamlImportExportMixin::CLASS_DIR_SUFFIX}" end end - def class_file(ns, klass) - klass_dir = class_dir(ns, klass) + def class_file(namespace, klass) + klass_dir = class_dir(namespace, klass) "#{klass_dir}/#{MiqAeYamlImportExportMixin::CLASS_YAML_FILENAME}" end - def add_class(ns, klass) - klass_dir = class_dir(ns, klass) + def add_class(namespace, klass) + klass_dir = class_dir(namespace, klass) add_file("#{klass_dir}/#{MiqAeYamlImportExportMixin::CLASS_YAML_FILENAME}") @instances.each { |instance| add_instance(klass_dir, instance) } @methods.each { |method| add_method(klass_dir, method) } diff --git a/spec/service_models/miq_ae_service_generic_object_spec.rb b/spec/service_models/miq_ae_service_generic_object_spec.rb index 3eab5403d..d60a4a11d 100644 --- a/spec/service_models/miq_ae_service_generic_object_spec.rb +++ b/spec/service_models/miq_ae_service_generic_object_spec.rb @@ -46,7 +46,6 @@ } svc_obj = MiqAeMethodService::MiqAeServiceGenericObject.find(go.id) expect(MiqAeEngine).to receive('deliver').with(options).and_return(workspace) - svc_obj.send(method_name) end end diff --git a/spec/service_models/miq_ae_service_miq_provision_request_spec.rb b/spec/service_models/miq_ae_service_miq_provision_request_spec.rb index 27056dfe3..dc59f75ad 100644 --- a/spec/service_models/miq_ae_service_miq_provision_request_spec.rb +++ b/spec/service_models/miq_ae_service_miq_provision_request_spec.rb @@ -69,7 +69,7 @@ def invoke_ae @ae_method.update(:data => method) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(MiqAeMethodService::MiqAeServiceMiqTemplate) - [:id, :name, :location].each { |method| expect(ae_object.send(method)).to eq(@vm_template.send(method)) } + [:id, :name, :location].each { |method_name| expect(ae_object.send(method_name)).to eq(@vm_template.send(method_name)) } end it "#request_type" do diff --git a/spec/service_models/miq_ae_service_miq_provision_spec.rb b/spec/service_models/miq_ae_service_miq_provision_spec.rb index dcd3aca25..16925b19a 100644 --- a/spec/service_models/miq_ae_service_miq_provision_spec.rb +++ b/spec/service_models/miq_ae_service_miq_provision_spec.rb @@ -39,7 +39,7 @@ def invoke_ae @ae_method.update(:data => method) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(MiqAeMethodService::MiqAeServiceMiqProvisionRequest) - [:id, :provision_type, :state, :status, :src_vm_id, :userid].each { |method| expect(ae_object.send(method)).to eq(miq_provision_request.send(method)) } + [:id, :provision_type, :state, :status, :src_vm_id, :userid].each { |method_name| expect(ae_object.send(method_name)).to eq(miq_provision_request.send(method_name)) } end it "#vm" do @@ -54,7 +54,7 @@ def invoke_ae ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(MiqAeMethodService::MiqAeServiceVm) - [:id, :name, :location].each { |method| expect(ae_object.send(method)).to eq(vm.send(method)) } + [:id, :name, :location].each { |method_name| expect(ae_object.send(method_name)).to eq(vm.send(method_name)) } end it "#vm_template" do @@ -62,7 +62,7 @@ def invoke_ae @ae_method.update(:data => method) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(MiqAeMethodService::MiqAeServiceMiqTemplate) - [:id, :name, :location].each { |method| expect(ae_object.send(method)).to eq(@vm_template.send(method)) } + [:id, :name, :location].each { |method_name| expect(ae_object.send(method_name)).to eq(@vm_template.send(method_name)) } end it "#execute" do @@ -154,7 +154,7 @@ def invoke_ae prov.eligible_iso_images.each {|iso| prov.set_iso_image(iso)} AUTOMATE_SCRIPT @ae_method.update(:data => method) - result = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) expect(@miq_provision.reload.options[:iso_image_id]).to eq([@iso_image.id, @iso_image.name]) end end @@ -199,7 +199,7 @@ def invoke_ae prov.eligible_customization_templates.each {|ct| prov.set_customization_template(ct)} AUTOMATE_SCRIPT @ae_method.update(:data => method) - result = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) expect(@miq_provision.reload.options[:customization_template_id]).to eq([@ct.id, @ct.name]) expect(@miq_provision.reload.options[:customization_template_script]).to eq(@ct.script) end @@ -226,7 +226,7 @@ def invoke_ae prov.eligible_resource_pools.each {|rsc| prov.set_resource_pool(rsc)} AUTOMATE_SCRIPT @ae_method.update(:data => method) - result = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) expect(@miq_provision.reload.options[:placement_rp_name]).to eq([@rsc.id, @rsc.name]) end end diff --git a/spec/service_models/miq_ae_service_miq_request_spec.rb b/spec/service_models/miq_ae_service_miq_request_spec.rb index 417332fca..c64a9afda 100644 --- a/spec/service_models/miq_ae_service_miq_request_spec.rb +++ b/spec/service_models/miq_ae_service_miq_request_spec.rb @@ -107,7 +107,7 @@ def invoke_ae ae_resource = invoke_ae.root(@ae_result_key) ae_class = "MiqAeMethodService::MiqAeService#{resource.class.name.gsub(/::/, '_')}".constantize expect(ae_resource).to be_kind_of(ae_class) - [:userid, :src_vm_id].each { |method| expect(ae_resource.send(method)).to eq(resource.send(method)) } + [:userid, :src_vm_id].each { |method_name| expect(ae_resource.send(method_name)).to eq(resource.send(method_name)) } end it "#reason" do @@ -138,7 +138,7 @@ def invoke_ae # wilma_approval.update(:state => 'denied', :reason => wilma_reason) reasons = invoke_ae.root(@ae_result_key) # Order of reasons is indeterminate - reasons.split('; ').each { |reason| expect([betty_reason, wilma_reason].include?(reason)).to be_truthy } + reasons.split('; ').each { |rsn| expect([betty_reason, wilma_reason].include?(rsn)).to be_truthy } end it "#options" do diff --git a/spec/service_models/miq_ae_service_service_spec.rb b/spec/service_models/miq_ae_service_service_spec.rb index 5ff6eac39..f325e7b63 100644 --- a/spec/service_models/miq_ae_service_service_spec.rb +++ b/spec/service_models/miq_ae_service_service_spec.rb @@ -25,7 +25,7 @@ def invoke_ae expect(Service.count).to eq(1) method = "$evm.root['#{@ae_result_key}'] = $evm.root['service'].remove_from_vmdb" @ae_method.update(:data => method) - ae_object = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) expect(Service.count).to eq(0) end @@ -33,7 +33,7 @@ def invoke_ae expect(@service.name).to eq('test_service') method = "$evm.root['#{@ae_result_key}'] = $evm.root['service'].name = 'new_test_service' " @ae_method.update(:data => method) - ae_object = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) @service.reload expect(@service.name).to eq('new_test_service') end @@ -46,7 +46,7 @@ def invoke_ae expect(@service.description).to eq('test_description') method = "$evm.root['#{@ae_result_key}'] = $evm.root['service'].description = 'new_test_description' " @ae_method.update(:data => method) - ae_object = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) @service.reload expect(@service.description).to eq('new_test_description') end diff --git a/spec/service_models/miq_ae_service_vm_spec.rb b/spec/service_models/miq_ae_service_vm_spec.rb index aaafa2cfa..4fbfb6b6d 100644 --- a/spec/service_models/miq_ae_service_vm_spec.rb +++ b/spec/service_models/miq_ae_service_vm_spec.rb @@ -31,7 +31,7 @@ def invoke_ae key1 = 'key1' value1 = 'value1' - c1 = FactoryBot.create(:ems_custom_attribute, :resource => @vm, :name => key1, :value => value1) + FactoryBot.create(:ems_custom_attribute, :resource => @vm, :name => key1, :value => value1) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(Array) expect(ae_object.length).to eq(1) @@ -39,7 +39,7 @@ def invoke_ae key2 = 'key2' value2 = 'value2' - c1 = FactoryBot.create(:ems_custom_attribute, :resource => @vm, :name => key2, :value => value2) + FactoryBot.create(:ems_custom_attribute, :resource => @vm, :name => key2, :value => value2) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_kind_of(Array) expect(ae_object.length).to eq(2) @@ -54,7 +54,7 @@ def invoke_ae ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to be_nil - c1 = FactoryBot.create(:ems_custom_attribute, :resource => @vm, :name => key, :value => value) + FactoryBot.create(:ems_custom_attribute, :resource => @vm, :name => key, :value => value) ae_object = invoke_ae.root(@ae_result_key) expect(ae_object).to eq(value) end @@ -63,7 +63,7 @@ def invoke_ae expect(VmOrTemplate.count).to eq(1) method = "$evm.root['#{@ae_result_key}'] = $evm.root['vm'].remove_from_vmdb" @ae_method.update(:data => method) - ae_object = invoke_ae.root(@ae_result_key) + invoke_ae.root(@ae_result_key) expect(VmOrTemplate.count).to eq(0) end