Sha256: d18599726e4f063dab6fd58a0ee52084148fa0fdb4ed62010e82b2b24e785050

Contents?: true

Size: 1.89 KB

Versions: 23

Compression:

Stored size: 1.89 KB

Contents

module ForemanInventoryUpload
  module Generators
    class Tags
      def initialize(host)
        @host = host
      end

      def generate
        (
          locations +
          hostgroups +
          host_collections +
          organizations +
          content_data +
          satellite_server_data
        ).reject { |key, value| value.empty? }.map { |key, value| [key, truncated_value(value)] }
      end

      def generate_parameters
        return [] unless Setting[:include_parameter_tags]

        (@host.host_params || {})
          .select { |_name, value| value.present? || value.is_a?(FalseClass) }
          .map { |key, value| [key, truncated_value(value)] }
      end

      private

      def locations
        return [] unless @host.location
        @host.location.title.split('/').map { |item| ['location', item] }.push(['location', @host.location.title])
      end

      def hostgroups
        return [] unless @host.hostgroup
        @host.hostgroup.title.split('/').map { |item| ['hostgroup', item] }.push(['hostgroup', @host.hostgroup.title])
      end

      def host_collections
        (@host.host_collections || []).map { |item| ['host collection', item.name] }
      end

      def organizations
        [['organization', @host.organization.name]]
      end

      def content_data
        (@host.lifecycle_environments.uniq || []).map { |item| ['lifecycle_environment', item.name] } +
        (@host.activation_keys || []).map { |item| ['activation_key', item.name] } +
        (@host.content_views || []).map { |item| ['content_view', item.name] }
      end

      def satellite_server_data
        [
          ['satellite_instance_id', Foreman.instance_id],
          ['organization_id', @host.organization_id.to_s],
        ]
      end

      def truncated_value(value)
        return 'Original value exceeds 250 characters' if value.to_s.length > 250

        value
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
foreman_rh_cloud-10.0.3 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-11.0.3 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.59 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-11.0.2 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-11.0.1 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-11.0.0 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.58 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-10.0.2 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.57 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-10.0.1 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.56 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.55 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.54 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.53 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.52 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-8.0.52 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-9.0.51 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-8.0.51 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-8.0.50 lib/foreman_inventory_upload/generators/tags.rb
foreman_rh_cloud-8.0.49 lib/foreman_inventory_upload/generators/tags.rb