foreman_fog_proxmox 0.12.2 → 0.13.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +24 -5
  3. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_compute_resource.js +36 -2
  4. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm.js +134 -60
  5. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_vm_server.js +2 -50
  6. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_volume.js +39 -0
  7. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_volume_cdrom.js +63 -0
  8. data/app/assets/javascripts/foreman_fog_proxmox/proxmox_volume_cloudinit.js +25 -0
  9. data/app/controllers/concerns/foreman_fog_proxmox/controller/parameters/compute_resource.rb +1 -1
  10. data/app/controllers/foreman_fog_proxmox/compute_resources_controller.rb +23 -25
  11. data/app/helpers/proxmox_compute_controllers_helper.rb +39 -0
  12. data/app/helpers/proxmox_compute_resources_helper.rb +49 -0
  13. data/app/helpers/proxmox_compute_selectors_helper.rb +6 -44
  14. data/app/helpers/proxmox_form_helper.rb +12 -4
  15. data/app/{models/concerns/fog_extensions/proxmox/volume.rb → helpers/proxmox_storages_helper.rb} +5 -8
  16. data/app/helpers/proxmox_vm_cdrom_helper.rb +35 -0
  17. data/app/helpers/proxmox_vm_cloudinit_helper.rb +43 -0
  18. data/app/helpers/proxmox_vm_config_helper.rb +160 -0
  19. data/app/helpers/proxmox_vm_helper.rb +24 -62
  20. data/app/helpers/proxmox_vm_interfaces_helper.rb +85 -0
  21. data/app/helpers/proxmox_vm_os_template_helper.rb +47 -0
  22. data/app/{models/foreman_fog_proxmox/proxmox_token_expiration.rb → helpers/proxmox_vm_uuid_helper.rb} +14 -10
  23. data/app/helpers/proxmox_vm_volumes_helper.rb +105 -0
  24. data/app/models/concerns/fog_extensions/proxmox/disk.rb +17 -2
  25. data/app/models/concerns/fog_extensions/proxmox/interface.rb +19 -4
  26. data/app/models/concerns/fog_extensions/proxmox/server.rb +12 -3
  27. data/app/models/concerns/fog_extensions/proxmox/server_config.rb +8 -30
  28. data/app/models/concerns/host_ext/proxmox/interfaces.rb +7 -2
  29. data/app/models/concerns/orchestration/proxmox/compute.rb +49 -0
  30. data/app/models/foreman_fog_proxmox/proxmox.rb +58 -15
  31. data/app/models/foreman_fog_proxmox/proxmox_compute_attributes.rb +14 -18
  32. data/app/models/foreman_fog_proxmox/proxmox_connection.rb +14 -9
  33. data/app/models/foreman_fog_proxmox/proxmox_images.rb +2 -1
  34. data/app/models/foreman_fog_proxmox/proxmox_interfaces.rb +53 -28
  35. data/app/models/foreman_fog_proxmox/proxmox_operating_systems.rb +1 -1
  36. data/app/models/foreman_fog_proxmox/proxmox_version.rb +7 -2
  37. data/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb +19 -31
  38. data/app/models/foreman_fog_proxmox/proxmox_vm_new.rb +108 -94
  39. data/app/models/foreman_fog_proxmox/proxmox_vm_queries.rb +9 -6
  40. data/app/models/foreman_fog_proxmox/proxmox_volumes.rb +79 -22
  41. data/app/services/foreman_fog_proxmox/node_dashboard/data.rb +6 -2
  42. data/app/views/api/v2/compute_resources/proxmox.json.rabl +1 -1
  43. data/app/views/compute_resources/form/_proxmox.html.erb +23 -10
  44. data/app/views/compute_resources/show/_proxmox.html.erb +6 -6
  45. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_nic_provider_specific_form.html.erb +3 -1
  46. data/app/views/compute_resources_vms/form/proxmox/_add_vm_type_to_volumes_edit.html.erb +46 -29
  47. data/app/views/compute_resources_vms/form/proxmox/_base.html.erb +3 -3
  48. data/app/views/compute_resources_vms/form/proxmox/_removable_layout.html.erb +2 -1
  49. data/app/views/compute_resources_vms/form/proxmox/container/_network.html.erb +8 -7
  50. data/app/views/compute_resources_vms/form/proxmox/server/_advanced.html.erb +0 -2
  51. data/app/views/compute_resources_vms/form/proxmox/server/_config.html.erb +15 -14
  52. data/app/views/compute_resources_vms/form/proxmox/server/_network.html.erb +2 -2
  53. data/app/views/compute_resources_vms/form/proxmox/server/_volume_cdrom.html.erb +34 -0
  54. data/app/views/compute_resources_vms/form/proxmox/server/_volume_cloud_init.html.erb +29 -0
  55. data/app/views/compute_resources_vms/form/proxmox/server/{_volume.html.erb → _volume_hard_disk.html.erb} +7 -3
  56. data/app/views/compute_resources_vms/show/_proxmox.html.erb +2 -0
  57. data/config/routes.rb +7 -7
  58. data/db/migrate/20210312105013_update_proxmox_uuid_host.rb +29 -0
  59. data/lib/foreman_fog_proxmox/engine.rb +15 -10
  60. data/lib/foreman_fog_proxmox/hash_collection.rb +69 -0
  61. data/lib/foreman_fog_proxmox/version.rb +1 -1
  62. data/lib/tasks/foreman_fog_proxmox_tasks.rake +0 -3
  63. data/test/factories/foreman_fog_proxmox/proxmox_container_mock_factory.rb +20 -8
  64. data/test/factories/foreman_fog_proxmox/proxmox_node_mock_factory.rb +5 -5
  65. data/test/factories/foreman_fog_proxmox/proxmox_server_mock_factory.rb +17 -7
  66. data/test/factories/proxmox_factory.rb +4 -4
  67. data/test/functional/compute_resources_controller_test.rb +4 -4
  68. data/test/unit/foreman_fog_proxmox/helpers/proxmox_container_helper_test.rb +53 -32
  69. data/test/unit/foreman_fog_proxmox/helpers/proxmox_server_helper_test.rb +56 -31
  70. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_helper_test.rb +22 -20
  71. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_uuid_helper_test.rb +38 -0
  72. data/test/unit/foreman_fog_proxmox/helpers/proxmox_vm_volumes_helper_test.rb +50 -0
  73. data/test/unit/foreman_fog_proxmox/proxmox_compute_attributes_test.rb +10 -11
  74. data/test/unit/foreman_fog_proxmox/proxmox_interfaces_test.rb +38 -10
  75. data/test/unit/foreman_fog_proxmox/proxmox_version_test.rb +10 -10
  76. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_container_test.rb +34 -24
  77. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_create_test.rb +8 -8
  78. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_cdrom_test.rb +181 -0
  79. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_cloudinit_test.rb +131 -0
  80. data/test/unit/foreman_fog_proxmox/{proxmox_vm_commands_server_update_volumes_test.rb → proxmox_vm_commands_server_update_hard_disk_test.rb} +45 -19
  81. data/test/unit/foreman_fog_proxmox/proxmox_vm_commands_server_update_test.rb +21 -21
  82. data/test/unit/foreman_fog_proxmox/proxmox_vm_new_test.rb +3 -3
  83. data/test/unit/foreman_fog_proxmox/proxmox_vm_queries_test.rb +3 -3
  84. metadata +44 -23
  85. data/app/helpers/proxmox_container_helper.rb +0 -163
  86. data/app/helpers/proxmox_server_helper.rb +0 -155
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/helpers/disk_helper'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'foreman_fog_proxmox/value'
23
+ require 'foreman_fog_proxmox/hash_collection'
24
+
25
+ # Convert a foreman form server hash into a fog-proxmox server attributes hash
26
+ module ProxmoxVmCdromHelper
27
+ def parse_server_cdrom(args)
28
+ cdrom_media = args['cdrom'] if args.key?('cdrom')
29
+ cdrom_image = args['volid'] if args.key?('volid')
30
+ volid = cdrom_image.empty? ? cdrom_media : cdrom_image
31
+ return {} unless volid
32
+
33
+ { id: 'ide2', volid: volid, media: 'cdrom' }
34
+ end
35
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/helpers/disk_helper'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'foreman_fog_proxmox/value'
23
+ require 'foreman_fog_proxmox/hash_collection'
24
+
25
+ # Convert a foreman form server hash into a fog-proxmox server attributes hash
26
+ module ProxmoxVmCloudinitHelper
27
+ def parse_server_cloudinit(args)
28
+ cloudinit_h = {}
29
+ cloudinit = args['cloudinit']
30
+ unless ['none'].include? cloudinit
31
+ volid = args['volid']
32
+ storage = args['storage']
33
+ cloudinit_volid = volid if volid
34
+ cloudinit_volid ||= "#{storage}:cloudinit" if storage
35
+ controller = args['controller']
36
+ device = args['device']
37
+ id = "#{controller}#{device}" if controller && device
38
+ cloudinit_h.store(:id, id.to_sym) if id
39
+ cloudinit_h.store(:volid, cloudinit_volid) if cloudinit_volid
40
+ end
41
+ cloudinit_h
42
+ end
43
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/helpers/disk_helper'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'fog/proxmox/helpers/cpu_helper'
23
+ require 'foreman_fog_proxmox/value'
24
+ require 'foreman_fog_proxmox/hash_collection'
25
+
26
+ # Convert a foreman form server hash into a fog-proxmox server attributes hash
27
+ module ProxmoxVmConfigHelper
28
+ KILO = 1024
29
+ MEGA = KILO * KILO
30
+ GIGA = KILO * MEGA
31
+
32
+ def object_to_config_hash(vm, type)
33
+ vm_h = ActiveSupport::HashWithIndifferentAccess.new
34
+ main_a = ['vmid']
35
+ main = vm.attributes.select { |key, _value| main_a.include? key }
36
+ main_a += ['templated']
37
+ config = vm.config.attributes.reject { |key, _value| main_a.include?(key) || Fog::Proxmox::DiskHelper.disk?(key) || Fog::Proxmox::NicHelper.nic?(key) }
38
+ vm_h = vm_h.merge(main)
39
+ vm_h = vm_h.merge('config_attributes': config)
40
+ logger.debug(format(_('object_to_config_hash(%<type>s): vm_h=%<vm_h>s'), type: type, vm_h: vm_h))
41
+ vm_h
42
+ end
43
+
44
+ def convert_memory_size(config_hash, key)
45
+ # default unit memory size is Mb
46
+ memory = (config_hash[key].to_i / MEGA).to_s == '0' ? config_hash[key] : (config_hash[key].to_i / MEGA).to_s
47
+ config_hash.store(key, memory)
48
+ end
49
+
50
+ def general_a(type)
51
+ general_a = ['node_id', 'type', 'config_attributes', 'volumes_attributes', 'interfaces_attributes']
52
+ general_a += ['firmware_type', 'provision_method', 'container_volumes', 'server_volumes', 'start_after_create']
53
+ general_a += ['name'] if type == 'lxc'
54
+ general_a
55
+ end
56
+
57
+ def config_typed_keys(type)
58
+ keys = { general: general_a(type) }
59
+ main_a = ['name', 'type', 'node_id', 'vmid', 'interfaces', 'mount_points', 'disks']
60
+ case type
61
+ when 'lxc'
62
+ cpu_a = ['arch', 'cpulimit', 'cpuunits', 'cores', 'sockets']
63
+ memory_a = ['memory', 'swap']
64
+ ostemplate_a = ['ostemplate', 'ostemplate_storage', 'ostemplate_file']
65
+ keys.store(:ostemplate, ostemplate_a)
66
+ when 'qemu'
67
+ cpu_a = ['cpu_type', 'cpu']
68
+ cpu_a += ForemanFogProxmox::HashCollection.stringify_keys(Fog::Proxmox::CpuHelper.flags).keys
69
+ memory_a = ['memory', 'balloon', 'shares']
70
+ cloud_init_a = ['ciuser', 'cipassword', 'searchdomain', 'nameserver']
71
+ keys.store(:cloud_init, cloud_init_a)
72
+ end
73
+ keys.store(:main, main_a)
74
+ keys.store(:cpu, cpu_a)
75
+ keys.store(:memory, memory_a)
76
+ keys
77
+ end
78
+
79
+ def convert_memory_sizes(args)
80
+ ['memory', 'balloon', 'shares', 'swap'].each { |key| convert_memory_size(args['config_attributes'], key) }
81
+ end
82
+
83
+ def config_general_or_ostemplate_key?(key)
84
+ config_typed_keys('lxc')[:general].include?(key) || config_typed_keys(type)[:ostemplate].include?(key)
85
+ end
86
+
87
+ def config_a(type)
88
+ config_a = []
89
+ case type
90
+ when 'qemu'
91
+ [:cpu, :memory, :general, :cloud_init].each { |key| config_a += config_typed_keys(type)[key] }
92
+ when 'lxc'
93
+ [:main].each { |key| config_a += config_typed_keys(type)[key] }
94
+ end
95
+ config_a
96
+ end
97
+
98
+ def args_a(type)
99
+ args_a = []
100
+ case type
101
+ when 'qemu'
102
+ [:general].each { |key| args_a += config_typed_keys(type)[key] }
103
+ when 'lxc'
104
+ [:general, :ostemplate].each { |key| args_a += config_typed_keys(type)[key] }
105
+ end
106
+ args_a
107
+ end
108
+
109
+ def config_options(config, args, type)
110
+ options = {}
111
+ case type
112
+ when 'qemu'
113
+ options = config.select { |key, _value| config_typed_keys(type)[:cloud_init].include? key }
114
+ when 'lxc'
115
+ options = parse_ostemplate(args, config)
116
+ end
117
+ options
118
+ end
119
+
120
+ def parsed_typed_config(args, type)
121
+ config = args['config_attributes']
122
+ config ||= ForemanFogProxmox::HashCollection.new_hash_reject_keys(args, config_typed_keys(type)[:main])
123
+ logger.debug("parsed_typed_config(#{type}): config=#{config}")
124
+ config_cpu = config.select { |key, _value| config_typed_keys(type)[:cpu].include? key }
125
+ logger.debug("parsed_typed_config(#{type}): config_cpu=#{config_cpu}")
126
+ cpu = parse_typed_cpu(config_cpu, type)
127
+ memory = parse_typed_memory(config.select { |key, _value| config_typed_keys(type)[:memory].include? key }, type)
128
+ parsed_config = config.reject { |key, value| config_a(type).include?(key) || ForemanFogProxmox::Value.empty?(value) }
129
+ parsed_vm = args.reject { |key, value| args_a(type).include?(key) || ForemanFogProxmox::Value.empty?(value) }
130
+ parsed_vm = parsed_vm.merge(config_options(config, args, type))
131
+ parsed_vm = parsed_vm.merge(parsed_config).merge(cpu).merge(memory)
132
+ logger.debug("parsed_typed_config(#{type}): parsed_vm=#{parsed_vm}")
133
+ parsed_vm
134
+ end
135
+
136
+ def parse_typed_memory(args, type)
137
+ memory = {}
138
+ ForemanFogProxmox::HashCollection.remove_empty_values(args)
139
+ config_typed_keys(type)[:memory].each { |key| ForemanFogProxmox::HashCollection.add_and_format_element(memory, key.to_sym, args, key, :to_i) }
140
+ memory
141
+ end
142
+
143
+ def parse_typed_cpu(args, type)
144
+ cpu = {}
145
+ ForemanFogProxmox::HashCollection.remove_empty_values(args)
146
+ if type == 'qemu'
147
+ logger.debug("parse_typed_cpu(#{type}): args=#{args}")
148
+ cpu_flattened = Fog::Proxmox::CpuHelper.flatten(args)
149
+ cpu_flattened = args[:cpu] if cpu_flattened.empty?
150
+ logger.debug("parse_typed_cpu(#{type}): cpu_flattened=#{cpu_flattened}")
151
+ ForemanFogProxmox::HashCollection.remove_empty_values(args)
152
+ ForemanFogProxmox::HashCollection.remove_keys(args, config_typed_keys('qemu')[:cpu])
153
+ args.each_value(&:to_i)
154
+ cpu = { cpu: cpu_flattened }
155
+ end
156
+ config_typed_keys('lxc')[:cpu].each { |key| ForemanFogProxmox::HashCollection.add_and_format_element(cpu, key.to_sym, args, key) } if type == 'lxc'
157
+ logger.debug("parse_typed_cpu(#{type}): cpu=#{cpu}")
158
+ cpu
159
+ end
160
+ end
@@ -22,67 +22,29 @@ require 'fog/proxmox/helpers/nic_helper'
22
22
  require 'foreman_fog_proxmox/value'
