Skip to content

Commit

Permalink
Merge pull request #16038 from bdunne/miq_request_workflow_deduplication
Browse files Browse the repository at this point in the history
Build two new methods to reduce duplicated logic
  • Loading branch information
chrisarcand authored Sep 28, 2017
2 parents c7814fb + 7e03fc2 commit 1c1c27d
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions app/models/miq_request_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1122,23 +1122,19 @@ def ci_to_datacenter(src, ci, ci_type)
def respool_to_cluster(src)
return nil unless ems_has_clusters?
sources = src[:respool].nil? ? find_all_ems_of_type(ResourcePool) : [src[:respool]]
targets = sources.collect { |rp| find_cluster_above_ci(rp) }.compact
targets.each_with_object({}) { |c, r| r[c.id] = c.name }
build_id_to_name_hash(sources.collect { |rp| find_cluster_above_ci(rp) }.compact)
end

def host_to_cluster(src)
return nil unless ems_has_clusters?
sources = src[:host].nil? ? allowed_hosts_obj : [src[:host]]
targets = sources.collect { |h| find_cluster_above_ci(h) }.compact
targets.each_with_object({}) { |c, r| r[c.id] = c.name }
build_id_to_name_hash(sources.collect { |h| find_cluster_above_ci(h) }.compact)
end

def folder_to_cluster(src)
return nil unless ems_has_clusters?
source = find_all_ems_of_type(EmsCluster)
# If a folder is selected, reduce the cluster list to only clusters in the same data center as the folder
source = source.reject { |c| find_datacenter_for_ci(c).id != src[:datacenter].id } unless src[:datacenter].nil?
source.each_with_object({}) { |c, r| r[c.id] = c.name }
build_id_to_name_hash(filter_to_objects_in_same_datacenter(source, src))
end

def cluster_to_respool(src)
Expand Down Expand Up @@ -1170,19 +1166,17 @@ def host_to_respool(src)
def cluster_to_host(src)
return nil unless ems_has_clusters?
hosts = src[:cluster].nil? ? find_all_ems_of_type(Host) : find_hosts_under_ci(src[:cluster])
hosts.each_with_object({}) { |h, r| r[h.id] = h.name }
build_id_to_name_hash(hosts)
end

def respool_to_host(src)
hosts = src[:respool].nil? ? find_all_ems_of_type(Host) : find_hosts_for_respool(src[:respool])
hosts.each_with_object({}) { |h, r| r[h.id] = h.name }
build_id_to_name_hash(hosts)
end

def folder_to_host(src)
source = find_all_ems_of_type(Host)
# If a folder is selected, reduce the host list to only hosts in the same datacenter as the folder
source = source.reject { |h| find_datacenter_for_ci(h).id != src[:datacenter].id } unless src[:datacenter].nil?
source.each_with_object({}) { |h, r| r[h.id] = h.name }
build_id_to_name_hash(filter_to_objects_in_same_datacenter(source, src))
end

def host_to_folder(src)
Expand Down Expand Up @@ -1461,9 +1455,18 @@ def apply_result(result, data_type)
[result.first, result.last] unless result.nil?
end

def build_id_to_name_hash(array)
array.each_with_object({}) { |i, h| h[i.id] = i.name }
end

def default_ci_to_hash_struct(ci)
attributes = []
attributes << :name if ci.respond_to?(:name)
build_ci_hash_struct(ci, attributes)
end

def filter_to_objects_in_same_datacenter(array, source)
# If a folder is selected, reduce the host/cluster list to only hosts/clusters in the same datacenter as the folder
source[:datacenter] ? array.reject { |i| find_datacenter_for_ci(i).id != source[:datacenter].id } : array
end
end

0 comments on commit 1c1c27d

Please sign in to comment.