foreman_netbox 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +619 -0
- data/README.md +50 -0
- data/Rakefile +50 -0
- data/app/contracts/foreman_netbox/device_contract.rb +42 -0
- data/app/contracts/foreman_netbox/params/interface_params.rb +16 -0
- data/app/contracts/foreman_netbox/params/ip_address_params.rb +15 -0
- data/app/contracts/foreman_netbox/params/tenant_params.rb +13 -0
- data/app/contracts/foreman_netbox/virtual_machine_contract.rb +29 -0
- data/app/interactors/foreman_netbox/concerns/facts.rb +11 -0
- data/app/interactors/foreman_netbox/concerns/primary_ips.rb +25 -0
- data/app/interactors/foreman_netbox/delete_host/delete_device.rb +20 -0
- data/app/interactors/foreman_netbox/delete_host/delete_virtual_machine.rb +20 -0
- data/app/interactors/foreman_netbox/delete_host/organizer.rb +12 -0
- data/app/interactors/foreman_netbox/sync_host/organizer.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/find.rb +41 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +29 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/organizer.rb +20 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +37 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/organizer.rb +23 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +46 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +39 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +38 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/organizer.rb +25 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +85 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/validate.rb +25 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/find.rb +26 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/organizer.rb +19 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/find.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +31 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb +23 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +46 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +39 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +38 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +82 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/validate.rb +25 -0
- data/app/jobs/foreman_netbox/sync_host_job.rb +19 -0
- data/app/lib/foreman_netbox/api.rb +12 -0
- data/app/models/concerns/foreman_netbox/host_extensions.rb +27 -0
- data/app/models/concerns/foreman_netbox/location_extensions.rb +13 -0
- data/app/models/concerns/foreman_netbox/nic/base_extensions.rb +23 -0
- data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +18 -0
- data/app/models/foreman_netbox/netbox_facet.rb +34 -0
- data/app/models/orchestration/netbox.rb +64 -0
- data/app/models/setting/netbox.rb +32 -0
- data/app/services/foreman_netbox/cached_netbox_parameters.rb +103 -0
- data/app/services/foreman_netbox/netbox_parameters.rb +185 -0
- data/app/services/foreman_netbox/netbox_parameters_comparator.rb +96 -0
- data/app/views/hosts/_netbox_tab.html.erb +28 -0
- data/config/routes.rb +4 -0
- data/db/migrate/20200527135812_create_netbox_facet.rb +12 -0
- data/db/migrate/20200604141933_add_sync_info_to_netbox_facet.rb +8 -0
- data/db/migrate/20200708093607_add_raw_data_to_netbox_facet.rb +7 -0
- data/lib/foreman_netbox.rb +6 -0
- data/lib/foreman_netbox/engine.rb +64 -0
- data/lib/foreman_netbox/version.rb +5 -0
- data/lib/tasks/foreman_netbox_tasks.rake +67 -0
- data/test/factories/host.rb +17 -0
- data/test/factories/netbox_facet.rb +17 -0
- data/test/fixtures/facts/k8s_physical_host.json +328 -0
- data/test/fixtures/facts/rhel_physical_host.json +512 -0
- data/test/fixtures/facts/rhel_virtual_host.json +455 -0
- data/test/fixtures/netbox_device_raw_data.json +330 -0
- data/test/fixtures/netbox_virtual_machine_raw_data.json +241 -0
- data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +81 -0
- data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +81 -0
- data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +93 -0
- data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +78 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +65 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/find_test.rb +105 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url_test.rb +39 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create_test.rb +45 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +55 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find_test.rb +63 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create_test.rb +48 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find_test.rb +105 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete_test.rb +41 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +72 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +92 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/find_test.rb +59 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +188 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +50 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +59 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +74 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/find_test.rb +77 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url_test.rb +39 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create_test.rb +50 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find_test.rb +65 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete_test.rb +37 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +73 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +88 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +171 -0
- data/test/jobs/foreman_netbox/sync_host_job_test.rb +14 -0
- data/test/models/foreman_netbox/location_test.rb +42 -0
- data/test/models/foreman_netbox/netbox_facet_test.rb +219 -0
- data/test/models/foreman_netbox/nics/base_test.rb +81 -0
- data/test/models/foreman_netbox/usergroup_test.rb +23 -0
- data/test/models/host/managed_test.rb +63 -0
- data/test/services/foreman_netbox/netbox_attributes_test.rb +278 -0
- data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +136 -0
- data/test/test_plugin_helper.rb +25 -0
- metadata +343 -0
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# ForemanNetbox
|
2
|
+
|
3
|
+
[<img src="https://opensourcelogos.aws.dmtech.cloud/dmTECH_opensource_logo.svg" height="21" width="130">](https://www.dmtech.de/)
|
4
|
+
|
5
|
+
This is a plugin for Foreman that introduces integration with [NetBox](https://netbox.readthedocs.io)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
See [How_to_Install_a_Plugin](https://theforeman.org/manuals/2.3/index.html#6.1InstallaPlugin)
|
10
|
+
for how to install Foreman plugins.
|
11
|
+
|
12
|
+
The gem name is "foreman_netbox".
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
## Integration tests
|
17
|
+
|
18
|
+
The easiest way to start Netbox is to use [netbox-docker](https://github.com/netbox-community/netbox-docker). To run integration tests provide `FOREMAN_NETBOX_URL` and `FOREMAN_NETBOX_TOKEN` variables, eg.
|
19
|
+
|
20
|
+
```
|
21
|
+
$ FOREMAN_NETBOX_URL=http://0.0.0.0:8000 FOREMAN_NETBOX_TOKEN=0123456789abcdef0123456789abcdef01234567 bundle exec rake test:foreman_netbox
|
22
|
+
```
|
23
|
+
|
24
|
+
After installation of the plugin you can find these settings as well under Administer/Settings/Netbox tab.
|
25
|
+
|
26
|
+
You will find there as well a Netbox Orchestration Switch.
|
27
|
+
|
28
|
+
If the switch is enabled every change of a Foreman Hostobject will be synced with your netbox instance.
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
Fork and send a Pull Request. Thanks!
|
33
|
+
|
34
|
+
## Copyright
|
35
|
+
|
36
|
+
Copyright (c) 2019 dmTECH GmbH, [dmtech.de](https://www.dmtech.de/)
|
37
|
+
|
38
|
+
This program is free software: you can redistribute it and/or modify
|
39
|
+
it under the terms of the GNU General Public License as published by
|
40
|
+
the Free Software Foundation, either version 3 of the License, or
|
41
|
+
(at your option) any later version.
|
42
|
+
|
43
|
+
This program is distributed in the hope that it will be useful,
|
44
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
45
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
46
|
+
GNU General Public License for more details.
|
47
|
+
|
48
|
+
You should have received a copy of the GNU General Public License
|
49
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
50
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# !/usr/bin/env rake
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'bundler/setup'
|
7
|
+
rescue LoadError
|
8
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
9
|
+
end
|
10
|
+
begin
|
11
|
+
require 'rdoc/task'
|
12
|
+
rescue LoadError
|
13
|
+
require 'rdoc/rdoc'
|
14
|
+
require 'rake/rdoctask'
|
15
|
+
RDoc::Task = Rake::RDocTask
|
16
|
+
end
|
17
|
+
|
18
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'ForemanNetbox'
|
21
|
+
rdoc.options << '--line-numbers'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
|
32
|
+
Rake::TestTask.new(:test) do |t|
|
33
|
+
t.libs << 'lib'
|
34
|
+
t.libs << 'test'
|
35
|
+
t.pattern = 'test/**/*_test.rb'
|
36
|
+
t.verbose = false
|
37
|
+
end
|
38
|
+
|
39
|
+
task default: :test
|
40
|
+
|
41
|
+
begin
|
42
|
+
require 'rubocop/rake_task'
|
43
|
+
RuboCop::RakeTask.new
|
44
|
+
rescue StandardError
|
45
|
+
puts 'Rubocop not loaded.'
|
46
|
+
end
|
47
|
+
|
48
|
+
task :default do
|
49
|
+
Rake::Task['rubocop'].execute
|
50
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
class DeviceContract < Dry::Validation::Contract
|
5
|
+
# rubocop:disable Metrics/BlockLength
|
6
|
+
params do
|
7
|
+
optional(:tenant).hash(ForemanNetbox::Params::TenantParams.new)
|
8
|
+
optional(:ip_addresses).array(ForemanNetbox::Params::IpAddressParams.new)
|
9
|
+
optional(:interfaces).array(ForemanNetbox::Params::InterfaceParams.new)
|
10
|
+
|
11
|
+
required(:device).filled(:hash) do
|
12
|
+
required(:name).filled(:string)
|
13
|
+
optional(:serial).maybe(:string)
|
14
|
+
optional(:tags).array(:string)
|
15
|
+
end
|
16
|
+
|
17
|
+
required(:device_type).filled(:hash) do
|
18
|
+
required(:model).filled(:string)
|
19
|
+
required(:slug).filled(:string)
|
20
|
+
optional(:tags).array(:string)
|
21
|
+
end
|
22
|
+
|
23
|
+
required(:device_role).filled(:hash) do
|
24
|
+
required(:name).filled(:string)
|
25
|
+
required(:slug).filled(:string)
|
26
|
+
optional(:color).maybe(:string)
|
27
|
+
end
|
28
|
+
|
29
|
+
required(:manufacturer).filled(:hash) do
|
30
|
+
required(:name).filled(:string)
|
31
|
+
required(:slug).filled(:string)
|
32
|
+
end
|
33
|
+
|
34
|
+
required(:site).filled(:hash) do
|
35
|
+
required(:name).filled(:string)
|
36
|
+
required(:slug).filled(:string)
|
37
|
+
optional(:tags).array(:string)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
# rubocop:enable Metrics/BlockLength
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module Params
|
5
|
+
class InterfaceParams < Dry::Schema::Params
|
6
|
+
define do
|
7
|
+
required(:name).filled(:string)
|
8
|
+
required(:type).filled(:hash) do
|
9
|
+
required(:value).filled(:string)
|
10
|
+
end
|
11
|
+
optional(:mac_address).maybe(:string)
|
12
|
+
optional(:tags).array(:string)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module Params
|
5
|
+
class IpAddressParams < Dry::Schema::Params
|
6
|
+
define do
|
7
|
+
required(:address).filled(:string)
|
8
|
+
required(:interface).filled(:hash) do
|
9
|
+
required(:name).filled(:string)
|
10
|
+
end
|
11
|
+
optional(:tags).array(:string)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
class VirtualMachineContract < Dry::Validation::Contract
|
5
|
+
params do
|
6
|
+
optional(:tenant).hash(ForemanNetbox::Params::TenantParams.new)
|
7
|
+
optional(:ip_addresses).array(ForemanNetbox::Params::IpAddressParams.new)
|
8
|
+
optional(:interfaces).array(ForemanNetbox::Params::InterfaceParams.new)
|
9
|
+
|
10
|
+
required(:virtual_machine).filled(:hash) do
|
11
|
+
required(:name).filled(:string)
|
12
|
+
optional(:vcpus).maybe(:integer)
|
13
|
+
optional(:memory).maybe(:integer)
|
14
|
+
optional(:disk).maybe(:integer)
|
15
|
+
optional(:tags).array(:string)
|
16
|
+
end
|
17
|
+
|
18
|
+
required(:cluster).filled(:hash) do
|
19
|
+
required(:name).filled(:string)
|
20
|
+
optional(:tags).array(:string)
|
21
|
+
end
|
22
|
+
|
23
|
+
required(:cluster_type).filled(:hash) do
|
24
|
+
required(:name).filled(:string)
|
25
|
+
required(:slug).filled(:string)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module Concerns
|
5
|
+
module PrimaryIps
|
6
|
+
def primary_ip4
|
7
|
+
return if context.host.ip.blank?
|
8
|
+
|
9
|
+
ip_addresses_map[IPAddr.new(context.host.ip).to_i]
|
10
|
+
end
|
11
|
+
|
12
|
+
def primary_ip6
|
13
|
+
return if context.host.ip6.blank?
|
14
|
+
|
15
|
+
ip_addresses_map[IPAddr.new(context.host.ip6).to_i]
|
16
|
+
end
|
17
|
+
|
18
|
+
def ip_addresses_map
|
19
|
+
@ip_addresses_map ||= context.ip_addresses.each_with_object({}) do |ip, hash|
|
20
|
+
hash[ip.address.to_i] = ip.id
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module DeleteHost
|
5
|
+
class DeleteDevice
|
6
|
+
include ::Interactor
|
7
|
+
|
8
|
+
around do |interactor|
|
9
|
+
interactor.call unless context.host.compute?
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
ForemanNetbox::API.client.dcim.devices.find_by(name: context.host.name)&.delete
|
14
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
15
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
16
|
+
context.fail!(error: "#{self.class}: #{e}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module DeleteHost
|
5
|
+
class DeleteVirtualMachine
|
6
|
+
include ::Interactor
|
7
|
+
|
8
|
+
around do |interactor|
|
9
|
+
interactor.call if context.host.compute?
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
ForemanNetbox::API.client.virtualization.virtual_machines.find_by(name: context.host.name)&.delete
|
14
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
15
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
16
|
+
context.fail!(error: "#{self.class}: #{e}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
class Organizer
|
6
|
+
include ::Interactor::Organizer
|
7
|
+
|
8
|
+
before do
|
9
|
+
context.netbox_params = context.host.netbox_facet.netbox_params
|
10
|
+
context.raw_data = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
organize SyncHost::SyncVirtualMachine::Organizer,
|
14
|
+
SyncHost::SyncDevice::Organizer
|
15
|
+
|
16
|
+
def call
|
17
|
+
super
|
18
|
+
|
19
|
+
update_status
|
20
|
+
rescue Interactor::Failure => e
|
21
|
+
update_status
|
22
|
+
raise e
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
delegate :host, :error, to: :context
|
28
|
+
delegate :netbox_facet, to: :host
|
29
|
+
|
30
|
+
def update_status
|
31
|
+
netbox_facet.update(synchronized_at: Time.zone.now, synchronization_error: error, raw_data: context.raw_data)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
class Create
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call unless context.device
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
context.device = ForemanNetbox::API.client::DCIM::Device.new(params).save
|
15
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
16
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
17
|
+
context.fail!(error: "#{self.class}: #{e}")
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
delegate :netbox_params, :device_type, :device_role, :site, to: :context
|
23
|
+
delegate :tenant, to: :context, allow_nil: true
|
24
|
+
|
25
|
+
def params
|
26
|
+
netbox_params.fetch(:device).merge(
|
27
|
+
device_type: device_type.id,
|
28
|
+
device_role: device_role.id,
|
29
|
+
site: site.id,
|
30
|
+
tenant: tenant&.id
|
31
|
+
).compact
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
class Find
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
def call
|
10
|
+
# rubocop:disable Rails/DynamicFindBy
|
11
|
+
context.device = find_by_serial || find_by_mac || find_by_name
|
12
|
+
# rubocop:enable Rails/DynamicFindBy
|
13
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
14
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
15
|
+
context.fail!(error: "#{self.class}: #{e}")
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
delegate :host, :netbox_params, to: :context
|
21
|
+
delegate :mac, to: :host, allow_nil: true
|
22
|
+
|
23
|
+
def find_by_serial
|
24
|
+
serial = netbox_params.dig(:device, :serial)
|
25
|
+
|
26
|
+
ForemanNetbox::API.client.dcim.devices.filter(serial: serial).first if serial
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_by_mac
|
30
|
+
ForemanNetbox::API.client.dcim.devices.filter(mac_address: mac).first if mac
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_by_name
|
34
|
+
name = netbox_params.dig(:device, :name)
|
35
|
+
|
36
|
+
ForemanNetbox::API.client.dcim.devices.find_by(name: name)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
class Organizer
|
7
|
+
include ::Interactor::Organizer
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call unless context.host.compute?
|
11
|
+
end
|
12
|
+
|
13
|
+
after do
|
14
|
+
context.raw_data[:device] = context.device.raw_data!
|
15
|
+
end
|
16
|
+
|
17
|
+
organize SyncDevice::Validate,
|
18
|
+
SyncHost::SyncTenant::Organizer,
|
19
|
+
SyncDevice::SyncSite::Organizer,
|
20
|
+
SyncDevice::SyncDeviceRole::Organizer,
|
21
|
+
SyncDevice::SyncDeviceType::Organizer,
|
22
|
+
SyncDevice::Find,
|
23
|
+
SyncDevice::Create,
|
24
|
+
SyncDevice::SyncInterfaces::Organizer,
|
25
|
+
SyncDevice::Update,
|
26
|
+
SyncDevice::SaveNetboxUrl
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|