Module: ProxmoxVmVolumesHelper

Includes:
ProxmoxVmCdromHelper, ProxmoxVmCloudinitHelper
Included in:
ProxmoxVmHelper
Defined in:
app/helpers/proxmox_vm_volumes_helper.rb

Overview

Convert a foreman form server hash into a fog-proxmox server attributes hash

Instance Method Summary collapse

Methods included from ProxmoxVmCloudinitHelper

#parse_server_cloudinit

Methods included from ProxmoxVmCdromHelper

#parse_server_cdrom

Instance Method Details

#add_disk_options(disk, args) ⇒ Object



30
31
32
33
34
35
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 30

def add_disk_options(disk, args)
  options = ForemanFogProxmox::HashCollection.new_hash_reject_keys(args,
    ['id', 'volid', 'controller', 'device', 'storage', 'size', '_delete', 'storage_type'])
  ForemanFogProxmox::HashCollection.remove_empty_values(options)
  disk[:options] = options
end

#add_typed_volume(volumes, value, type) ⇒ Object



83
84
85
86
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 83

def add_typed_volume(volumes, value, type)
  volume = parse_typed_volume(value, type)
  volumes.push(volume) unless ForemanFogProxmox::Value.empty?(volume)
end

#parse_hard_disk_volume(args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 51

def parse_hard_disk_volume(args)
  logger.debug(format(_('parse_hard_disk_volume(): args=%<args>s'), args: args))
  disk = {}
  disk[:id] = args['id'] if args.key?('id')
  disk[:volid] = args['volid'] if args.key?('volid')
  disk[:storage] = args['storage'].to_s if args.key?('storage')
  disk[:size] = args['size'].to_i if args.key?('size')
  add_disk_options(disk, args) unless args.key?('options')
  disk[:options] = args['options'] if args.key?('options')
  disk.key?(:storage) ? disk : {}
end

#parse_typed_volume(args, type) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 73

def parse_typed_volume(args, type)
  logger.debug("parse_typed_volume(#{type}): args=#{args}")
  disk = parse_hard_disk_volume(args) if volume_type?(args,
    'hard_disk') || volume_type?(args, 'mp') || volume_type?(args, 'rootfs')
  disk = parse_server_cloudinit(args) if volume_type?(args, 'cloud_init')
  disk = parse_server_cdrom(args) if volume_type?(args, 'cdrom')
  logger.debug("parse_typed_volume(#{type}): disk=#{disk}")
  Fog::Proxmox::DiskHelper.flatten(disk) unless disk.empty?
end

#parse_typed_volumes(args, type) ⇒ Object



88
89
90
91
92
93
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 88

def parse_typed_volumes(args, type)
  logger.debug("parse_typed_volumes(#{type}): args=#{args}")
  volumes = []
  args&.each_value { |value| add_typed_volume(volumes, value, type) }
  volumes
end

#parsed_typed_volumes(args, type, parsed_vm) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 37

def parsed_typed_volumes(args, type, parsed_vm)
  logger.debug("parsed_typed_volumes(#{type}): args=#{args}")
  volumes_attributes = args['volumes_attributes']
  unless ForemanFogProxmox::Value.empty?(args['config_attributes'])
    volumes_attributes ||= args['config_attributes']['volumes_attributes']
  end
  unless ForemanFogProxmox::Value.empty?(args['vm_attrs'])
    volumes_attributes ||= args['vm_attrs']['volumes_attributes']
  end
  volumes = parse_typed_volumes(volumes_attributes, type)
  volumes.each { |volume| parsed_vm = parsed_vm.merge(volume) }
  parsed_vm
end

#remove_volume_keys(args) ⇒ Object



95
96
97
98
99
100
101
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 95

def remove_volume_keys(args)
  if args.key?('volumes_attributes')
    args['volumes_attributes'].each_value do |volume_attributes|
      ForemanFogProxmox::HashCollection.remove_keys(volume_attributes, ['_delete'])
    end
  end
end

#volume_type?(args, type) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
# File 'app/helpers/proxmox_vm_volumes_helper.rb', line 63

def volume_type?(args, type)
  if args.key?('storage_type')
    args['storage_type'] == type
  else
    Fog::Proxmox::DiskHelper.cloud_init?(args['volid']) if type == 'cloud_init'
    Fog::Proxmox::DiskHelper.cdrom?(args['volid']) if type == 'cdrom'
    Fog::Proxmox::DiskHelper.disk?(args['id']) if ['hard_disk', 'rootfs', 'mp'].include?(type)
  end
end