Skip to content

Commit

Permalink
feat: ✨ memory unit is Mb and disk size is Gb
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanrobert committed Dec 1, 2022
1 parent 22ae627 commit c5410b5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
14 changes: 7 additions & 7 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-11-30 13:58:22 UTC using RuboCop version 1.39.0.
# on 2022-12-01 11:01:10 UTC using RuboCop version 1.39.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -52,7 +52,7 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 128
Max: 133

# Offense count: 5
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
Expand All @@ -71,7 +71,7 @@ Naming/ConstantName:
Exclude:
- 'lib/fog/proxmox/helpers/ip_helper.rb'

# Offense count: 5
# Offense count: 6
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.
# NamePrefix: is_, has_, have_
# ForbiddenPrefixes: is_, has_, have_
Expand All @@ -80,6 +80,7 @@ Naming/ConstantName:
Naming/PredicateName:
Exclude:
- 'spec/**/*'
- 'lib/fog/proxmox/compute/models/disk.rb'
- 'lib/fog/proxmox/helpers/nic_helper.rb'
- 'lib/fog/proxmox/identity/models/pool.rb'

Expand All @@ -93,7 +94,7 @@ RSpec/BeforeAfterAll:
- 'spec/identity_spec.rb'
- 'spec/network_spec.rb'

# Offense count: 127
# Offense count: 131
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: SkipBlocks, EnforcedStyle.
# SupportedStyles: described_class, explicit
Expand Down Expand Up @@ -137,16 +138,15 @@ RSpec/InstanceVariable:
- 'spec/identity_spec.rb'
- 'spec/network_spec.rb'

# Offense count: 31
# Offense count: 32
# Configuration parameters: AllowSubject.
RSpec/MultipleMemoizedHelpers:
Max: 12

# Offense count: 4
# Offense count: 2
RSpec/RepeatedDescription:
Exclude:
- 'spec/helpers/cpu_helper_spec.rb'
- 'spec/helpers/disk_helper_spec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Expand Down
7 changes: 6 additions & 1 deletion lib/fog/proxmox/compute/models/disk.rb
Expand Up @@ -23,7 +23,8 @@
module Fog
module Proxmox
class Compute
# class Disk model
# class Disk model: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/{qemu|lxc}/{vmid}/config
# size is in Gb
class Disk < Fog::Model
identity :id
attribute :volid
Expand Down Expand Up @@ -83,6 +84,10 @@ def flatten
def to_s
Fog::Proxmox::Hash.flatten(flatten)
end

def has_volume?
!volid.empty?
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/fog/proxmox/compute/models/server_config.rb
Expand Up @@ -24,7 +24,8 @@
module Fog
module Proxmox
class Compute
# ServerConfig model
# ServerConfig model: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/{qemu|lxc}/{vmid}/config
# memory, balloon, shares and swap are in Mb
class ServerConfig < Fog::Model
identity :vmid
attribute :description
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/proxmox/compute/models/storage.rb
Expand Up @@ -29,7 +29,7 @@
module Fog
module Proxmox
class Compute
# class Storage model
# class Storage model: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/storage/{storage}
class Storage < Fog::Model
identity :storage
attribute :node_id, aliases: :node
Expand Down
3 changes: 2 additions & 1 deletion lib/fog/proxmox/compute/models/volume.rb
Expand Up @@ -31,7 +31,8 @@
module Fog
module Proxmox
class Compute
# class Volume model
# class Volume model: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/storage/{storage}/content/{volume}
# size is in bytes
class Volume < Fog::Model
identity :volid
attribute :content
Expand Down
8 changes: 7 additions & 1 deletion lib/fog/proxmox/helpers/disk_helper.rb
Expand Up @@ -129,9 +129,15 @@ def self.to_human_bytes(size)
human_size
end

def self.to_int_gb(size_bytes)
val = to_human_bytes(size_bytes.to_i).match(/\d+(Gb)/)
val ? val[0].to_i : 0
end

def self.extract_size(disk_value)
size = extract_option('size', disk_value)
size ? to_bytes(size) : '1G'
size = to_int_gb(to_bytes(size)).to_s if size.match?(/\d+(G)/)
size
end

def self.disk?(id)
Expand Down
42 changes: 33 additions & 9 deletions spec/helpers/disk_helper_spec.rb
Expand Up @@ -30,15 +30,15 @@
end

let(:scsi) do
{ scsi0: 'local-lvm:vm-100-disk-1,size=8G,cache=none' }
{ scsi0: 'local-lvm:vm-100-disk-1,size=8,cache=none' }
end

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

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

let(:ide2) do
Expand Down Expand Up @@ -115,14 +115,21 @@
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_589_934_592, size)
assert_equal('8', 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)
assert_equal('2', size)
end

it 'returns virtio1 get local storage volid and size' do
storage, volid, size = Fog::Proxmox::DiskHelper.extract_storage_volid_size(virtio1[:volid])
assert_equal('local', storage)
assert_equal('local:108/vm-108-disk-1.qcow2', volid)
assert_equal('2', size)
end

it 'returns scsi0 creation storage and volid' do
Expand All @@ -149,14 +156,19 @@
end

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

it 'returns size' do
it 'size=2 returns size 2' do
size = Fog::Proxmox::DiskHelper.extract_size(virtio[:virtio1])
assert_equal(245, size)
assert_equal('2', size)
end

it 'size=2G returns size 2' do
size = Fog::Proxmox::DiskHelper.extract_size(virtio1[:volid])
assert_equal('2', size)
end
end

Expand Down Expand Up @@ -240,6 +252,18 @@
end
end

describe '#to_int_gb' do
it '8589934592 returns 8' do
size = Fog::Proxmox::DiskHelper.to_int_gb(8_589_934_592)
assert_equal(8, size)
end

it '2 returns 0' do
size = Fog::Proxmox::DiskHelper.to_int_gb(2)
assert_equal(0, size)
end
end

describe '#template?' do
it 'local:100/base-100-disk-0.qcow2 returns true' do
assert Fog::Proxmox::DiskHelper.template?(template[:virtio0])
Expand Down

0 comments on commit c5410b5

Please sign in to comment.