Skip to content

Commit

Permalink
Move 'memory' into MiqTemplate classes
Browse files Browse the repository at this point in the history
Move method calculating the memory for a given request into MiqTemplate
base class, and then override it in the
ManageIQ::Providers::CloudManager::Template subclass.
  • Loading branch information
jaywcarman committed Oct 2, 2023
1 parent e0242de commit 3b1acf3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 5 additions & 0 deletions app/models/manageiq/providers/cloud_manager/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ def self.display_name(number = 1)
n_('Image', 'Images', number)
end

def memory_for_request(request)
flavor_obj = Flavor.find(request.get_option(:instance_type))
flavor_obj.try(:memory)
end

def number_of_cpus_for_request(request)
flavor_obj = Flavor.find(request.get_option(:instance_type))
flavor_obj.try(:cpus)
Expand Down
5 changes: 5 additions & 0 deletions app/models/miq_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def display_deprecated
end
end

def memory_for_request(request)
memory = request.get_option(:vm_memory).to_i
%w[amazon openstack google].include?(vendor) ? memory : memory.megabytes
end

def number_of_cpus_for_request(request)
num_cpus = request.get_option(:number_of_sockets).to_i * request.get_option(:cores_per_socket).to_i
num_cpus.zero? ? request.get_option(:number_of_cpus).to_i : num_cpus
Expand Down
13 changes: 2 additions & 11 deletions app/models/mixins/miq_provision_quota_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def vm_quota_values(pr, result)
return if num_vms_for_request.zero?
flavor_obj = flavor(pr)
result[:count] += num_vms_for_request
result[:memory] += memory(pr, cloud?(pr), vendor(pr), flavor_obj) * num_vms_for_request
result[:memory] += pr.source.memory_for_request(pr) * num_vms_for_request
result[:cpu] += pr.source.number_of_cpus_for_request(pr) * num_vms_for_request
result[:storage] += storage(pr, cloud?(pr), vendor(pr), flavor_obj) * num_vms_for_request
result[:ids] << pr.id
Expand All @@ -302,7 +302,7 @@ def vm_quota_values(pr, result)
next unless p.state == 'Active'
host_id, storage_id = p.get_option(:dest_host).to_i, p.get_option(:dest_storage).to_i
active = result[:active]
active[:memory_by_host_id][host_id] += memory(p, cloud?(pr), vendor(pr), flavor_obj)
active[:memory_by_host_id][host_id] += p.source.memory(p)
active[:cpu_by_host_id][host_id] += p.source.number_of_cpus_for_request(p)
active[:storage_by_id][storage_id] += storage(p, cloud?(pr), vendor(pr), flavor_obj)
active[:vms_by_storage_id][storage_id] << p.id
Expand Down Expand Up @@ -375,13 +375,4 @@ def storage(prov, cloud, vendor, flavor_obj = nil)
prov.kind_of?(MiqRequest) ? prov.vm_template.provisioned_storage : prov.miq_request.vm_template.provisioned_storage
end
end

def memory(prov, cloud, vendor, flavor_obj = nil)
memory = flavor_obj.try(:memory) if cloud
return memory if memory.present?

request = prov.kind_of?(MiqRequest) ? prov : prov.miq_request
memory = request.get_option(:vm_memory).to_i
%w(amazon openstack google).include?(vendor) ? memory : memory.megabytes
end
end

0 comments on commit 3b1acf3

Please sign in to comment.