foreman_fog_proxmox 0.12.1 → 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -6
  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 +47 -26
  85. data/app/helpers/proxmox_container_helper.rb +0 -163
  86. data/app/helpers/proxmox_server_helper.rb +0 -155
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 Tristan Robert
3
+ # Copyright 2018 Tristan Robert
4
4
 
5
5
  # This file is part of ForemanFogProxmox.
6
6
 
@@ -11,20 +11,24 @@
11
11
 
12
12
  # ForemanFogProxmox is distributed in the hope that it will be useful,
13
13
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
15
  # GNU General Public License for more details.
16
16
 
17
17
  # You should have received a copy of the GNU General Public License
18
18
  # along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
- module ForemanFogProxmox
21
- module ProxmoxTokenExpiration
22
- def token_expired?
23
- Fog::Proxmox.credentials_has_expired?
24
- end
20
+ module ProxmoxVmUuidHelper
21
+ UUID_REGEXP = /(?<cluster_id>\d+)[_](?<vmid>\d+)/.freeze
22
+ def extract(uuid, name)
23
+ captures_h = uuid ? UUID_REGEXP.match(uuid.to_s) : { cluster_id: '', vmid: '' }
24
+ captures_h ? captures_h[name] : ''
25
+ end
26
+
27
+ def match_uuid?(uuid)
28
+ extract(uuid, :cluster_id) != ''
29
+ end
25
30
 
26
- def token_deadline
27
- Fog::Proxmox.credentials[:deadline]
28
- end
31
+ def extract_vmid(uuid)
32
+ extract(uuid, :vmid)
29
33
  end
30
34
  end
@@ -0,0 +1,105 @@
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 ProxmoxVmVolumesHelper
27
+ include ProxmoxVmCdromHelper
28
+ include ProxmoxVmCloudinitHelper
29
+ KILO = 1024
30
+ MEGA = KILO * KILO
31
+ GIGA = KILO * MEGA
32
+
33
+ def add_disk_options(disk, args)
34
+ options = ForemanFogProxmox::HashCollection.new_hash_reject_keys(args, ['id', 'volid', 'controller', 'device', 'storage', 'size', '_delete', 'storage_type'])
35
+ ForemanFogProxmox::HashCollection.remove_empty_values(options)
36
+ disk[:options] = options
37
+ end
38
+
39
+ def parsed_typed_volumes(args, type, parsed_vm)
40
+ logger.debug(format(_('parsed_typed_volumes(%<type>s): args=%<args>s'), args: args, type: type))
41
+ volumes_attributes = args['volumes_attributes']
42
+ volumes_attributes ||= args['config_attributes']['volumes_attributes'] unless ForemanFogProxmox::Value.empty?(args['config_attributes'])
43
+ volumes_attributes ||= args['vm_attrs']['volumes_attributes'] unless ForemanFogProxmox::Value.empty?(args['vm_attrs'])
44
+ volumes = parse_typed_volumes(volumes_attributes, type)
45
+ volumes.each { |volume| parsed_vm = parsed_vm.merge(volume) }
46
+ parsed_vm
47
+ end
48
+
49
+ def parse_hard_disk_volume(args)
50
+ disk = {}
51
+ disk[:id] = args['id'] if args.key?('id')
52
+ disk[:volid] = args['volid'] if args.key?('volid')
53
+ disk[:storage] = args['storage'].to_s if args.key?('storage')
54
+ disk[:size] = args['size'].to_i if args.key?('size')
55
+ add_disk_options(disk, args)
56
+ disk.key?(:storage) ? disk : {}
57
+ end
58
+
59
+ def volume_type?(args, type)
60
+ if args.key?('storage_type')
61
+ args['storage_type'] == type
62
+ else
63
+ Fog::Proxmox::DiskHelper.cloud_init?(args['volid']) if type == 'cloud_init'
64
+ Fog::Proxmox::DiskHelper.cdrom?(args['volid']) if type == 'cdrom'
65
+ Fog::Proxmox::DiskHelper.disk?(args['id']) if ['hard_disk', 'rootfs', 'mp'].include?(type)
66
+ end
67
+ end
68
+
69
+ def parse_typed_volume(args, type)
70
+ logger.debug(format(_('parse_typed_volume(%<type>s): args=%<args>s'), args: args, type: type))
71
+ disk = parse_hard_disk_volume(args) if volume_type?(args, 'hard_disk') || volume_type?(args, 'mp') || volume_type?(args, 'rootfs')
72
+ disk = parse_server_cloudinit(args) if volume_type?(args, 'cloud_init')
73
+ disk = parse_server_cdrom(args) if volume_type?(args, 'cdrom')
74
+ logger.debug(format(_('parse_typed_volume(%<type>s): disk=%<disk>s'), disk: disk, type: type))
75
+ Fog::Proxmox::DiskHelper.flatten(disk) unless disk.empty?
76
+ end
77
+
78
+ def add_typed_volume(volumes, value, type)
79
+ volume = parse_typed_volume(value, type)
80
+ volumes.push(volume) unless ForemanFogProxmox::Value.empty?(volume)
81
+ end
82
+
83
+ def parse_typed_volumes(args, type)
84
+ logger.debug(format(_('parse_typed_volumes(%<type>s): args=%<args>s'), args: args, type: type))
85
+ volumes = []
86
+ args&.each_value { |value| add_typed_volume(volumes, value, type) }
87
+ volumes
88
+ end
89
+
90
+ def convert_volumes_size(args)
91
+ args['volumes_attributes'].each_value { |value| value['size'] = (value['size'].to_i / GIGA).to_s unless ForemanFogProxmox::Value.empty?(value['size']) }
92
+ end
93
+
94
+ def convert_sizes(args)
95
+ convert_memory_size(args['config_attributes'], 'memory')
96
+ convert_memory_size(args['config_attributes'], 'balloon')
97
+ convert_memory_size(args['config_attributes'], 'shares')
98
+ convert_memory_size(args['config_attributes'], 'swap')
99
+ args['volumes_attributes'].each_value { |value| value['size'] = (value['size'].to_i / GIGA).to_s unless ForemanFogProxmox::Value.empty?(value['size']) }
100
+ end
101
+
102
+ def remove_volume_keys(args)
103
+ args['volumes_attributes'].each_value { |volume_attributes| ForemanFogProxmox::HashCollection.remove_keys(volume_attributes, ['_delete']) } if args.key?('volumes_attributes')
104
+ end
105
+ end
@@ -21,8 +21,23 @@ module FogExtensions
21
21
  module Proxmox
