foreman_netbox 1.1.1 → 2.0.0

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 (112) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -6
  3. data/app/interactors/foreman_netbox/delete_host/delete_device.rb +1 -1
  4. data/app/interactors/foreman_netbox/delete_host/delete_virtual_machine.rb +1 -1
  5. data/app/interactors/foreman_netbox/delete_host/organizer.rb +1 -1
  6. data/app/interactors/foreman_netbox/sync_host/organizer.rb +1 -1
  7. data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +1 -1
  8. data/app/interactors/foreman_netbox/sync_host/sync_device/find.rb +3 -5
  9. data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +10 -10
  10. data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +1 -1
  11. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create.rb +1 -1
  12. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find.rb +1 -1
  13. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/organizer.rb +1 -1
  14. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +1 -1
  15. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find.rb +1 -1
  16. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/organizer.rb +3 -3
  17. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create.rb +1 -1
  18. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb +4 -5
  19. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/organizer.rb +1 -1
  20. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +1 -1
  21. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find.rb +2 -2
  22. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/organizer.rb +4 -4
  23. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +2 -2
  24. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +3 -1
  25. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find.rb +2 -2
  26. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/organizer.rb +3 -3
  27. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +1 -1
  28. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/find.rb +1 -1
  29. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/organizer.rb +2 -2
  30. data/app/interactors/foreman_netbox/sync_host/sync_tags/create.rb +1 -1
  31. data/app/interactors/foreman_netbox/sync_host/sync_tags/find.rb +1 -1
  32. data/app/interactors/foreman_netbox/sync_host/sync_tags/organizer.rb +2 -2
  33. data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +1 -1
  34. data/app/interactors/foreman_netbox/sync_host/sync_tenant/find.rb +1 -1
  35. data/app/interactors/foreman_netbox/sync_host/sync_tenant/organizer.rb +2 -2
  36. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +1 -1
  37. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/find.rb +2 -4
  38. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +8 -8
  39. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +1 -1
  40. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +1 -1
  41. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find.rb +1 -1
  42. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/organizer.rb +3 -3
  43. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create.rb +1 -1
  44. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find.rb +1 -1
  45. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/organizer.rb +1 -1
  46. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +8 -8
  47. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find.rb +2 -2
  48. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb +4 -4
  49. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +2 -2
  50. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +3 -3
  51. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find.rb +2 -2
  52. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/organizer.rb +3 -3
  53. data/app/lib/foreman_netbox/api.rb +11 -2
  54. data/app/models/orchestration/netbox.rb +2 -4
  55. data/app/services/foreman_netbox/cached_netbox_parameters.rb +11 -11
  56. data/app/services/foreman_netbox/netbox_parameters.rb +29 -29
  57. data/app/services/foreman_netbox/netbox_parameters_comparator.rb +14 -8
  58. data/db/migrate/20230303095650_fix_netbox_settings_category_to_dsl.rb +0 -2
  59. data/lib/foreman_netbox/engine.rb +47 -40
  60. data/lib/foreman_netbox/version.rb +1 -1
  61. data/lib/tasks/foreman_netbox_tasks.rake +0 -50
  62. data/test/fixtures/netbox_device_raw_data.json +1 -1
  63. data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +8 -8
  64. data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +8 -8
  65. data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +8 -8
  66. data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +9 -9
  67. data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +2 -2
  68. data/test/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url_test.rb +2 -2
  69. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create_test.rb +1 -1
  70. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find_test.rb +4 -4
  71. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +1 -1
  72. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find_test.rb +4 -4
  73. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create_test.rb +1 -1
  74. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find_test.rb +6 -6
  75. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +2 -2
  76. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +3 -3
  77. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete_test.rb +2 -2
  78. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find_test.rb +4 -4
  79. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +5 -5
  80. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +7 -7
  81. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find_test.rb +3 -3
  82. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +5 -5
  83. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +10 -10
  84. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +1 -1
  85. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/find_test.rb +3 -3
  86. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +3 -3
  87. data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +11 -11
  88. data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +1 -1
  89. data/test/interactors/foreman_netbox/sync_host/sync_tenant/find_test.rb +3 -3
  90. data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +2 -2
  91. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +2 -2
  92. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url_test.rb +2 -2
  93. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +1 -1
  94. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find_test.rb +2 -2
  95. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find_test.rb +4 -4
  96. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +2 -2
  97. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +3 -3
  98. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete_test.rb +2 -2
  99. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find_test.rb +4 -4
  100. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +5 -5
  101. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +7 -7
  102. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find_test.rb +3 -3
  103. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +7 -7
  104. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +9 -9
  105. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +10 -10
  106. data/test/models/foreman_netbox/netbox_facet_test.rb +1 -1
  107. data/test/models/foreman_netbox/nics/base_test.rb +1 -1
  108. data/test/services/foreman_netbox/netbox_attributes_test.rb +16 -16
  109. data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +31 -31
  110. data/test/test_plugin_helper.rb +1 -1
  111. data/test/unit/foreman_netbox/api_test.rb +52 -0
  112. metadata +88 -165