23
23
 
24
24
  module ProxmoxVmHelper
25
- KILO = 1024
26
- MEGA = KILO * KILO
27
- GIGA = KILO * MEGA
28
-
29
- def object_to_config_hash(vm)
30
- vm_h = ActiveSupport::HashWithIndifferentAccess.new
31
- main_a = ['hostname', 'name', 'vmid']
32
- main = vm.attributes.select { |key, _value| main_a.include? key }
33
- main_a += ['templated']
34
- config = vm.config.attributes.reject { |key, _value| main_a.include?(key) || Fog::Proxmox::DiskHelper.disk?(key) || Fog::Proxmox::NicHelper.nic?(key) }
35
- vm_h = vm_h.merge(main)
36
- vm_h = vm_h.merge('config_attributes': config)
37
- vm_h
38
- end
39
-
40
- def add_cdrom_to_config_server(vm, config)
41
- cd_disks = vm.config.disks.select { |disk| disk.id == 'ide2' }
42
- cdrom = {}
43
- disk_to_cdrom(cd_disks.first, cdrom)
44
- config = config.merge(cdrom)
45
- config
46
- end
47
-
48
- def disk_to_cdrom(disk, cdrom)
49
- volid = disk.volid
50
- cdrom_a = ['none', 'cdrom']
51
- if cdrom_a.include? volid
52
- cdrom.store('cdrom', volid)
53
- else
54
- cdrom.store('cdrom', 'image')
55
- cdrom.store('cdrom_iso', volid)
56
- cdrom.store('cdrom_storage', disk.storage)
57
- end
58
- end
59
-
60
- def convert_memory_sizes(args)
61
- convert_memory_size(args['config_attributes'], 'memory')
62
- convert_memory_size(args['config_attributes'], 'balloon')
63
- convert_memory_size(args['config_attributes'], 'shares')
64
- convert_memory_size(args['config_attributes'], 'swap')
65
- end
66
-
67
- def convert_volumes_size(args)
68
- args['volumes_attributes'].each_value { |value| value['size'] = (value['size'].to_i / GIGA).to_s unless ForemanFogProxmox::Value.empty?(value['size']) }
69
- end
70
-
71
- def convert_sizes(args)
72
- convert_memory_size(args['config_attributes'], 'memory')
73
- convert_memory_size(args['config_attributes'], 'balloon')
74
- convert_memory_size(args['config_attributes'], 'shares')
75
- convert_memory_size(args['config_attributes'], 'swap')
76
- args['volumes_attributes'].each_value { |value| value['size'] = (value['size'].to_i / GIGA).to_s unless ForemanFogProxmox::Value.empty?(value['size']) }
77
- end
78
-
79
- def remove_deletes(args)
80
- args['volumes_attributes']&.delete_if { |_key, value| value.key? '_delete' }
81
- end
82
-
83
- def convert_memory_size(config_hash, key)
84
- # default unit memory size is Mb
85
- memory = (config_hash[key].to_i / MEGA).to_s == '0' ? config_hash[key] : (config_hash[key].to_i / MEGA).to_s
86
- config_hash.store(key, memory)
25
+ include ProxmoxVmInterfacesHelper
26
+ include ProxmoxVmVolumesHelper
27
+ include ProxmoxVmConfigHelper
28
+ include ProxmoxVmOsTemplateHelper
29
+
30
+ def vm_collection(type)
31
+ collection = :servers
32
+ collection = :containers if type == 'lxc'
33
+ collection
34
+ end
35
+
36
+ # Convert a foreman form server/container vm hash into a fog-proxmox server/container attributes hash
37
+ def parse_typed_vm(args, type)
38
+ args = ActiveSupport::HashWithIndifferentAccess.new(args)
39
+ return {} unless args
40
+ return {} if args.empty?
41
+ return {} unless args['type'] == type
42
+
43
+ logger.debug("parse_typed_vm(#{type}): args=#{args}")
44
+ parsed_vm = parsed_typed_config(args, type)
45
+ parsed_vm = parsed_typed_interfaces(args, type, parsed_vm)
46
+ parsed_vm = parsed_typed_volumes(args, type, parsed_vm)
47
+ logger.debug("parse_typed_vm(#{type}): parsed_vm=#{parsed_vm}")
48
+ parsed_vm
87
49
  end