22
22
  module Disk
23
23
  extend ActiveSupport::Concern
24
- def templated?
25
- volid ? volid.match(/^([\w-]+)[:]base-(\d+)-disk-(\d+)/) : false
24
+
25
+ attr_accessor :ciuser, :cipassword, :searchdomain, :nameserver, :sshkeys
26
+
27
+ def storage_type
28
+ return 'cdrom' if cdrom?
29
+
30
+ cloud_init? ? 'cloud_init' : 'hard_disk'
31
+ end
32
+
33
+ def cdrom
34
+ return 'none' unless cdrom? || volid.nil?
35
+
36
+ ['none', 'cdrom'].include?(volid) ? volid : 'image'
37
+ end
38
+
39
+ def cloudinit
40
+ cloud_init? ? 'disk' : 'none'
26
41
  end
27
42
  end
28
43
  end
@@ -23,17 +23,32 @@ module FogExtensions
23
23
  module Proxmox
24
24
  module Interface
25
25
  extend ActiveSupport::Concern
26
- attr_accessor :dhcpv4, :dhcpv6, :gwv4, :gwv6
27
26
  def mac
28
27
  macaddr
29
28
  end
30
29
 
31
- def cidrv4_prefix
30
+ def dhcp
31
+ ip == 'dhcp'
32
+ end
33
+
34
+ def dhcp6
35
+ ip6 == 'dhcp'
36
+ end
37
+
38
+ def dhcp=(value)
39
+ @dhcp = value
40
+ end
41
+
42
+ def dhcp6=(value)
43
+ @dhcp6 = value
44
+ end
45
+
46
+ def cidr
32
47
  Fog::Proxmox::IpHelper.prefix(ip) if ip
33
48
  end
34
49
 
35
- def cidrv6_prefix
36
- Fog::Proxmox::IpHelper.prefix6(ip) if ip
50
+ def cidr6
51
+ Fog::Proxmox::IpHelper.prefix6(ip6) if ip6
37
52
  end
38
53
  end
39
54
  end