@@ -17,14 +17,14 @@ module ForemanNetbox
17
17
  .select { |i| i[:name] }
18
18
  .reject { |i| interfaces.map(&:name).include?(i[:name]) }
19
19
  .map do |new_interface|
20
- ForemanNetbox::API.client::Virtualization::Interface.new(
21
- new_interface.except(:type)
22
- .merge(
23
- virtual_machine: virtual_machine.id,
24
- tags: default_tag_ids
25
- )
26
- ).save
27
- end
20
+ ForemanNetbox::Api.client::Virtualization::Interface.new(
21
+ new_interface.except(:type)
22
+ .merge(
23
+ virtual_machine: virtual_machine.id,
24
+ tags: default_tag_ids
25
+ )
26
+ ).save
27
+ end
28
28
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
29
29
  ::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
30
30
  context.fail!(error: "#{self.class}: #{e}")
@@ -8,7 +8,7 @@ module ForemanNetbox
8
8
  include ::Interactor
9
9
 
10
10
  def call
11
- context.interfaces = ForemanNetbox::API.client.virtualization.interfaces.filter(params)
11
+ context.interfaces = ForemanNetbox::Api.client.virtualization.interfaces.filter(params)
12
12
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
13
13
  ::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
14
14
  context.fail!(error: "#{self.class}: #{e}")
@@ -20,7 +20,7 @@ module ForemanNetbox
20
20
 
21
21
  def params
22
22
  {
23
- virtual_machine_id: virtual_machine.id
23
+ virtual_machine_id: virtual_machine.id,
24
24
  }
25
25
  end
26
26
  end
@@ -12,10 +12,10 @@ module ForemanNetbox
12
12
  end
13
13
 
14
14
  organize SyncInterfaces::Find,
15
- SyncInterfaces::Delete,
16
- SyncInterfaces::Create,
17
- SyncInterfaces::SyncIpAddresses::Organizer,
18
- SyncInterfaces::Update
15
+ SyncInterfaces::Delete,
16
+ SyncInterfaces::Create,
17
+ SyncInterfaces::SyncIpAddresses::Organizer,
18
+ SyncInterfaces::Update
19
19
  end
20
20
  end
21
21
  end
@@ -14,14 +14,14 @@ module ForemanNetbox
14
14
  interface_id = interfaces_map.fetch(ip_address.dig(:interface, :name), nil)
15
15
 
16
16
  next unless interface_id
17
- next unless ForemanNetbox::API.client
17
+ next unless ForemanNetbox::Api.client
18
18
  .ipam
19
19
  .ip_addresses
20
20
  .filter(vminterface_id: interface_id, address: ip_address[:address])
21
21
  .total
22
22
  .zero?
23
23
 
