Skip to content

Commit

Permalink
Multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Jul 27, 2022
1 parent 362dae5 commit b719dee
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 84 deletions.
66 changes: 32 additions & 34 deletions app/models/concerns/foreman/sti.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module Foreman
module STI
def self.prepended(base)
class << base
cattr_accessor :preloaded, instance_accessor: false
prepend ClassMethods
end
extend ActiveSupport::Concern

prepended do
cattr_accessor :preloaded, instance_accessor: false
end

module ClassMethods
class_methods do
# ensures that the correct STI object is created when :type is passed.
def new(*attributes, &block)
if (h = attributes.first).is_a?(Hash) && (type = h.with_indifferent_access.delete(:type)) && !type.empty?
Expand All @@ -19,6 +18,33 @@ def new(*attributes, &block)

super
end

def descendants
preload_sti unless preloaded
super
end

# Constantizes all types present in the database. There might be more on
# disk, but that does not matter in practice as far as the STI API is
# concerned.
#
# Assumes store_full_sti_class is true, the default.
def preload_sti
return [] unless base_class.connected? && base_class.table_exists?
types_in_db = base_class
.unscoped
.select(inheritance_column)
.distinct
.pluck(inheritance_column)
.compact

types_in_db.each do |type|
logger.debug("Preloading STI type #{type}")
type.constantize
end

self.preloaded = true
end
end

def save(*args)
Expand All @@ -28,33 +54,5 @@ def save(*args)
ensure
self.class.instance_variable_set("@finder_needs_type_condition", :true) if type_changed
end

def descendants
preload_sti unless preloaded
super
end

# Constantizes all types present in the database. There might be more on
# disk, but that does not matter in practice as far as the STI API is
# concerned.
#
# Assumes store_full_sti_class is true, the default.
def preload_sti
puts "#{self}: #{base_class.connected?} #{base_class.table_exists?}"
return [] unless base_class.connected? && base_class.table_exists?
types_in_db = base_class
.unscoped
.select(inheritance_column)
.distinct
.pluck(inheritance_column)
.compact

types_in_db.each do |type|
logger.debug("Preloading STI type #{type}")
type.constantize
end

self.preloaded = true
end
end
end
2 changes: 1 addition & 1 deletion app/models/subnet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def external_ipam?
end

def external_ipam_proxy(attrs = {})
@external_ipam_proxy ||= ProxyAPI::ExternalIpam.new({:url => externalipam.url}.merge(attrs)) if external_ipam?
@external_ipam_proxy ||= ProxyAPI::ExternalIPAM.new({:url => externalipam.url}.merge(attrs)) if external_ipam?
end

def ipam?
Expand Down
4 changes: 1 addition & 3 deletions app/registries/foreman/plugin/report_scanner_registry.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require_dependency File.expand_path('../../../services/report_scanner/puppet_report_scanner', __dir__)

module Foreman
class Plugin
class ReportScannerRegistry
DEFAULT_REPORT_SCANNERS = [
::Foreman::PuppetReportScanner,
::Foreman::ReportScanner::PuppetReportScanner,
].freeze

attr_accessor :report_scanners
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions app/services/foreman/report_scanner/puppet_report_scanner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Foreman
module ReportScanner
class PuppetReportScanner
class << self
def identify_origin(report_data)
'Puppet' if puppet_report?(report_data['logs'] || [])
end

def add_reporter_data(report, report_data)
# no additional data apart of origin
end

def puppet_report?(logs)
log = logs.last
log && log['log'].fetch('sources', {}).fetch('source', '') =~ /Puppet/
end
end
end
end
end
4 changes: 2 additions & 2 deletions app/services/ipam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def self.new(type, *args)
when IPAM::MODES[:none]
IPAM::None.new(*args)
when IPAM::MODES[:dhcp]
IPAM::Dhcp.new(*args)
IPAM::DHCP.new(*args)
when IPAM::MODES[:db]
IPAM::Db.new(*args)
when IPAM::MODES[:random_db]
IPAM::RandomDb.new(*args)
when IPAM::MODES[:eui64]
IPAM::Eui64.new(*args)
when IPAM::MODES[:external_ipam]
IPAM::ExternalIpam.new(*args)
IPAM::ExternalIPAM.new(*args)
else
raise ::Foreman::Exception.new(N_("Unknown IPAM type - can't continue"))
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/ipam/dhcp.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module IPAM
class Dhcp < Base
class DHCP < Base
delegate :dhcp, :dhcp_proxy, :to => :subnet
def suggest_ip
unless subnet.dhcp?
Expand Down
2 changes: 1 addition & 1 deletion app/services/ipam/external_ipam.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module IPAM
class ExternalIpam < Base
class ExternalIPAM < Base
delegate :external_ipam_proxy, :to => :subnet