@@ -23,6 +23,10 @@ module FogExtensions
23
23
  extend ActiveSupport::Concern
24
24
  attr_accessor :image_id, :templated, :ostemplate_storage, :ostemplate_file, :password, :start_after_create
25
25
 
26
+ def unique_cluster_identity
27
+ compute_resource.name + '_' + identity
28
+ end
29
+
26
30
  def start
27
31
  action('start')
28
32
  end
@@ -55,7 +59,7 @@ module FogExtensions
55
59
  delegate :description, to: :config
56
60
 
57
61
  def vm_description
58
- "Name=#{name}, vmid=#{vmid}"
62
+ format(_('Type %<type>s, node %<node>s, %<cpus>s CPUs and %<memory>s MB memory'), type: type, node: node_id, cpus: config.cores || '0', memory: config.memory / (1024 * 1024) || '0')
59
63
  end
60
64
 
61
65
  def select_nic(fog_nics, nic)
@@ -69,7 +73,7 @@ module FogExtensions
69
73
  end
70
74
 
71
75
  def volumes
72
- config.disks.reject(&:cdrom?)
76
+ config.disks
73
77
  end
74
78
 
75
79
  def disks
@@ -78,6 +82,7 @@ module FogExtensions
78
82
 
79
83
  delegate :vga, to: :config
80
84
  delegate :pool, to: :config
85
+ delegate :cloud_init?, to: :config
81
86
 
82
87
  def interfaces_attributes=(attrs); end
83
88
 
@@ -86,7 +91,11 @@ module FogExtensions
86
91
  def config_attributes=(attrs); end
87
92
 
88
93
  def templated?
89
- volumes.any?(&:templated?)
94
+ volumes.any?(&:template?)
95
+ end
96
+
97
+ def cdrom?
98
+ volumes.any?(&:cdrom?)
90
99
  end
91
100
  end
92
101
  end
@@ -24,38 +24,12 @@ module FogExtensions
24
24
  module ServerConfig
25
25
  extend ActiveSupport::Concern
26
26
  def cpu_type
27
- Fog::Proxmox::CpuHelper.extract_type(cpu)
27
+ Fog::Proxmox::CpuHelper.extract_cputype(cpu)
28
28
  end
29
29
 
30
- def spectre
31
- Fog::Proxmox::CpuHelper.has_spectre?(cpu)
32
- end
33
-
34
- def pcid
35
- Fog::Proxmox::CpuHelper.has_pcid?(cpu)
36
- end
37
-
38
- def cdrom
39
- if disks.cdrom
40
- ['none', 'cdrom'].include?(disks.cdrom.volid) ? disks.cdrom.volid : 'image'
41
- else
42
- 'none'
43
- end
44
- end
45
-
46
- def cdrom_storage
47
- disks.cdrom ? disks.cdrom.storage : ''
48
- end
49
-
50
- def cdrom_iso
51
- disks.cdrom ? disks.cdrom.volid : ''
52
- end
53
-
54
- def cdrom_image
55
- if disks.cdrom
56
- ['none', 'cdrom'].include?(disks.cdrom.volid) ? disks.cdrom.volid : 'image'
57
- else
58
- 'none'
30
+ Fog::Proxmox::CpuHelper.flags.each do |flag_key, flag_value|
31
+ define_method(flag_key) do
32
+ Fog::Proxmox::CpuHelper.flag_value(cpu, flag_value)
59
33
  end
60
34
  end
61
35
 
@@ -66,6 +40,10 @@ module FogExtensions
66
40
  def rootfs_file
67
41
  disks.rootfs&.volid
68
42
  end
43
+
44
+ def cloud_init?
45
+ disks.any?(&:cloud_init?)
46
+ end
69
47
  end
70
48
  end
71
49
  end
@@ -35,8 +35,12 @@ module HostExt
35
35
  end
36
36
  end
37
37
 
38
- def cidr_ip(interface_attributes)
39
- Fog::Proxmox::IpHelper.to_cidr(interface_attributes['ip'], interface_attributes['compute_attributes']['cidr_suffix'])
38
+ def cidr_ip(interface_attributes, v = 4)
39
+ key_ip = 'ip'
40
+ key_ip += '6' if v == 6
41
+ key_cidr = 'cidr'
42
+ key_cidr += '6' if v == 6
43
+ Fog::Proxmox::IpHelper.to_cidr(interface_attributes[key_ip], interface_attributes['compute_attributes'][key_cidr])
40
44
  end