24
- ForemanNetbox::API.client::IPAM::IpAddress.new(
24
+ ForemanNetbox::Api.client::IPAM::IpAddress.new(
25
25
  ip_address.slice(:address)
26
26
  .merge(
27
27
  assigned_object_type: 'virtualization.vminterface', assigned_object_id: interface_id,
@@ -21,8 +21,9 @@ module ForemanNetbox
21
21
  ip_addresses_netbox_params = netbox_params.fetch(:ip_addresses, [])
22
22
 
23
23
  context.interfaces.each do |netbox_interface|
24
- host_interface_ips = ip_addresses_netbox_params.select { |ip| ip.dig(:interface, :name) == netbox_interface.name }
25
- .map { |ip| ip.fetch(:address) }
24
+ host_interface_ips = ip_addresses_netbox_params.select do |ip|
25
+ ip.dig(:interface, :name) == netbox_interface.name
26
+ end.map { |ip| ip.fetch(:address) }
26
27
 
27
28
  context.ip_addresses
28
29
  .select { |ip| ip['assigned_object_type'] == 'virtualization.vminterface' && ip['assigned_object_id'] == netbox_interface.id }
@@ -30,7 +31,6 @@ module ForemanNetbox
30
31
  .each(&:delete)
31
32
  end
32
33
  end
33
-
34
34
  delegate :netbox_params, to: :context
35
35
  end
36
36
  end
@@ -9,7 +9,7 @@ module ForemanNetbox
9
9
  include ::Interactor
10
10
 
11
11
  def call
12
- context.ip_addresses = ForemanNetbox::API.client.ipam.ip_addresses.filter(params)
12
+ context.ip_addresses = ForemanNetbox::Api.client.ipam.ip_addresses.filter(params)
13
13
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
14
14
  ::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
15
15
  context.fail!(error: "#{self.class}: #{e}")
@@ -17,7 +17,7 @@ module ForemanNetbox
17
17
 
18
18
  def params
19
19
  {
20
- virtual_machine_id: context.virtual_machine.id
20
+ virtual_machine_id: context.virtual_machine.id,
21
21
  }
22
22
  end
23
23
  end
@@ -13,9 +13,9 @@ module ForemanNetbox
13
13
  end
14
14
 
15
15
  organize SyncIpAddresses::Find,
16
- SyncIpAddresses::Delete,
17
- SyncIpAddresses::Update,
18
- SyncIpAddresses::Create
16
+ SyncIpAddresses::Delete,
17
+ SyncIpAddresses::Update,
18
+ SyncIpAddresses::Create
19
19
  end
20
20
  end
21
21
  end
@@ -1,12 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanNetbox
4
- class API
4
+ class Api
5
5
  def self.client
6
6
  NetboxClientRuby.configure do |config|
7
- config.netbox.api_base_url = "#{Setting['netbox_url']}/api"
7
+ config.netbox.api_base_url = netbox_api_url
8
8
  config.netbox.auth.token = Setting['netbox_api_token']
9
9
  end
10
10
  end
11
+
12
+ def self.netbox_api_url
13
+ return Setting['netbox_api_url'] if URI.parse(Setting['netbox_api_url']).is_a?(URI::HTTP)
14
+
15
+ api_url = "#{Setting['netbox_url']}/api"
16
+ return api_url if URI.parse(api_url).is_a?(URI::HTTP)
17
+
18
+ raise Foreman::Exception, 'Invalid Netbox API URL, please check the netbox_url and netbox_api_url settings'
19
+ end
11
20
  end
12
21
  end
@@ -23,8 +23,7 @@ module Orchestration
23
23
  ::Foreman::Logging.logger('foreman_netbox/import')
24
24
  .info("Queued import of #{name} to Netbox. Changes that will be sent: #{netbox_params_diff}")
25
25
 
26
- post_queue.create(name: _('Push host %s to Netbox') % self, priority: 100,
27
- action: [self, :set_netbox])
26
+ post_queue.create(name: _('Push host %s to Netbox') % self, priority: 100, action: [self, :set_netbox])
28
27
  end
29
28
 
30
29
  def queue_netbox_destroy
@@ -33,8 +32,7 @@ module Orchestration
33
32
  ::Foreman::Logging.logger('foreman_netbox/import')
34
33
  .info("Queued delete of #{name} from Netbox.")
35
34
 
36
- queue.create(name: _('Delete host %s from Netbox') % self, priority: 60,
37
- action: [self, :del_netbox])
35
+ queue.create(name: _('Delete host %s from Netbox') % self, priority: 60, action: [self, :del_netbox])
38
36
  end
39
37
 
40
38
  def set_netbox
@@ -25,7 +25,7 @@ module ForemanNetbox
25
25
  attr_accessor :result
26
26
 
27
27
  def read_interfaces
28
- return unless data.keys.include?(:interfaces)
28
+ return unless data.key?(:interfaces)
29
29
 
30
30
  result[:interfaces] = data.fetch(:interfaces, {}).fetch(:results, []).map do |interface|
31
31
  interface.slice(:name, :mac_address)
@@ -35,7 +35,7 @@ module ForemanNetbox
35
35
  end
36
36
 
37
37
  def read_ip_addresses
38
- return unless data.keys.include?(:ip_addresses)
38
+ return unless data.key?(:ip_addresses)
39
39
 
40
40
  result[:ip_addresses] = data.fetch(:ip_addresses, {}).fetch(:results, []).map do |ip_address|
41
41
  ip_address.slice(:address)
@@ -44,31 +44,31 @@ module ForemanNetbox
44
44
  end
45
45
 
46
46
  def read_virtual_machine
47
- return unless data.keys.include?(:virtual_machine)
47
+ return unless data.key?(:virtual_machine)
48
48
 
49
49
  result[:virtual_machine] = data.fetch(:virtual_machine, {}).slice(:name, :vcpus, :memory, :disk)
50
50
  end
51
51
 
52
52
  def read_tenant
53
- return unless data.keys.include?(:tenant)
53
+ return unless data.key?(:tenant)
54
54
 
55
55
  result[:tenant] = data.fetch(:tenant, {}).slice(:name, :slug)
56
56
  end
57
57
 
58
58
  def read_cluster
59
- return unless data.keys.include?(:cluster)
59
+ return unless data.key?(:cluster)
60
60
 
61
61
  result[:cluster] = data.fetch(:cluster, {}).slice(:name)
62
62
  end
63
63
 
64
64
  def read_cluster_type
65
- return unless data.keys.include?(:cluster_type)
65
+ return unless data.key?(:cluster_type)
66
66
 
67
67
  result[:cluster_type] = data.fetch(:cluster_type, {}).slice(:name, :slug)
68
68
  end
69
69
 
70
70
  def read_device
71
- return unless data.keys.include?(:device)
71
+ return unless data.key?(:device)
72
72
 
73
73
  result[:device] = data.fetch(:device, {})
74
74
  .slice(:name, :serial)
@@ -77,25 +77,25 @@ module ForemanNetbox
77
77
  end
78
78
 
79
79
  def read_device_role
80
- return unless data.keys.include?(:device_role)
80
+ return unless data.key?(:device_role)
81
81
 
82
82
  result[:device_role] = data.fetch(:device_role, {}).slice(:name, :color, :slug)
83
83
  end
84
84
 
85
85
  def read_device_type
86
- return unless data.keys.include?(:device_type)
86
+ return unless data.key?(:device_type)
87
87
 
88
88
  result[:device_type] = data.fetch(:device_type, {}).slice(:model, :slug)
89
89
  end
90
90
 
91
91
  def read_manufacturer
92
- return unless data.keys.include?(:manufacturer)
92
+ return unless data.key?(:manufacturer)
93
93
 
94
94
  result[:manufacturer] = data.fetch(:manufacturer, {}).slice(:name, :slug)
95
95
  end
96
96
 
97
97
  def read_site
98
- return unless data.keys.include?(:site)
98
+ return unless data.key?(:site)
99
99
 
100
100
  result[:site] = data.fetch(:site, {}).slice(:name, :slug)
101
101
  end
@@ -6,15 +6,15 @@ module ForemanNetbox
6
6
  UNKNOWN = 'Unknown'
7
7
  DEFAULT_INTERFACE_TYPE = 'virtual'
8
8
  CLUSTER_TYPES = {
9
- :'Foreman::Model::Vmware' => {
9
+ 'Foreman::Model::Vmware': {
10
10
  name: 'VMware ESXi',
11
- slug: 'vmware-esxi'
12
- }
11
+ slug: 'vmware-esxi',
12
+ },
13
13
  }.freeze
14
14
  DEVICE_ROLE = {
15
15
  name: 'SERVER',
16
16
  color: '9e9e9e',
17
- slug: 'server'
17
+ slug: 'server',
18
18
  }.freeze
19
19
 
20
20
  def self.call(host)
@@ -46,7 +46,7 @@ module ForemanNetbox
46
46
  vcpus: compute_object&.cpus,
47
47
  memory: compute_object&.memory_mb,
48
48
  disk: compute_object&.volumes&.map(&:size_gb)&.reduce(&:+),
49
- cluster: compute_object&.cluster
49
+ cluster: compute_object&.cluster,
50
50
  }.compact
51
51
  end
52
52
  end
@@ -55,8 +55,8 @@ module ForemanNetbox
55
55
  {
56
56
  tenant: {
57
57
  name: host.owner&.netbox_tenant_name,
58
- slug: host.owner&.netbox_tenant_slug
59
- }
58
+ slug: host.owner&.netbox_tenant_slug,
59
+ },
60
60
  }
61
61
  end
62
62
 
@@ -64,14 +64,14 @@ module ForemanNetbox
64
64
  {
65
65
  device: {
66
66
  name: host.name,
67
- serial: host.facts.deep_symbolize_keys[:serialnumber]
68
- }
67
+ serial: host.facts.deep_symbolize_keys[:serialnumber],
68
+ },
69
69
  }
