Skip to content

Commit

Permalink
Merge pull request #374 from astrozzc/rubocop_correction_5
Browse files Browse the repository at this point in the history
Rubocop correction 5
  • Loading branch information
gmcculloug authored Feb 24, 2020
2 parents b8cbf9a + 1be65de commit 700bcc5
Show file tree
Hide file tree
Showing 41 changed files with 410 additions and 374 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
30 changes: 15 additions & 15 deletions app/models/miq_ae_class_copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
30 changes: 15 additions & 15 deletions app/models/miq_ae_datastore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})")

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions app/models/miq_ae_datastore/xml_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions app/models/miq_ae_datastore/xml_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions app/models/miq_ae_domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions app/models/miq_ae_instance_copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
20 changes: 10 additions & 10 deletions app/models/miq_ae_method_copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/models/miq_ae_yaml_import_consolidated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 700bcc5

Please sign in to comment.