-
Notifications
You must be signed in to change notification settings - Fork 897
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
Quota - Calculate quota values for active provisions. #15466
Conversation
@mkanoor Please review. |
@miq-bot add_label bug, fine/yes, assign @gmcculloug |
@tinaafitz Cannot apply the following label because they are not recognized: assign @gmcculloug |
def number_of_vms(request) | ||
num_vms_for_request = request.get_option(:number_of_vms).to_i | ||
if options[:nil_vm_id_only] == true && request.miq_request_tasks.length == num_vms_for_request | ||
no_vm = request.miq_request_tasks.find_all { |p| p.destination_id.nil? && p.state != 'finished' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tinaafitz Is this a count or an array of objects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkanoor It's an array of objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe request.miq_request_tasks
is an ActiveRecord Collection, correct?
If so, prefer where
over find_all
.
request.miq_request_tasks.where(:destination_id => nil).where.not(:state => 'finished')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait... this method returns an integer or an array. Based on the name, I would expect it to return an Integer
, so you need a .count
on the suggestion above.
c5a8e4a
to
5ddc971
Compare
prov_requests = quota_find_active_prov_request(options).select do |p| | ||
prov_request_group == p.miq_request.requester.current_group | ||
end | ||
prov_requests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prov_requests
here and on line 272 are not needed, the select is the last thing run in method, so the method will return that by default.
end | ||
prov_request_group = miq_request.requester.current_group | ||
prov_requests = quota_find_active_prov_request(options).select do |p| | ||
prov_request_group == p.miq_request.requester.current_group |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need the .miq_request
? Isn't p
already an instance of MiqRequest
?
@@ -250,40 +261,88 @@ def quota_find_active_prov_request_by_owner(options) | |||
quota_find_active_prov_request(options).select { |p| email.casecmp(p.get_option(:owner_email).to_s.strip) == 0 } | |||
end | |||
|
|||
def quota_find_active_prov_request_by_user(options) | |||
quota_find_active_prov_request(options).select do |p| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why accept and forward options
if they're ultimately ignored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne The method needs to have the options argument because it's called by the quota_provision_stats method which calls all of the provision methods with options:
def quota_provision_stats(prov_method, options)
....
send(prov_method, options).each do |pr|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not do all of this in the database?
quota_find_active_prov_request(options).where(:userid => miq_request.requester.userid)
And tack on a .all
if you need to.
def number_of_vms(request) | ||
num_vms_for_request = request.get_option(:number_of_vms).to_i | ||
if options[:nil_vm_id_only] == true && request.miq_request_tasks.length == num_vms_for_request | ||
no_vm = request.miq_request_tasks.find_all { |p| p.destination_id.nil? && p.state != 'finished' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe request.miq_request_tasks
is an ActiveRecord Collection, correct?
If so, prefer where
over find_all
.
request.miq_request_tasks.where(:destination_id => nil).where.not(:state => 'finished')
def number_of_vms(request) | ||
num_vms_for_request = request.get_option(:number_of_vms).to_i | ||
if options[:nil_vm_id_only] == true && request.miq_request_tasks.length == num_vms_for_request | ||
no_vm = request.miq_request_tasks.find_all { |p| p.destination_id.nil? && p.state != 'finished' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait... this method returns an integer or an array. Based on the name, I would expect it to return an Integer
, so you need a .count
on the suggestion above.
|
||
def memory(prov, cloud, vendor, flavor_obj = nil) | ||
if cloud | ||
return nil unless flavor_obj |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flavor_obj.try(:memory)
would one-line and remove the return
from this side of the if
flavor_obj.memory | ||
else | ||
memory = prov.kind_of?(MiqRequest) ? prov.get_option(:vm_memory).to_i : prov.miq_request.get_option(:vm_memory).to_i | ||
memory.megabytes if %w(amazon openstack google).exclude?(vendor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil
if it's not one of those 3 vendors?
If that's the intent, then we should return before line 412 to avoid doing extra work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne We return memory if it's not one of those 3 vendors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant it returns nil if it is one of those vendors.
It would read easier and be more efficient as:
return if %w(amazon openstack google).include?(vendor)
request = prov.kind_of?(MiqRequest) ? prov : prov.miq_request
request.get_option(:vm_memory).to_i.megabytes
|
||
stats = @pr.check_quota(:requests_by_owner) | ||
expect(stats).to be_kind_of(Hash) | ||
expect(stats[:class_name]).to eq("MiqProvisionRequest") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce the number of expectations on the same object, you can:
expect(stats).to have_attributes(
:class_name => "MiqProvisionRequest",
:count => 2,
.....
)
end | ||
|
||
def create_request(user, vm_template, prov_options) | ||
FactoryGirl.create(:miq_provision_request, :requester => user, :description => "request", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alignment is awkward
end | ||
|
||
def create_request(user, template, prov_options = {}) | ||
FactoryGirl.create(:service_template_provision_request, :requester => user, :description => "request", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same awkward alignment
@@ -130,30 +130,18 @@ | |||
expect(stats.fetch_path(:active, :class_name)).to eq("MiqProvision") | |||
end | |||
|
|||
it "should return stats from quota methods" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne This test wasn't necessary.
This pull request is not mergeable. Please rebase and repush. |
792d42a
to
ec130e7
Compare
@@ -250,40 +261,88 @@ def quota_find_active_prov_request_by_owner(options) | |||
quota_find_active_prov_request(options).select { |p| email.casecmp(p.get_option(:owner_email).to_s.strip) == 0 } | |||
end | |||
|
|||
def quota_find_active_prov_request_by_user(options) | |||
quota_find_active_prov_request(options).select do |p| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not do all of this in the database?
quota_find_active_prov_request(options).where(:userid => miq_request.requester.userid)
And tack on a .all
if you need to.
|
||
def quota_find_active_prov_request_by_tenant(options) | ||
quota_find_active_prov_request(options).select do |p| | ||
miq_request.tenant == p.tenant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same where comment here.
end | ||
|
||
def flavor(request) | ||
Flavor.find_by(:id => request.get_option(:instance_type)) if cloud?(request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify to using find
since you're searching by id
end | ||
|
||
def memory(prov, cloud, vendor, flavor_obj = nil) | ||
if cloud |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use guard clauses to prevent doing extra work.
return flavor_obj.try(:memory) if cloud
return if %w(amazon openstack google).includes?(vendor)
request = prov.kind_of?(MiqRequest) ? prov : prov.miq_request
request.get_option(:vm_memory).to_i.megabytes
let(:request) { create_test_task(@vmware_user1, @vmware_template) } | ||
let(:quota_method) { :active_provisions } | ||
let(:counts_hash) do | ||
{ :count => 12, :memory => 8_589_938_688, :cpu => 32, :storage => 44.gigabytes } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't have spaces inside the hash braces
context "active_provisions_by_user," do | ||
let(:quota_method) { :active_provisions_by_user } | ||
let(:counts_hash) do | ||
{ :count => 2, :memory => 2048, :cpu => 8, :storage => 20.gigabytes } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't have spaces inside the hash braces
let(:load_queue) { queue(vmware_tasks) } | ||
let(:request) { create_test_task(@vmware_user1, @vmware_template) } | ||
let(:counts_hash) do | ||
{ :count => 6, :memory => 6.gigabytes, :cpu => 16, :storage => 3.gigabytes } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't have spaces inside the hash braces
context "active_provisions_by_user," do | ||
let(:quota_method) { :active_provisions_by_user } | ||
let(:counts_hash) do | ||
{ :count => 3, :memory => 3.gigabytes, :cpu => 8, :storage => 1_610_612_736 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't have spaces inside the hash braces
let(:load_queue) { queue(google_tasks) } | ||
let(:request) { create_test_task(@google_user1, @google_template) } | ||
let(:counts_hash) do | ||
{ :count => 4, :memory => 4096, :cpu => 16, :storage => 40.gigabytes } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't have spaces inside the hash braces
context "active_provisions_by_user," do | ||
let(:quota_method) { :active_provisions_by_user } | ||
let(:counts_hash) do | ||
{ :count => 2, :memory => 2048, :cpu => 8, :storage => 20.gigabytes } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't have spaces inside the hash braces
a731f03
to
55111d5
Compare
end | ||
|
||
def number_of_cpus(prov, cloud, flavor_obj) | ||
if cloud |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer a guard clause like return flavor_obj.try(:cpus) if cloud
rather than if
/ else
.
return flavor_obj.try(:memory) if cloud | ||
request = prov.kind_of?(MiqRequest) ? prov : prov.miq_request | ||
memory = request.get_option(:vm_memory).to_i | ||
memory.megabytes if %w(amazon openstack google).exclude?(vendor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We seem to keep losing my comment that we should return if %w(amazon openstack google).exclude?(vendor)
early.
@mkanoor Please review. |
:state => 'dequeue' | ||
).pluck(:instance_id) | ||
|
||
prov_ids = [] | ||
MiqQueue | ||
.where(:method_name => 'deliver', :state => %w(ready dequeue), :class_name => 'MiqAeEngine') | ||
.where("tracking_label like ?", '%miq_provision_%') | ||
.where("task_id like ?", '%_provision_%') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ceafd63
to
a7736f9
Compare
a7736f9
to
873340b
Compare
prov_req_owner = p.get_owner | ||
prov_req_owner && group.casecmp(prov_req_owner.ldap_group) == 0 | ||
end | ||
prov_request_group = miq_request.requester.current_group |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Knowing that current_group
change change in the db for the user after the request is created should we base this on miq_request.options[:requester_group]
? (Note: This is the group name, not id)
end | ||
prov_request_group = miq_request.requester.current_group | ||
quota_find_active_prov_request(options).select do |r| | ||
prov_request_group == r.requester.current_group |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here for the current_group.
end | ||
|
||
def service_request?(request) | ||
request.type == "ServiceTemplateProvisionRequest" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest request.kind_of?(ServiceTemplateProvisionRequest)
.
This will future-proof the method if the ServiceTemplateProvisionRequest ever gets sub-classed. Because the name isn't already long enough. 😄
873340b
to
eb8da72
Compare
@tinaafitz The tests are failing because of the |
eb8da72
to
ad37766
Compare
Checked commits tinaafitz/manageiq@fa19836~...ad37766 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
Quota - Calculate quota values for active provisions. (cherry picked from commit 6d5cb11) https://bugzilla.redhat.com/show_bug.cgi?id=1489507
Fine backport details:
|
@miq-bot add_label euwe/yes |
Backported to Euwe via #16141 |
Quota - Calculate quota values for active provisions. (cherry picked from commit 6d5cb11) https://bugzilla.redhat.com/show_bug.cgi?id=1489507
Modified provision quota mixin to calculate active or inflight provisions.
The changes are meant only as a temporary measure for quota calculations.
Updated quota mixin to reflect changes in method and object names.
Added methods for tenant and group active provisions and for quota item calculations.
Added tests.
https://bugzilla.redhat.com/show_bug.cgi?id=1456819