70
70
  end
71
71
 
72
72
  def device_role
73
73
  {
74
- device_role: DEVICE_ROLE
74
+ device_role: DEVICE_ROLE,
75
75
  }
76
76
  end
77
77
 
@@ -81,8 +81,8 @@ module ForemanNetbox
81
81
  {
82
82
  device_type: {
83
83
  model: model,
84
- slug: model.parameterize
85
- }
84
+ slug: model.parameterize,
85
+ },
86
86
  }
87
87
  end
88
88
 
@@ -92,8 +92,8 @@ module ForemanNetbox
92
92
  {
93
93
  manufacturer: {
94
94
  name: name,
95
- slug: cached_netbox_params.dig(:manufacturer, :slug) || name.parameterize
96
- }
95
+ slug: cached_netbox_params.dig(:manufacturer, :slug) || name.parameterize,
96
+ },
97
97
  }
98
98
  end
99
99
 
@@ -101,8 +101,8 @@ module ForemanNetbox
101
101
  {
102
102
  site: {
103
103
  name: host.location&.netbox_site_name,
104
- slug: host.location&.netbox_site_slug
105
- }
104
+ slug: host.location&.netbox_site_slug,
105
+ },
106
106
  }
107
107
  end
108
108
 
@@ -116,20 +116,20 @@ module ForemanNetbox
116
116
  name: identifier || (mac_address && "Interface #{mac_address}"),
