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

Vm rollup defaults (ruby solution) #14478

Merged
merged 2 commits into from
Mar 24, 2017
Merged
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
4 changes: 3 additions & 1 deletion app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1521,9 +1521,11 @@ def changed_vm_value?(options)
# Hardware Disks/Memory storage methods
#

virtual_delegate :allocated_disk_storage, :used_disk_storage, :provisioned_storage,
virtual_delegate :allocated_disk_storage, :used_disk_storage,
:to => :hardware, :allow_nil => true, :uses => {:hardware => :disks}

virtual_delegate :provisioned_storage, :to => :hardware, :allow_nil => true, :default => 0

def used_storage
used_disk_storage.to_i + ram_size_in_bytes
end
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/ar_virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def define_delegate(method_name, method, to: nil, allow_nil: nil, default: nil)
if allow_nil
method_def = <<-METHOD
def #{method_name}(#{definition})
return self[:#{method_name}] if has_attribute?(:#{method_name})
return self[:#{method_name}]#{default} if has_attribute?(:#{method_name})
_ = #{to}
if !_.nil? || nil.respond_to?(:#{method})
_.#{method}(#{definition})
Expand All @@ -159,7 +159,7 @@ def #{method_name}(#{definition})

method_def = <<-METHOD
def #{method_name}(#{definition})
return self[:#{method_name}] if has_attribute?(:#{method_name})
return self[:#{method_name}]#{default} if has_attribute?(:#{method_name})
_ = #{to}
_.#{method}(#{definition})#{default}
rescue NoMethodError => e
Expand Down
16 changes: 16 additions & 0 deletions spec/models/vm_or_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,22 @@
end
end

describe "#provisioned_storage" do
context "with no hardware" do
let(:vm) { FactoryGirl.create(:vm_vmware) }

it "calculates in ruby" do
expect(vm.provisioned_storage).to eq(0.0)
end

it "uses calculated (inline) attribute" do
vm # make sure the record is created
vm2 = VmOrTemplate.select(:id, :provisioned_storage).first
expect { expect(vm2.provisioned_storage).to eq(0) }.to match_query_limit_of(0)
end
end
end

describe ".ram_size", ".mem_cpu" do
let(:vm) { FactoryGirl.create(:vm_vmware, :hardware => hardware) }
let(:hardware) { FactoryGirl.create(:hardware, :memory_mb => 10) }
Expand Down