def suggest_ip
Expand Down
2 changes: 1 addition & 1 deletion app/services/proxy_api/external_ipam.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'uri'

module ProxyAPI
class ExternalIpam < ProxyAPI::Resource
class ExternalIPAM < ProxyAPI::Resource
def initialize(args)
@url = args[:url] + "/ipam"
super args
Expand Down
18 changes: 0 additions & 18 deletions app/services/report_scanner/puppet_report_scanner.rb

This file was deleted.

59 changes: 39 additions & 20 deletions config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
Rails.autoloaders.each do |autoloader|
autoloader.inflector = Zeitwerk::Inflector.new
autoloader.inflector.inflect(
'ui' => 'UI',
'proxy_api' => 'ProxyAPI',
'sti' => 'STI',
'dhcp' => 'DHCP',
'dns' => 'DNS',
'tftp' => 'TFTP',
'external_ipam' => 'ExternalIPAM',
'bmc' => 'BMC',
'ui_notifications' => 'UINotifications',
'ipam' => 'IPAM',
'ssh' => 'SSH',
'ssh_provision' => 'SSHProvision',
'ssh_execution_provider' => 'SSHExecutionProvider',
'keep_current_request_id' => 'KeepCurrentRequestID'
)
end
# Rails.autoloaders.log!
Rails.autoloaders.main.inflector.inflect(
'ui' => 'UI',
'proxy_api' => 'ProxyAPI',
'sti' => 'STI',
'dhcp' => 'DHCP',
'dns' => 'DNS',
'tftp' => 'TFTP',
'external_ipam' => 'ExternalIPAM',
'bmc' => 'BMC',
'ui_notifications' => 'UINotifications',
'ipam' => 'IPAM',
'ssh' => 'SSH',
'ssh_provision' => 'SSHProvision',
'ssh_execution_provider' => 'SSHExecutionProvider',
'keep_current_request_id' => 'KeepCurrentRequestID',
'ec2' => 'EC2',
'aws' => 'AWS',
'gce' => 'GCE',
'aix' => 'AIX',
'nxos' => 'NXOS',
'vrp' => 'VRP',
'sso' => 'SSO',
'puppet_ca_certificate' => 'PuppetCACertificate',
'url_resolver' => 'URLResolver',
'ztp_record' => 'ZTPRecord',
'aaaa_record' => 'AAAARecord',
'ptr4_record' => 'PTR4Record',
'ptr6_record' => 'PTR6Record'
)

Rails.autoloaders.main.ignore(
Rails.root.join('lib/core_extensions.rb'),
Rails.root.join('lib/generators')
)
Rails.autoloaders.once.ignore(
Rails.root.join('app/registries/foreman/access_permissions.rb'),
Rails.root.join('app/registries/foreman/settings.rb'),
Rails.root.join('app/registries/foreman/settings')
)
2 changes: 1 addition & 1 deletion test/models/subnet/external_ipam_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class Subnet::ExternalIpamTest < ActiveSupport::TestCase
class Subnet::ExternalIPAMTest < ActiveSupport::TestCase
test 'external ipam is supported for IPv4' do
subnet = FactoryBot.build(:subnet_ipv4)
assert subnet.supports_ipam_mode?(:external_ipam)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ipam_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IPAMTest < ActiveSupport::TestCase
fake_proxy = mock("dhcp_proxy")
fake_proxy.stubs(:unused_ip => {'ip' => '192.168.1.25'})
subnet.stubs(:dhcp_proxy => fake_proxy)
ipam = IPAM::Dhcp.new(:subnet => subnet, :mac => '00:11:22:33:44:55')
ipam = IPAM::DHCP.new(:subnet => subnet, :mac => '00:11:22:33:44:55')
assert_equal '192.168.1.25', ipam.suggest_ip
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/unit/report_scanner/puppet_report_scanner_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'

class PuppetReportScannerTest < ActiveSupport::TestCase
subject { Foreman::PuppetReportScanner }
subject { Foreman::ReportScanner::PuppetReportScanner }

describe '.identify_origin' do
it 'returns Puppet if puppet_report' do
Expand Down

0 comments on commit b719dee

Please sign in to comment.