117
117
  mac_address: mac_address,
118
118
  type: {
119
- value: DEFAULT_INTERFACE_TYPE
120
- }
119
+ value: DEFAULT_INTERFACE_TYPE,
120
+ },
121
121
  }
122
- end
122
+ end,
123
123
  }
124
124
  end
125
125
 
126
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
126
+ # rubocop:disable Metrics/CyclomaticComplexity
127
127
  def ip_addresses
128
128
  {
129
129
  ip_addresses: host.interfaces.map do |interface|
130
130
  [
131
131
  interface.ip && interface.subnet && "#{interface.ip}/#{interface.subnet.cidr}",
132
- interface.ip6 && interface.subnet6 && "#{interface.ip6}/#{interface.subnet6.cidr}"
132
+ interface.ip6 && interface.subnet6 && "#{interface.ip6}/#{interface.subnet6.cidr}",
133
133
  ].compact.map do |ip_address|
134
134
  identifier = interface.identifier.present? && interface.identifier
135
135
  mac_address = interface.mac&.upcase
@@ -137,14 +137,14 @@ module ForemanNetbox
137
137
  {
138
138
  address: ip_address,
139
139
  interface: {
140
- name: identifier || (mac_address && "Interface #{mac_address}")
141
- }
140
+ name: identifier || (mac_address && "Interface #{mac_address}"),
141
+ },
142
142
  }