88
50
  end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/helpers/disk_helper'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'foreman_fog_proxmox/value'
23
+ require 'foreman_fog_proxmox/hash_collection'
24
+
25
+ # Convert a foreman form server hash into a fog-proxmox server attributes hash
26
+ module ProxmoxVmInterfacesHelper
27
+ def parsed_typed_interfaces(args, type, parsed_vm)
28
+ interfaces_to_add, interfaces_to_delete = parse_typed_interfaces(args, type)
29
+ interfaces_to_add.each { |interface| parsed_vm = parsed_vm.merge(interface) }
30
+ parsed_vm = parsed_vm.merge(delete: interfaces_to_delete.join(',')) unless interfaces_to_delete.empty?
31
+ parsed_vm
32
+ end
33
+
34
+ def parse_typed_interfaces(args, type)
35
+ interfaces_to_add = []
36
+ interfaces_to_delete = []
37
+ interfaces_attributes = args['interfaces_attributes']
38
+ interfaces_attributes ||= args['config_attributes']['interfaces_attributes'] unless ForemanFogProxmox::Value.empty?(args['config_attributes'])
39
+ interfaces_attributes&.each_value { |value| add_or_delete_typed_interface(value, interfaces_to_delete, interfaces_to_add, type) }
40
+ [interfaces_to_add, interfaces_to_delete]
41
+ end
42
+
43
+ def interface_compute_attributes_typed_keys(type)
44
+ keys = ['rate', 'bridge', 'tag']
45
+ case type
46
+ when 'qemu'
47
+ keys += ['model', 'firewall', 'link_down', 'queues']
48
+ when 'lxc'
49
+ keys += ['name', 'ip', 'ip6', 'gw', 'gw6', 'dhcp', 'dhcp6', 'cidr', 'cidr6', 'firewall']
50
+ end
51
+ keys
52
+ end
53
+
54
+ def interface_common_typed_keys(type)
55
+ [{ origin: 'id', dest: 'id' }, { origin: 'mac', dest: type == 'qemu' ? 'macaddr' : 'hwaddr' }]
56
+ end
57
+
58
+ def compute_dhcps(interface_attributes_h)
59
+ interface_attributes_h[:dhcp] = interface_attributes_h[:ip] == 'dhcp' ? '1' : '0'
60
+ interface_attributes_h[:ip] = '' if interface_attributes_h[:dhcp] == '1'
61
+ interface_attributes_h[:dhcp6] = interface_attributes_h[:ip6] == 'dhcp' ? '1' : '0'
62
+ interface_attributes_h[:ip6] = '' if interface_attributes_h[:dhcp6] == '1'
63
+ end
64
+
65
+ def add_or_delete_typed_interface(interface_attributes, interfaces_to_delete, interfaces_to_add, type)
66
+ logger.debug("add_or_delete_typed_interface(#{type}): interface_attributes=#{interface_attributes}")
67
+ ForemanFogProxmox::HashCollection.remove_empty_values(interface_attributes)
68
+ ForemanFogProxmox::HashCollection.remove_empty_values(interface_attributes['compute_attributes']) if interface_attributes['compute_attributes']
69
+ nic = {}
70
+ id = interface_attributes['id']
71
+ delete = interface_attributes['_delete'].to_i == 1
72
+ if delete
73
+ logger.debug("add_or_delete_typed_interface(#{type}): delete id=#{id}")
74
+ interfaces_to_delete.push(id.to_s)
75
+ else
76
+ interface_common_typed_keys(type).each { |key| ForemanFogProxmox::HashCollection.add_and_format_element(nic, key[:dest].to_sym, interface_attributes, key[:origin]) }
77
+ interface_attributes_h = interface_attributes['compute_attributes']
78
+ interface_attributes_h ||= interface_attributes if ForemanFogProxmox::Value.empty?(interface_attributes['compute_attributes'])
79
+ interface_compute_attributes_typed_keys(type).each { |key| ForemanFogProxmox::HashCollection.add_and_format_element(nic, key.to_sym, interface_attributes_h, key) }
80
+ compute_dhcps(interface_attributes_h)
81
+ logger.debug("add_or_delete_typed_interface(#{type}): add nic=#{nic}")
82
+ interfaces_to_add.push(Fog::Proxmox::NicHelper.flatten(nic))
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Tristan Robert
4
+
5
+ # This file is part of ForemanFogProxmox.
6
+
7
+ # ForemanFogProxmox is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # ForemanFogProxmox is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ require 'fog/proxmox/helpers/disk_helper'
21
+ require 'fog/proxmox/helpers/nic_helper'
22
+ require 'foreman_fog_proxmox/value'
23
+ require 'foreman_fog_proxmox/hash_collection'
24
+
25
+ module ProxmoxVmOsTemplateHelper
26
+ def ostemplate_keys
27
+ ['ostemplate_storage', 'ostemplate_file']
28
+ end
29
+
30
+ def parse_ostemplate_without_keys(args)
31
+ parse_container_ostemplate(args.select { |key, _value| ostemplate_keys.include? key })
32
+ end
33
+
34
+ def parse_ostemplate(args, config)
35
+ ostemplate = parse_ostemplate_without_keys(args)
36
+ ostemplate = parse_ostemplate_without_keys(config) unless ostemplate[:ostemplate]
37
+ ostemplate
38
+ end
39
+
40
+ def parse_container_ostemplate(args)
41
+ ostemplate = args['ostemplate']
42
+ ostemplate_file = args['ostemplate_file']
43
+ ostemplate ||= ostemplate_file
44
+ parsed_ostemplate = { ostemplate: ostemplate }
45
+ parsed_ostemplate
46
+ end
47
+ end