Skip to content

Commit

Permalink
Merge pull request #39 from ATIX-AG/38_disksize
Browse files Browse the repository at this point in the history
report correct disk size when using local storage
  • Loading branch information
tristanrobert authored Jan 2, 2019
2 parents 30f309c + 9f232d0 commit fd01ae6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
19 changes: 17 additions & 2 deletions lib/fog/proxmox/helpers/disk_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def self.extract_option(name, disk_value)
end

def self.extract_storage_volid_size(disk_value)
#volid definition: <VOULME_ID>:=<STORAGE_ID>:<storage type dependent volume name>
values_a = disk_value.scan(/^(([\w-]+)[:]{0,1}([\w\/\.-]+))/)
no_cdrom = !disk_value.match(/^(.+)[,]{1}(media=cdrom)$/)
creation = disk_value.match(/^(([\w-]+)[:]{1}([\d]+))/)
creation = disk_value.split(',')[0].match(/^(([\w-]+)[:]{1}([\d]+))$/)
values = values_a.first if values_a
if no_cdrom
if creation
Expand All @@ -79,8 +80,22 @@ def self.extract_storage_volid_size(disk_value)
[storage, volid, size]
end

def self.to_bytes(size)
val=size.match(/\d+(\w?)/)
m=0
case val[1]
when "K" then m=1
when "M" then m=2
when "G" then m=3
when "T" then m=4
when "P" then m=5
end
val[0].to_i*1024**m
end

def self.extract_size(disk_value)
extract_option('size', disk_value).to_i
size=extract_option('size', disk_value)
self.to_bytes(size)
end
end
end
Expand Down
31 changes: 26 additions & 5 deletions spec/helpers/disk_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
{ scsi0: 'local-lvm:vm-100-disk-1,size=8G,cache=none'}
end

let(:virtio1) do
{ id: 'virtio1', volid: 'local:108/vm-108-disk-1.qcow2,size=15G' }
end

let(:virtio) do
{ virtio1: 'local:108/vm-108-disk-1.qcow2,size=245'}
end

let(:cdrom_none) do
{ ide2: 'none,media=cdrom'}
end
Expand All @@ -54,7 +62,11 @@
end

describe '#extract_controller' do
it "returns controller" do
it "returns virtio controller" do
controller = Fog::Proxmox::DiskHelper.extract_controller(virtio1[:id])
assert_equal('virtio', controller)
end
it "returns scsi controller" do
controller = Fog::Proxmox::DiskHelper.extract_controller(scsi0[:id])
assert_equal('scsi', controller)
end
Expand All @@ -72,7 +84,13 @@
storage, volid, size = Fog::Proxmox::DiskHelper.extract_storage_volid_size(scsi[:scsi0])
assert_equal('local-lvm', storage)
assert_equal('local-lvm:vm-100-disk-1', volid)
assert_equal(8, size)
assert_equal(8589934592, size)
end
it "returns virtio get local storage volid and size" do
storage, volid, size = Fog::Proxmox::DiskHelper.extract_storage_volid_size(virtio[:virtio1])
assert_equal('local', storage)
assert_equal('local:108/vm-108-disk-1.qcow2', volid)
assert_equal(245, size)
end
it "returns scsi0 creation storage and volid" do
disk = Fog::Proxmox::DiskHelper.flatten(scsi0)
Expand All @@ -94,11 +112,14 @@
assert_nil size
end
end

describe '#extract_size' do
it "returns size" do
size = Fog::Proxmox::DiskHelper.extract_size(scsi[:scsi0])
assert_equal(8, size)
assert_equal(8589934592, size)
end
it "returns size" do
size = Fog::Proxmox::DiskHelper.extract_size(virtio[:virtio1])
assert_equal(245, size)
end
end
end
end

0 comments on commit fd01ae6

Please sign in to comment.