143
143
  end
144
- end.flatten
144
+ end.flatten,
145
145
  }
146
146
  end
147
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
147
+ # rubocop:enable Metrics/CyclomaticComplexity
148
148
 
149
149
  def virtual_machine
150
150
  {
@@ -152,15 +152,15 @@ module ForemanNetbox
152
152
  :vcpus, :memory, :disk
153
153
  ).merge(
154
154
  name: host.name
155
- )
155
+ ),
156
156
  }
157
157
  end
158
158
 
159
159
  def cluster
160
160
  {
161
161
  cluster: {
162
- name: netbox_compute_attributes[:cluster]
163
- }.compact
162
+ name: netbox_compute_attributes[:cluster],
163
+ }.compact,
164
164
  }
165
165
  end
166
166
 
@@ -168,7 +168,7 @@ module ForemanNetbox
168
168
  type = host.compute_resource&.type&.to_sym
169
169
 
170
170
  {
171
- cluster_type: CLUSTER_TYPES.fetch(type, nil)
171
+ cluster_type: CLUSTER_TYPES.fetch(type, nil),
172
172
  }
173
173
  end
174
174
  end
@@ -32,22 +32,27 @@ module ForemanNetbox
32
32
  def keys_diff
33
33
  result = {}
34
34
 
35
- result[:added] = added_keys.each_with_object({}) { |key, memo| memo[key] = new_hash[key] } if added_keys.any?
36
- result[:removed] = removed_keys.each_with_object({}) { |key, memo| memo[key] = old_hash[key] } if removed_keys.any?
35
+ result[:added] = added_keys.index_with { |key| new_hash[key] } if added_keys.any?
36
+ if removed_keys.any?
37
+ result[:removed] = removed_keys.index_with do |key|
38
+ old_hash[key]
39
+ end
40
+ end
37
41
 
38
42
  result
39
43
  end
40
44
 
41
45
  def diff_old
42
46
  old_hash.except(*removed_keys).each_with_object({}) do |(key, old_value), memo|
43
- if old_value.is_a?(Hash)
47
+ case old_value
48
+ when Hash
44
49
  new_value = new_hash.fetch(key, {})
45
50
  diff = ForemanNetbox::NetboxParametersComparator.call(old_value, new_value)
46
51
 
47
52
  next unless diff.keys.any?
48
53
 
49
54
  memo[key] = diff
50
- elsif old_value.is_a?(Array)
55
+ when Array
51
56
  next if new_hash[key]
52
57
 
53
58
  memo[key] = { added: [], removed: old_value }
@@ -61,10 +66,11 @@ module ForemanNetbox
61
66
  end.compact
62
67
  end
63
68
 
64
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
69
+ # rubocop:disable Metrics/CyclomaticComplexity
65
70
  def diff_new
66
71
  new_hash.except(*added_keys).each_with_object({}) do |(key, new_value), memo|
67
- if new_value.is_a?(Hash)
72
+ case new_value
73
+ when Hash
68
74
  old_value = old_hash.fetch(key, {})
