Skip to content

Commit

Permalink
Merge pull request #17412 from yrudman/add-virtual-column-storage-to-…
Browse files Browse the repository at this point in the history
…persistent_volume-model

Add virtual columns PersistentVolume#storage_capacity and PersistentVolumeClaim#storage_capacity
  • Loading branch information
Fryguy authored May 29, 2018
2 parents 6619a2a + d922fe1 commit bfcd5a4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/models/persistent_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ class PersistentVolume < ContainerVolume
has_many :container_volumes, -> { where(:type => 'ContainerVolume') }, :through => :persistent_volume_claim
has_many :parents, -> { distinct }, :through => :container_volumes, :source_type => 'ContainerGroup'
alias_attribute :container_groups, :parents

virtual_column :storage_capacity, :type => :integer

def storage_capacity
capacity[:storage]
end
end
6 changes: 6 additions & 0 deletions app/models/persistent_volume_claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ class PersistentVolumeClaim < ApplicationRecord
serialize :requests, Hash
serialize :limits, Hash

virtual_column :storage_capacity, :type => :integer

def persistent_volume
container_volumes.find_by_type('PersistentVolume')
end

def storage_capacity
capacity[:storage]
end
end
21 changes: 21 additions & 0 deletions spec/models/persistent_volume_claim_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe PersistentVolumeClaim do
describe "#storage_capacity" do
let(:storage_size) { 123_456_789 }

it "returns value for :storage key in Hash column :capacity" do
persistent_volume = FactoryGirl.create(
:persistent_volume_claim,
:capacity => {:storage => storage_size, :foo => "something"}
)
expect(persistent_volume.storage_capacity).to eq storage_size
end

it "returns nil if there is no :storage key in Hash column :capacity" do
persistent_volume = FactoryGirl.create(
:persistent_volume_claim,
:capacity => {:foo => "something"}
)
expect(persistent_volume.storage_capacity).to be nil
end
end
end
20 changes: 20 additions & 0 deletions spec/models/persistent_volume_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,24 @@ def assert_pv_relationships(persistent_volume)
expect(persistent_volume.container_groups.first.name).to eq("group")
expect(persistent_volume.container_groups.count).to eq(1)
end

describe "#storage_capacity" do
let(:storage_size) { 123_456_789 }

it "returns value for :storage key in Hash column :capacity" do
persistent_volume = FactoryGirl.create(
:persistent_volume,
:capacity => {:storage => storage_size, :foo => "something"}
)
expect(persistent_volume.storage_capacity).to eq storage_size
end

it "returns nil if there is no :storage key in Hash column :capacity" do
persistent_volume = FactoryGirl.create(
:persistent_volume,
:capacity => {:foo => "something"}
)
expect(persistent_volume.storage_capacity).to be nil
end
end
end

0 comments on commit bfcd5a4

Please sign in to comment.