From 5d4c745730b90ef9891cc76770f08a475bb2dab7 Mon Sep 17 00:00:00 2001 From: william fitzgerald Date: Tue, 10 Oct 2017 09:29:54 -0400 Subject: [PATCH] Added active provisions to quota count. Modified used method to count active provisions for quota. Added log messages to method. https://bugzilla.redhat.com/show_bug.cgi?id=1456819 Log messages display counts and type of quota. Sample log messages below: Quota Used: {:cpu=>373, :memory=>922780434432, :vms=>141, :storage=>9111136632832, :provisioned_storage=>10033917067264} Quota active_provisions_by_tenant: {:cpu=>1, :memory=>1073741824, :vms=>1, :storage=>44023414784, :provisioned_storage=>0} Quota Totals: {:cpu=>374, :memory=>923854176256, :vms=>142, :storage=>9155160047616, :provisioned_storage=>10033917067264} Quota Used: {:cpu=>1, :memory=>1073741824, :vms=>1, :storage=>8589934592, :provisioned_storage=>9663676416} Quota active_provisions_by_group: {:cpu=>1, :memory=>1073741824, :vms=>1, :storage=>44023414784, :provisioned_storage=>0} Quota Totals: {:cpu=>2, :memory=>2147483648, :vms=>2, :storage=>52613349376, :provisioned_storage=>9663676416} Quota Used: {:cpu=>1, :memory=>1073741824, :vms=>1, :storage=>8589934592, :provisioned_storage=>9663676416} Quota active_provisions_by_owner: {:cpu=>0, :memory=>0, :vms=>0, :storage=>0, :provisioned_storage=>0} Quota Totals: {:cpu=>1, :memory=>1073741824, :vms=>1, :storage=>8589934592, :provisioned_storage=>9663676416} --- .../QuotaMethods.class/__methods__/used.rb | 29 ++++++++++++++++- .../method_validation/calculate_used_spec.rb | 26 --------------- .../__methods__/used_spec.rb | 32 +++++++++++++++---- 3 files changed, 53 insertions(+), 34 deletions(-) delete mode 100644 spec/automation/unit/method_validation/calculate_used_spec.rb diff --git a/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used.rb b/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used.rb index d6fd55301..98be05667 100644 --- a/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used.rb +++ b/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # # Description: calculate entity used quota values # @@ -19,7 +20,13 @@ def main private def used(quota_source) - @handle.root['quota_used'] = consumption(quota_source) + quota_used = consumption(quota_source) + @handle.log("info", "Quota Used: #{quota_used.inspect}") + + quota_active = active_provision_counts + @handle.log("info", "Quota #{active_method_name}: #{quota_active.inspect}") + + merge_counts(quota_used, quota_active) end def quota_source @@ -36,6 +43,26 @@ def consumption(source) :provisioned_storage => source.provisioned_storage } end + + def active_method_name + quota_source = @handle.root['quota_source_type'].downcase + source = quota_source == 'user' ? 'owner' : quota_source + "active_provisions_by_#{source}".to_sym + end + + def active_provision_counts + active_provisions = @handle.root['miq_request'].check_quota(active_method_name) + {:cpu => active_provisions[:cpu], + :memory => active_provisions[:memory], + :vms => active_provisions[:count], + :storage => active_provisions[:storage], + :provisioned_storage => 0} + end + + def merge_counts(quota_used, quota_active) + @handle.root['quota_used'] = quota_used.merge(quota_active) { |_key, val1, val2| val1 + val2 } + @handle.log("info", "Quota Totals: #{@handle.root['quota_used'].inspect}") + end end end end diff --git a/spec/automation/unit/method_validation/calculate_used_spec.rb b/spec/automation/unit/method_validation/calculate_used_spec.rb deleted file mode 100644 index 48e9d55f4..000000000 --- a/spec/automation/unit/method_validation/calculate_used_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -describe "Quota Validation" do - include Spec::Support::QuotaHelper - - def run_automate_method(prov_req) - attrs = [] - attrs << "MiqProvisionRequest::miq_provision_request=#{@miq_provision_request.id}&" \ - "MiqRequest::miq_request=#{@miq_provision_request.id}&Tenant::quota_source=#{@tenant.id}" if prov_req - MiqAeEngine.instantiate("/ManageIQ/system/request/Call_Instance?namespace=System/CommonMethods&" \ - "class=QuotaMethods&instance=used&#{attrs.join('&')}", @user) - end - - before do - setup_model - end - - it "calculate_used" do - ws = run_automate_method(@miq_provision_request) - root = ws.root - expect(root['quota_source']).to be_kind_of(MiqAeMethodService::MiqAeServiceTenant) - expect(root['quota_used'][:storage]).to eq(1_000_000) - expect(root['quota_used'][:cpu]).to eq(0) - expect(root['quota_used'][:vms]).to eq(1) - expect(root['quota_used'][:provisioned_storage]).to eq(1_074_741_824) - expect(root['quota_used'][:memory]).to eq(1_073_741_824) - end -end diff --git a/spec/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used_spec.rb b/spec/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used_spec.rb index d1890a1fc..8f7338941 100644 --- a/spec/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used_spec.rb +++ b/spec/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used_spec.rb @@ -6,15 +6,25 @@ let!(:model) { setup_model } let(:root_hash) do { - 'miq_provision_request' => @miq_provision_request, - 'miq_request' => @miq_provision_request, + 'miq_provision_request' => svc_miq_request, + 'miq_request' => svc_miq_request, 'quota_source' => quota_source, 'quota_source_type' => quota_source_type } end + let(:svc_miq_request) { MiqAeMethodService::MiqAeServiceMiqRequest.find(@miq_provision_request.id) } + let(:counts_hash) do - {:storage => 1_000_000, :cpu => 0, :vms => 4, :memory => 1_073_741_824} + {:storage => 1_000_000, :cpu => 0, :vms => 1, :memory => 1_073_741_824} + end + + let(:active_counts_hash) do + {:storage => 2_000_000, :cpu => 8, :count => 9, :memory => 6_000_000_000} + end + + let(:result_counts_hash) do + {:storage => 3_000_000, :cpu => 8, :vms => 10, :memory => 7_073_741_824} end let(:root_object) do @@ -31,26 +41,33 @@ shared_examples_for "used" do it "check" do + expect(svc_miq_request).to receive(:check_quota).with(active_method).and_return(active_counts_hash) described_class.new(ae_service).main - expect(ae_service.root['quota_used']).to include(counts_hash) + expect(ae_service.root['quota_used']).to include(result_counts_hash) end end context "returns ok for tenant counts" do - let(:quota_source) { @tenant } + let(:quota_source) { MiqAeMethodService::MiqAeServiceTenant.find(@tenant.id) } let(:quota_source_type) { 'tenant' } + let(:active_method) { 'active_provisions_by_tenant'.to_sym } + it_behaves_like "used" end context "returns ok for user counts" do - let(:quota_source) { @tenant } + let(:quota_source) { MiqAeMethodService::MiqAeServiceUser.find(@user.id) } let(:quota_source_type) { 'user' } + let(:active_method) { 'active_provisions_by_owner'.to_sym } + it_behaves_like "used" end context "returns ok for group counts" do - let(:quota_source) { @tenant } + let(:quota_source) { MiqAeMethodService::MiqAeServiceMiqGroup.find(@miq_group.id) } let(:quota_source_type) { 'group' } + let(:active_method) { 'active_provisions_by_group'.to_sym } + it_behaves_like "used" end @@ -58,6 +75,7 @@ let(:quota_source_type) { nil } let(:quota_source) { nil } let(:errormsg) { 'ERROR - quota_source not found' } + it "when no quota source" do expect { described_class.new(ae_service).main }.to raise_error(errormsg) end