69
75
 
70
76
  next if old_value == new_value
@@ -74,7 +80,7 @@ module ForemanNetbox
74
80
  next unless diff.keys.any?
75
81
 
76
82
  memo[key] = diff
77
- elsif new_value.is_a?(Array)
83
+ when Array
78
84
  old_value = old_hash.fetch(key, [])
79
85
  added = new_value.reject { |item| old_value.find { |x| x == item } }
80
86
  removed = old_value.reject { |item| new_value.find { |x| x == item } }
@@ -91,6 +97,6 @@ module ForemanNetbox
91
97
  end
92
98
  end.compact
93
99
  end
94
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
100
+ # rubocop:enable Metrics/CyclomaticComplexity
95
101
  end
96
102
  end
@@ -2,8 +2,6 @@
2
2
 
3
3
  class FixNetboxSettingsCategoryToDsl < ActiveRecord::Migration[6.0]
4
4
  def up
5
- # rubocop:disable Rails/SkipsModelValidations
6
5
  Setting.where(category: 'Setting::Netbox').update_all(category: 'Setting') if column_exists?(:settings, :category)
7
- # rubocop:enable Rails/SkipsModelValidations
8
6
  end
9
7
  end
@@ -8,8 +8,6 @@ module ForemanNetbox
8
8
  class Engine < ::Rails::Engine
9
9
  engine_name 'foreman_netbox'
10
10
 
11
- config.autoload_paths += Dir["#{config.root}/app/interactors"]
12
-
13
11
  # Add any db migrations
14
12
  initializer 'foreman_netbox.load_app_instance_data' do |app|
15
13
  ForemanNetbox::Engine.paths['db/migrate'].existent.each do |path|
@@ -17,51 +15,60 @@ module ForemanNetbox
17
15
  end
18
16
  end
19
17
 
20
- initializer 'foreman_netbox.register_plugin', :before => :finisher_hook do |_app|
21
- Foreman::Plugin.register :foreman_netbox do
22
- requires_foreman '>= 3.1'
18
+ # rubocop:disable Metrics/BlockLength
19
+ initializer 'foreman_netbox.register_plugin', before: :finisher_hook do |app|
20
+ app.reloader.to_prepare do
21
+ Foreman::Plugin.register :foreman_netbox do
22
+ requires_foreman '>= 3.13'
23
23
 
24
- # Netbox Facet
25
- register_facet(ForemanNetbox::NetboxFacet, :netbox_facet) do
26
- set_dependent_action :destroy
27
- end
24
+ # Netbox Facet
25
+ register_facet(ForemanNetbox::NetboxFacet, :netbox_facet) do
26
+ set_dependent_action :destroy
27
+ end
28
28
 
29
- # extend host show page
30
- extend_page('hosts/show') do |context|
31
- context.add_pagelet :main_tabs,
32
- name: N_('Netbox'),
33
- partial: 'hosts/netbox_tab',
34
- onlyif: proc { |host| host.netbox_facet.synchronized_at }
35
- end
29
+ # extend host show page
30
+ extend_page('hosts/show') do |context|
31
+ context.add_pagelet :main_tabs,
32
+ name: N_('Netbox'),
33
+ partial: 'hosts/netbox_tab',
34
+ onlyif: proc { |host| host.netbox_facet.synchronized_at }
35
+ end
36
36
 