41
45
 
42
46
  def add_interface_to_compute_attributes(index, interface_attributes, compute_attributes)
@@ -45,6 +49,7 @@ module HostExt
45
49
  compute_attributes[index].store('_delete', interface_attributes['_destroy'])
46
50
  compute_attributes[index].store('macaddr', interface_attributes['mac'])
47
51
  compute_attributes[index].store('ip', cidr_ip(interface_attributes))
52
+ compute_attributes[index].store('ip6', cidr_ip(interface_attributes, 6))
48
53
  compute_attributes[index].merge!(interface_attributes['compute_attributes'].reject { |k, _v| k == 'id' })
49
54
  end
50
55
  end
@@ -25,6 +25,7 @@ module Orchestration
25
25
  def setComputeUpdate
26
26
  logger.info "Update Proxmox Compute instance for #{name}"
27
27
  final_compute_attributes = compute_attributes.merge(compute_resource.host_compute_attrs(self))
28
+ logger.debug("setComputeUpdate: final_compute_attributes=#{final_compute_attributes}")
28
29
  compute_resource.save_vm uuid, final_compute_attributes
29
30
  rescue StandardError => e
30
31
  failure format(_('Failed to update a compute %<compute_resource>s instance %<name>s: %<e>s'), :compute_resource => compute_resource, :name => name, :e => e), e
@@ -37,6 +38,54 @@ module Orchestration
37
38
  rescue StandardError => e
38
39
  failure format(_('Failed to undo update compute %<compute_resource>s instance %<name>s: %<e>s'), :compute_resource => compute_resource, :name => name, :e => e), e
39
40
  end
41
+
42
+ def empty_provided_ips?(ip, ip6)
43
+ ip.blank? && ip6.blank? && (compute_provides?(:ip) || compute_provides?(:ip6))
44
+ end
45
+
46
+ def ips_keys
47
+ [:ip, :ip6]
48
+ end
49
+
50
+ def computeIp(foreman_attr, fog_attr)
51
+ vm.send(fog_attr) || find_address(foreman_attr)
52
+ end
53
+
54
+ def computeValue(foreman_attr, fog_attr)
55
+ value = ''
56
+ value += compute_resource.id.to_s + '_' if foreman_attr == :uuid
57
+ value += vm.send(fog_attr)
58
+ value
59
+ end
60
+
61
+ def setVmDetails
62
+ attrs = compute_resource.provided_attributes
63
+ result = true
64
+ attrs.each do |foreman_attr, fog_attr|
65
+ if foreman_attr == :mac
66
+ result = false unless match_macs_to_nics(fog_attr)
67
+ elsif ips_keys.include?(foreman_attr)
68
+ value = computeIp(foreman_attr, fog_attr)
69
+ send("#{foreman_attr}=", value)
70
+ result = false if send(foreman_attr).present? && !validate_foreman_attr(value, ::Nic::Base, foreman_attr)
71
+ else
72
+ value = computeValue(foreman_attr, fog_attr)
73
+ send("#{foreman_attr}=", value)
74
+ result = false unless validate_required_foreman_attr(value, Host, foreman_attr)
75
+ end
76
+ end
77
+ return failure(format(_('Failed to acquire IP addresses from compute resource for %<name>s'), name: name)) if empty_provided_ips?(ip, ip6)
78
+
79
+ result
80
+ end
81
+
82
+ def setComputeDetails
83
+ if vm
84
+ setVmDetails
85
+ else
86
+ failure format(_('failed to save %<name>s'), name: name)
87
+ end
88
+ end
40
89
  end
41
90
  end
42
91
  end
@@ -24,10 +24,7 @@ require 'foreman_fog_proxmox/value'
24
24
  module ForemanFogProxmox
25
25
  class Proxmox < ComputeResource
26
26
  include ProxmoxVmHelper
27
- include ProxmoxServerHelper
28
- include ProxmoxContainerHelper
29
27
  include ProxmoxConnection
30
- include ProxmoxTokenExpiration
31
28
  include ProxmoxVmNew
32
29
  include ProxmoxVmCommands
33
30
  include ProxmoxVmQueries
