Module: Fog::Proxmox::DiskHelper

Defined in:
lib/fog/proxmox/helpers/disk_helper.rb

Overview

module Disk mixins

Class Method Summary collapse

Class Method Details

.extract(id) ⇒ Object


49
50
51
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 49

def self.extract(id)
  id.scan(/(\w+)(\d+)/).first
end

.extract_controller(id) ⇒ Object


41
42
43
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 41

def self.extract_controller(id)
  extract(id)[0]
end

.extract_device(id) ⇒ Object


45
46
47
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 45

def self.extract_device(id)
  extract(id)[1].to_i
end

.extract_option(name, disk_value) ⇒ Object


53
54
55
56
57
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 53

def self.extract_option(name, disk_value)
  values = disk_value.scan(/#{name}=(\w+)/)
  name_value = values.first if values
  name_value&.first
end

.extract_size(disk_value) ⇒ Object


96
97
98
99
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 96

def self.extract_size(disk_value)
  size=extract_option('size', disk_value)
	self.to_bytes(size)
end

.extract_storage_volid_size(disk_value) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 59

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.split(',')[0].match(/^(([\w-]+)[:]{1}([\d]+))$/)
  values = values_a.first if values_a
  if no_cdrom
    if creation
      storage = values[1]
      volid = nil
      size = values[2].to_i
    else
      storage = values[1]
      volid = values[0]
      size = extract_size(disk_value)
    end
  else
    volid = values[0]
    storage = values[1] unless %w[none cdrom].include? volid
    size = nil
  end
  [storage, volid, size]
end

.flatten(disk) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 26

def self.flatten(disk)
  volid = disk[:volid]
  value = if volid
            "#{disk[:volid]},size=#{disk[:size]}"
          else
            "#{disk[:storage]}:#{disk[:size]}"
          end
  opts = disk[:options] if disk.has_key? :options
  main_a = [:id,:volid,:storage,:size]
  opts = disk.reject { |key,_value| main_a.include? key } unless opts
  options = Fog::Proxmox::Hash.stringify(opts) if opts
  value += ",#{options}" if options
  { "#{disk[:id]}": value }
end

.to_bytes(size) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/proxmox/helpers/disk_helper.rb', line 83

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