37
- settings do
38
- category(:netbox, N_('Netbox')) do
39
- setting 'netbox_url',
40
- type: :string,
41
- default: '-',
42
- full_name: N_('Netbox URL'),
43
- description: N_('URL where Netbox is reachable')
44
- setting 'netbox_api_token',
45
- type: :string,
46
- default: '-',
47
- full_name: N_('Netbox API token'),
48
- description: N_('API token to Netbox')
49
- setting 'netbox_orchestration_enabled',
50
- type: :boolean,
51
- default: false,
52
- full_name: N_('Netbox Orchestration'),
53
- description: N_('Enable Netbox Orchestration')
54
- setting 'netbox_skip_site_update',
55
- type: :boolean,
56
- default: false,
57
- full_name: N_('Skip Site Update'),
58
- description: N_('Skip updating Site attribute for Devices')
37
+ settings do
38
+ category(:netbox, N_('Netbox')) do
39
+ setting 'netbox_url',
40
+ type: :string,
41
+ default: '-',
42
+ full_name: N_('Netbox UI URL'),
43
+ description: N_('URL where Netbox UI is reachable')
44
+ setting 'netbox_api_url',
45
+ type: :string,
46
+ default: '-',
47
+ full_name: N_('Netbox API URL'),
48
+ description: N_('URL where Netbox API is reachable, in case it is different than Netbox UI')
49
+ setting 'netbox_api_token',
50
+ type: :string,
51
+ default: '-',
52
+ full_name: N_('Netbox API token'),
53
+ description: N_('API token to Netbox')
54
+ setting 'netbox_orchestration_enabled',
55
+ type: :boolean,
56
+ default: false,
57
+ full_name: N_('Netbox Orchestration'),
58
+ description: N_('Enable Netbox Orchestration')
59
+ setting 'netbox_skip_site_update',
60
+ type: :boolean,
61
+ default: false,
62
+ full_name: N_('Skip Site Update'),
63
+ description: N_('Skip updating Site attribute for Devices')
64
+ end
59
65
  end
60
- end
61
66
 
62
- logger :import, enabled: true
67
+ logger :import, enabled: true
68
+ end
63
69
  end
64
70
  end
71
+ # rubocop:enable Metrics/BlockLength
65
72
 
66
73
  config.to_prepare do
67
74
  ::Host::Managed.include(ForemanNetbox::HostExtensions)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanNetbox
4
- VERSION = '1.1.1'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -2,31 +2,6 @@
2
2
 
3
3
  require 'rake/testtask'
4
4
 
5
- namespace :foreman_netbox do
6
- namespace :sync do
7
- desc 'Example Task'
8
- task full: :environment do
9
- User.as_anonymous_admin do
10
- Host::Managed.where(managed: true).each do |host|
11
- unless host.netbox_facet.netbox_params_diff.empty?
12
- puts "Nothing to push for #{host.name}"
13
- next
14
- end
15
-
16
- puts "Pushing #{host.name} to Netbox"
17
- result = host.push_to_netbox
18
-
19
- if result.success?
20
- puts "#{host.name} pushed"
21
- else
22
- puts "Failed to push #{host.name}: #{result.error}"
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
29
-
30
5
  # Tests
31
6
  namespace :test do
32
7
  desc 'Test ForemanNetbox'
@@ -39,29 +14,4 @@ namespace :test do
39
14
  end
40
15
  end
41
16
 
42
- namespace :foreman_netbox do
43
- task rubocop: :environment do
44
- begin
45
- require 'rubocop/rake_task'
46
- RuboCop::RakeTask.new(:rubocop_foreman_netbox) do |task|
47
- task.patterns = ["#{ForemanNetbox::Engine.root}/app/**/*.rb",
48
- "#{ForemanNetbox::Engine.root}/lib/**/*.rb",
49
- "#{ForemanNetbox::Engine.root}/test/**/*.rb"]
50
- end
51
- rescue StandardError
52
- puts 'Rubocop not loaded.'
53
- end
54
-
55
- Rake::Task['rubocop_foreman_netbox'].invoke
56
- end
57
- end
58
-
59
17
  Rake::Task[:test].enhance ['test:foreman_netbox']
60
-
61
- load 'tasks/jenkins.rake'
62
-
63
- # rubocop:disable Style/IfUnlessModifier
64
- if Rake::Task.task_defined?(:'jenkins:unit')
65
- Rake::Task['jenkins:unit'].enhance ['test:foreman_netbox', 'foreman_netbox:rubocop']
66
- end
67
- # rubocop:enable Style/IfUnlessModifier