Module: ProxmoxVmAttrsHelper

Defined in:
app/helpers/proxmox_vm_attrs_helper.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#additional_attrs(vms, param_scope, start_checked) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 104

def additional_attrs(vms, param_scope, start_checked)
  attributes = {
    pool: vms.pool,
    image_id: vms.image_id,
    cpu_type: vms.config.cpu_type,
    nameserver: vms.config.nameserver,
    searchdomain: vms.config.searchdomain,
    hostname: vms.config.hostname,
    ostemplate_storage: vms.ostemplate_storage,
    ostemplate_file: vms.ostemplate_file,
    start_after_create: vms.start_after_create,
    templated: vms.templated,
  }
  vms_keys = [:pool, :start_after_create]
  extra_attrs = ActiveSupport::HashWithIndifferentAccess.new
  attributes.each do |key, value|
    camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
    nested_key = vms_keys.include?(key) ? key : "config_attributes[#{key}]"
    value = start_checked if key == :start_after_create
    extra_attrs[camel_key] = { name: "#{param_scope}[#{nested_key}]", value: value }
  end
  extra_attrs
end

#cpu_flags_attrs(param_scope, config) ⇒ Object



48
49
50
51
52
53
54
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 48

def cpu_flags_attrs(param_scope, config)
  flag_attrs = ActiveSupport::HashWithIndifferentAccess.new
  Fog::Proxmox::CpuHelper.flags.each do |key, _val|
    flag_attrs.merge!({ key => { :name => "#{param_scope}[config_attributes][#{key}]", :value => config.public_send(key) } })
  end
  flag_attrs
end

#network_attrs(param_scope, interfaces) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 91

def network_attrs(param_scope, interfaces)
  networks_attrs = []
  interfaces.each_with_index do |interface, id|
    attrs = ActiveSupport::HashWithIndifferentAccess.new
    interface.all_attributes.each do |key, value|
      camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
      attrs[camel_key] = { :name => "#{param_scope}[interfaces_attributes][#{id}][#{key}]", :value => value }
    end
    networks_attrs << { :name => 'interface', :value => attrs }
  end
  networks_attrs
end

#object_to_attributes_hash(vms, from_profile, start_checked) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 28

def object_to_attributes_hash(vms, from_profile, start_checked)
  param_scope = from_profile ? "compute_attribute[vm_attrs]" : "host[compute_attributes]"
  vm_h = ActiveSupport::HashWithIndifferentAccess.new
  keys = [:vmid, :node_id, :type, :pool]
  main = vms.attributes.select { |key, _value| keys.include? key }
  vms.config.all_attributes.each do |key, value|
    camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
    vm_h[camel_key] = { :name => "#{param_scope}[config_attributes][#{key}]", :value => value } unless keys.include? key
  end
  main.each do |key, value|
    camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
    vm_h[camel_key] = { :name => "#{param_scope}[#{key}]", :value => value } if keys.include? key
  end

  vm_h.merge!(additional_attrs(vms, param_scope, start_checked))
  vm_h[:interfaces] = network_attrs(param_scope, vms.interfaces)
  vm_h[:disks] = volumes_attrs(param_scope, vms.volumes)
  vm_h.merge(cpu_flags_attrs(param_scope, vms.config))
end

#snake_to_camel(str) ⇒ Object



128
129
130
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 128

def snake_to_camel(str)
  str.split('_').inject([]) { |buffer, e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
end

#vol_keys(param_scope, keys, vol, id) ⇒ Object



82
83
84
85
86
87
88
89
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 82

def vol_keys(param_scope, keys, vol, id)
  attrs = ActiveSupport::HashWithIndifferentAccess.new
  keys.each do |key|
    camel_key = key.to_s.include?('_') ? snake_to_camel(key.to_s).to_sym : key
    attrs[camel_key] = { :name => "#{param_scope}[volumes_attributes][#{id}][#{key}]", :value => vol.public_send(key) }
  end
  attrs
end

#volumes_attrs(param_scope, volumes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/proxmox_vm_attrs_helper.rb', line 56

def volumes_attrs(param_scope, volumes)
  vol_attrs = []
  volumes.each_with_index do |vol, id|
    keys = []
    type = ""
    if vol.rootfs?
      keys = ['id', 'volid', 'storage', 'size', 'storage_type']
      type = 'rootfs'
    elsif vol.hard_disk?
      keys = ['id', 'volid', 'storage_type', 'storage', 'controller', 'device', 'cache', 'size']
      type = 'hard_disk'
    elsif  vol.cdrom?
      keys = ['id', 'storage_type', 'cdrom', 'storage', 'volid']
      type = 'cdrom'
    elsif vol.cloud_init?
      keys = ['id', 'volid', 'storage_type', 'storage', 'controller', 'device']
      type = 'cloud_init'
    elsif vol.mount_point?
      keys = ['id', 'volid', 'storage_type', 'storage', 'device', 'mp', 'size']
      type = 'mount_point'
    end
    vol_attrs << { :name => type, :value => vol_keys(param_scope, keys, vol, id) }
  end
  vol_attrs
end