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 3c653ad5a..d6fd55301 100644 --- a/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used.rb +++ b/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used.rb @@ -2,14 +2,47 @@ # Description: calculate entity used quota values # -def consumption(source) - { - :cpu => source.allocated_vcpu, - :memory => source.allocated_memory, - :vms => source.vms.count { |vm| vm.id if vm.ems_id }, - :storage => source.allocated_storage, - :provisioned_storage => source.provisioned_storage - } +module ManageIQ + module Automate + module System + module CommonMethods + module QuotaMethods + class Used + def initialize(handle = $evm) + @handle = handle + end + + def main + used(quota_source) + end + + private + + def used(quota_source) + @handle.root['quota_used'] = consumption(quota_source) + end + + def quota_source + raise "ERROR - quota_source not found" unless @handle.root['quota_source'] + @handle.root['quota_source'] + end + + def consumption(source) + { + :cpu => source.allocated_vcpu, + :memory => source.allocated_memory, + :vms => source.vms.count { |vm| vm.id if vm.ems_id }, + :storage => source.allocated_storage, + :provisioned_storage => source.provisioned_storage + } + end + end + end + end + end + end end -$evm.root['quota_used'] = consumption($evm.root['quota_source']) if $evm.root['quota_source'] +if __FILE__ == $PROGRAM_NAME + ManageIQ::Automate::System::CommonMethods::QuotaMethods::Used.new.main +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 new file mode 100644 index 000000000..0a8bbf879 --- /dev/null +++ b/spec/content/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/used_spec.rb @@ -0,0 +1,76 @@ +require_domain_file + +describe ManageIQ::Automate::System::CommonMethods::QuotaMethods::Used do + include Spec::Support::QuotaHelper + + let!(:model) { setup_model } + let(:root_hash) do + { + 'miq_provision_request' => @miq_provision_request, + 'miq_request' => @miq_provision_request, + 'quota_source' => quota_source, + 'quota_source_type' => quota_source_type + } + end + + let(:counts_hash) do + {:storage => 1_000_000, :cpu => 0, :vms => 4, :memory => 1_073_741_824} + end + + let(:root_object) do + Spec::Support::MiqAeMockObject.new(root_hash) + end + + let(:ae_service) do + Spec::Support::MiqAeMockService.new(root_object).tap do |service| + current_object = Spec::Support::MiqAeMockObject.new + current_object.parent = root_object + service.object = current_object + end + end + + shared_examples_for "used" do + it "check" do + described_class.new(ae_service).main + expect(ae_service.root['quota_used']).to include(counts_hash) + end + end + + context "returns ok for tenant counts" do + let(:quota_source) { @tenant } + let(:quota_source_type) { 'tenant' } + let(:status_and_message) { [true, ""] } + let(:ae_result) { "ok" } + let(:errormsg) { 'ERROR - quota_source not found' } + it_behaves_like "used" + end + + context "returns ok for user counts" do + let(:quota_source) { @tenant } + let(:quota_source_type) { 'user' } + let(:status_and_message) { [true, ""] } + let(:ae_result) { "ok" } + let(:errormsg) { 'ERROR - quota_source not found' } + it_behaves_like "used" + end + + context "returns ok for group counts" do + let(:quota_source) { @tenant } + let(:quota_source_type) { 'group' } + let(:status_and_message) { [true, ""] } + let(:ae_result) { "ok" } + let(:errormsg) { 'ERROR - quota_source not found' } + it_behaves_like "used" + end + + context "returns error " do + let(:quota_source_type) { nil } + let(:quota_source) { nil } + let(:status_and_message) { [true, ""] } + let(:errormsg) { 'ERROR - quota_source not found' } + it "when no quota source" do + allow(ae_service).to receive(:used).and_return(status_and_message) + expect { described_class.new(ae_service).main }.to raise_error(errormsg) + end + end +end