Skip to content

Commit

Permalink
Move quota calcuation methods into Template class
Browse files Browse the repository at this point in the history
Move methods calculating the number of cpus, memory and storage (TODO)
into the base MiqTemplate class.

The is no common logic between cloud and infrastructure provider types,
so the base MiqRequest just raises NotImplementedError.

Most existing providers can use the common overrides provided in the
CloudManager::Template and InfraManager::Template subclasses, but custom
logic can be now be defined in each provider type subclass.
  • Loading branch information
jaywcarman committed Oct 18, 2023
1 parent d2bff93 commit 9167dd5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
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)
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
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(*args)
raise NotImplementedError, _("must be implemented in a subclass")
end

def number_of_cpus_for_request(*args)
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.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

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.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
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

0 comments on commit 9167dd5

Please sign in to comment.