@@ -39,9 +36,11 @@ module ForemanFogProxmox
39
36
  include ProxmoxVersion
40
37
  include ProxmoxConsole
41
38
  validates :url, :format => { :with => URI::DEFAULT_PARSER.make_regexp }, :presence => true
39
+ validates :auth_method, :presence => true, inclusion: { in: ['access_ticket', 'user_token'], message: ->(value) do format('%<value>s is not a valid authentication method', { value: value }) end }
42
40
  validates :user, :format => { :with => /(\w+)[@]{1}(\w+)/ }, :presence => true
43
- validates :password, :presence => true
44
- before_create :test_connection
41
+ validates :password, :presence => true, if: :access_ticket?
42
+ validates :token_id, :presence => true, if: :user_token?
43
+ validates :token, :presence => true, if: :user_token?
45
44
 
46
45
  def provided_attributes
47
46
  super.merge(
@@ -95,35 +94,79 @@ module ForemanFogProxmox
95
94
  attrs[:ssl_verify_peer] = value
96
95
  end
97
96
 
98
- def renew
99
- attrs[:renew].blank? ? false : Foreman::Cast.to_bool(attrs[:renew])
97
+ def auth_method
98
+ attrs[:auth_method] || 'access_ticket'
100
99
  end
101
100
 
102
- def renew=(value)
103
- attrs[:renew] = value
101
+ def auth_method=(value)
102
+ attrs[:auth_method] = value
103
+ end
104
+
105
+ def token_id
106
+ attrs[:token_id]
107
+ end
108
+
109
+ def token_id=(value)
110
+ attrs[:token_id] = value
111
+ end
112
+
113
+ def token
114
+ attrs[:token]
115
+ end
116
+
117
+ def token=(value)
118
+ attrs[:token] = value
104
119
  end
105
120
 
106
121
  private
107
122
 
123
+ def fog_credentials
124
+ hash = {
125
+ proxmox_url: url,
126
+ proxmox_auth_method: auth_method || 'access_ticket',
127
+ connection_options: connection_options
128
+ }
129
+ if access_ticket?
130
+ hash[:proxmox_username] = user
131
+ hash[:proxmox_password] = password
132
+ end
133
+ if user_token?
134
+ hash[:proxmox_userid] = user
135
+ hash[:proxmox_token] = token
136
+ hash[:proxmox_tokenid] = token_id
137
+ end
138
+ hash
139
+ end
140
+
141
+ def token_expired?(e)
142
+ e.response.reason_phrase == 'token expired'
143
+ end
144
+
108
145
  def client
109
146
  @client ||= ::Fog::Proxmox::Compute.new(fog_credentials)
147
+ rescue Excon::Errors::Unauthorized => e
148
+ raise ::Foreman::Exception, 'User token expired' if token_expired?(e)
110
149
  rescue StandardError => e
111
- logger.error(e)
112
- raise ::Foreman::Exception, format(N_('Failed retrieving proxmox compute client caused by %<e>s'), e: e)
150
+ logger.warn(format(_('failed to create compute client: %<e>s'), e: e))
151
+ raise e
113
152
  end
114
153
 
115
154
  def identity_client
116
155
  @identity_client ||= ::Fog::Proxmox::Identity.new(fog_credentials)
156
+ rescue Excon::Errors::Unauthorized => e
157
+ raise ::Foreman::Exception, 'User token expired' if token_expired?(e)
117
158
  rescue StandardError => e
118
- logger.error(e)
119
- raise ::Foreman::Exception, format(N_('Failed retrieving proxmox identity client caused by %<e>s'), e: e)
159
+ logger.warn(format(_('failed to create identity client: %<e>s'), e: e))
160
+ raise e
120
161
  end
121
162
 
122
163
  def network_client
123
164
  @network_client ||= ::Fog::Proxmox::Network.new(fog_credentials)
165
+ rescue Excon::Errors::Unauthorized => e
166
+ raise ::Foreman::Exception, 'User token expired' if token_expired?(e)
124
167
  rescue StandardError => e
125
- logger.error(e)
126
- raise ::Foreman::Exception, format(N_('Failed retrieving proxmox network client caused by %<e>s'), e: e)
168
+ logger.warn(format(_('failed to create network client: %<e>s'), e: e))
169
+ raise e
127
170
  end
128
171
 
129
172
  def host