Skip to content

Instantly share code, notes, and snippets.

@CalvinHartwell
Created January 14, 2016 14:56
Show Gist options
  • Save CalvinHartwell/c9990f7dc52cdb1687ac to your computer and use it in GitHub Desktop.
Save CalvinHartwell/c9990f7dc52cdb1687ac to your computer and use it in GitHub Desktop.
Showing 5 changed files
app/helpers/concerns/foreman_openscap/hosts_helper_extensions.rb
Wrap text
@@ -12,7 +12,10 @@ module ForemanOpenscap
end
def name_column_with_scap(record)
- record.nil? ? _('Host is deleted') : name_column_without_scap(record)
+ return _('Host is deleted') if record.nil?
+ return content_tag(:span, s_("Unlinked|U"), {:rel => "twipsy", :class => "label label-light label-warning", :"data-original-title" => _('Unlinked')}) +
+ link_to(" #{record.name}", "/content_hosts/#{record.uuid}/info") if defined?(::Katello::System) && record.is_a?(::Katello::System)
+ return name_column_without_scap(record)
end
end
end
app/models/concerns/foreman_openscap/arf_report_extensions.rb
Wrap text
@@ -15,12 +15,13 @@ module ForemanOpenscap
extend ActiveSupport::Concern
include Taxonomix
included do
- has_one :host, :through => :asset, :as => :assetable, :source => :assetable, :source_type => 'Host::Base'
+ has_one :foreman_host, :through => :asset, :as => :assetable, :source => :assetable, :source_type => 'Host::Base'
+ has_one :katello_system, :through => :asset, :as => :assetable, :source => :assetable, :source_type => 'Katello::System'
after_save :assign_locations_organizations
scope :hosts, lambda { includes(:policy, :arf_report_breakdown) }
- scope :latest, lambda { includes(:host, :policy, :arf_report_breakdown).limit(5).order("scaptimony_arf_reports.created_at DESC") }
+ scope :latest, lambda { includes(:foreman_host, :policy, :arf_report_breakdown).limit(5).order("scaptimony_arf_reports.created_at DESC") }
scoped_search :in => :host, :on => :name, :complete_value => :true, :rename => "host"
@@ -31,10 +32,20 @@ module ForemanOpenscap
}
end
+ def host
+ return foreman_host unless defined?(Katello::System)
+ foreman_host || katello_system
+ end
+
def assign_locations_organizations
- if host
- self.location_ids = [host.location_id] if SETTINGS[:locations_enabled]
- self.organization_ids = [host.organization_id] if SETTINGS[:organizations_enabled]
+ if foreman_host
+ self.location_ids = [foreman_host.location_id] if SETTINGS[:locations_enabled]
+ self.organization_ids = [foreman_host.organization_id] if SETTINGS[:organizations_enabled]
+ end
+
+ if katello_system
+ self.locations = Location.where(:name => katello_system.location) if SETTINGS[:locations_enabled]
+ self.organizations = [katello_system.organization] if SETTINGS[:organizations_enabled]
end
end
app/models/concerns/foreman_openscap/katello_system_extensions.rb 0 → 100644
Wrap text
@@ -0,0 +1,26 @@
+require 'scaptimony/asset'
+
+module ForemanOpenscap
+ module KatelloSystemExtensions
+ extend ActiveSupport::Concern
+
+ included do
+ has_one :asset, :as => :assetable, :class_name => '::Scaptimony::Asset'
+ has_many :asset_policies, :through => :asset, :class_name => '::Scaptimony::AssetPolicy'
+ has_many :policies, :through => :asset_policies, :class_name => '::Scaptimony::Policy'
+ has_many :arf_reports, :through => :asset, :class_name => '::Scaptimony::ArfReport'
+ after_save :migrate_asset_to_foreman_host
+ end
+
+ def get_asset
+ host_id ?
+ Scaptimony::Asset.where(:assetable_type => 'Host::Base', :assetable_id => host_id).first_or_create! :
+ Scaptimony::Asset.where(:assetable_type => 'Katello::System', :assetable_id => id).first_or_create!
+ end
+
+ private
+ def migrate_asset_to_foreman_host
+ asset.update_attributes(:assetable_type => 'Host::Base', :assetable_id => host_id) if host_id
+ end
+ end
+end
\ No newline at end of file
lib/foreman_openscap/engine.rb
Wrap text
@@ -77,6 +77,7 @@ module ForemanOpenscap
::Scaptimony::Asset.send(:include, ForemanOpenscap::AssetExtensions)
::Scaptimony::Policy.send(:include, ForemanOpenscap::PolicyExtensions)
::Scaptimony::ScapContent.send(:include, ForemanOpenscap::ScapContentExtensions)
+ Katello::System.send(:include, ForemanOpenscap::KatelloSystemExtensions)
end
rake_tasks do
lib/foreman_openscap/helper.rb
Wrap text
@@ -20,6 +20,7 @@ module ForemanOpenscap::Helper
def self.find_host_by_name_or_uuid(cname)
if defined?(Katello::System)
host = Host.includes(:content_host).where(:katello_systems => {:uuid => cname}).first
+ host ||= Katello::System.find_by_uuid(cname)
host ||= Host.find_by_name(cname)
else
host = Host.find_by_name(cname)
Showing 1 changed file
app/models/concerns/foreman_openscap/katello_system_extensions.rb
Wrap text
@@ -20,7 +20,7 @@ module ForemanOpenscap
private
def migrate_asset_to_foreman_host
- asset.update_attributes(:assetable_type => 'Host::Base', :assetable_id => host_id) if host_id
+ asset.update_attributes(:assetable_type => 'Host::Base', :assetable_id => host_id) if (asset && host_id)
2
0a0bebc09154d658ff79834afd87a388?s=40&d=identicon
Ohad Levy @olevy 3 months ago
why is it host::base and not managed?
4f06d97f10ac325f785a3dbd2a23c466?s=40&d=identicon
Shlomi Zadok @szadok 3 months ago · 3 months ago
Asset is calling the host base model via polymorphic relationship. (This happens because the _type column is supposed to identify the model's table and should not contain data on the model itself - just a reference.
See also: host extensions and asset
end
end
end
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment