Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Move provisioning quota calculation methods into MiqTemplate class (pluggable) #22680

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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,18 @@ def self.display_name(number = 1)
n_('Image', 'Images', number)
end

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

def number_of_cpus_for_request(request, flavor_id = nil)
flavor_id ||= request.get_option(:instance_type)
flavor_obj = Flavor.find(flavor_id)
flavor_obj.try(:cpus)
end

private

def raise_created_event
Expand Down
10 changes: 10 additions & 0 deletions app/models/manageiq/providers/infra_manager/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ def self.display_name(number = 1)
n_('Template', 'Templates', number)
end

def memory_for_request(request, _flavor_id = nil)
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, _flavor_id = nil)
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
end

private

def raise_created_event
Expand Down
8 changes: 8 additions & 0 deletions app/models/miq_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def display_deprecated
end
end

def memory_for_request(request, flavor_id = nil)
raise NotImplementedError, _("must be implemented in a subclass")
end

def number_of_cpus_for_request(request, flavor_id = nil)
raise NotImplementedError, _("must be implemented in a subclass")
end

private_class_method def self.refresh_association
:miq_templates
end
Expand Down
26 changes: 4 additions & 22 deletions app/models/mixins/miq_provision_quota_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,17 @@ 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[:cpu] += number_of_cpus(pr, cloud?(pr), flavor_obj) * num_vms_for_request
result[:memory] += pr.vm_template.memory_for_request(pr) * num_vms_for_request
result[:cpu] += pr.vm_template.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

pr.miq_request_tasks.each do |p|
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[:cpu_by_host_id][host_id] += number_of_cpus(p, cloud?(pr), flavor_obj)
active[:memory_by_host_id][host_id] += p.vm_template.memory_for_request(p)
active[:cpu_by_host_id][host_id] += p.vm_template.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
active[:ids] << p.id
Expand Down Expand Up @@ -364,15 +364,6 @@ def flavor(request)
Flavor.find(request.get_option(:instance_type)) if cloud?(request)
end

def number_of_cpus(prov, cloud, flavor_obj)
num_cpus = flavor_obj.try(:cpus) if cloud
return num_cpus if num_cpus.present?

request = prov.kind_of?(MiqRequest) ? prov : prov.miq_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
end

def storage(prov, cloud, vendor, flavor_obj = nil)
if cloud
if vendor == 'google'
Expand All @@ -384,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
Loading