Class: Katello::Api::V2::HostPackagesController

Inherits:
ApiController
  • Object
show all
Includes:
Concerns::Api::V2::RepositoryContentController, Concerns::FilteredAutoCompleteSearch
Defined in:
app/controllers/katello/api/v2/host_packages_controller.rb

Constant Summary collapse

UPGRADABLE =
"upgradable".freeze
UP_TO_DATE =
"up-to-date".freeze
VERSION_STATUSES =
[UPGRADABLE, UP_TO_DATE].freeze
PERSISTENCE_TRANSIENT =
"transient".freeze
PERSISTENCE_PERSISTENT =
"persistent".freeze
PERSISTENCE_NIL =
"nil".freeze
PERSISTENCE_TYPES =
[PERSISTENCE_TRANSIENT, PERSISTENCE_PERSISTENT, PERSISTENCE_NIL].freeze

Constants included from Concerns::FilteredAutoCompleteSearch

Concerns::FilteredAutoCompleteSearch::PAGE_SIZE

Instance Method Summary collapse

Methods included from Concerns::FilteredAutoCompleteSearch

#auto_complete_search

Methods inherited from ApiController

#empty_search_query?, #full_result_response, #scoped_search, #skip_session

Methods included from Rendering

#respond_for_async, #respond_for_bulk_async, #respond_for_create, #respond_for_destroy, #respond_for_index, #respond_for_show, #respond_for_status, #respond_for_update, #respond_with_template, #respond_with_template_collection, #respond_with_template_resource, #try_specific_collection_template, #try_specific_resource_template

Methods included from Katello::Api::Version2

#api_version

Instance Method Details

#containerfile_index_relationObject



115
116
117
118
119
120
121
# File 'app/controllers/katello/api/v2/host_packages_controller.rb', line 115

def containerfile_index_relation
  relation = @host.installed_packages.where(katello_host_installed_packages: { persistence: PERSISTENCE_TRANSIENT })
  if ::Foreman::Cast.to_bool(params[:include_unknown_persistence])
    relation = relation.or(@host.installed_packages.where(katello_host_installed_packages: { persistence: nil }))
  end
  relation
end

#containerfile_install_commandObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/katello/api/v2/host_packages_controller.rb', line 77

def containerfile_install_command
  # We need to pass the full result param to avoid pagination but retain other scoped search features.
  params[:full_result] = true
  collection = scoped_search(containerfile_index_relation, :name, :asc, :resource_class => ::Katello::InstalledPackage)
  if collection[:results].empty?
    render json: {
      command: nil,
      packageCount: 0,
    },
    status: :ok
  else
    render json: {
      command: "RUN dnf install -y #{collection[:results].map { |pkg| Shellwords.escape(pkg.nvrea) }.join(' ')}",
      packageCount: collection[:results].length,
    },
    status: :ok
  end
end

#indexObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/katello/api/v2/host_packages_controller.rb', line 51

def index
  validate_index_params!
  options = { :resource_class => ::Katello::InstalledPackage }

  # Handle persistence sorting (normal scoped_search cannot handle this join for multiple hosts)
  if params[:sort_by] == 'persistence'
    options[:custom_sort] = lambda do |query|
      query.joins(:host_installed_packages)
        .where(katello_host_installed_packages: {host_id: @host.id})
        .order("katello_host_installed_packages.persistence #{sanitize_sort_order(params[:sort_order])}")
    end
  end

  collection = scoped_search(index_relation, :name, :asc, options)
  include_upgradable = ::Foreman::Cast.to_bool(params[:include_latest_upgradable])

  # Present packages with persistence and (if requested) latest upgradable info
  collection[:results] = HostPackagePresenter.package_map(collection[:results], @host, include_upgradable, true)
  respond_for_index(:collection => collection)
end

#index_relationObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/katello/api/v2/host_packages_controller.rb', line 96

def index_relation
  packages = @host.installed_packages
  upgradable_packages = ::Katello::Rpm.installable_for_hosts([@host]).select(:name)
  if params[:status].present?
    packages = case params[:status]
               when 'up-to-date' then packages.where.not(name: upgradable_packages)
               when 'upgradable' then packages.where(name: upgradable_packages)
               end
  end
  if params[:persistence].present?
    packages = case params[:persistence]
               when PERSISTENCE_TRANSIENT then packages.where(katello_host_installed_packages: { persistence: PERSISTENCE_TRANSIENT })
               when PERSISTENCE_PERSISTENT then packages.where(katello_host_installed_packages: { persistence: PERSISTENCE_PERSISTENT })
               when PERSISTENCE_NIL then packages.where(katello_host_installed_packages: { persistence: nil })
               end
  end
  packages
end

#installed_packagesObject



32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/katello/api/v2/host_packages_controller.rb', line 32

def installed_packages
  _sort_by, _sort_order, options = sort_options
  sort_by = 'name'
  sort_order = 'asc'

  options[:select] = "DISTINCT ON (#{::Katello::InstalledPackage.table_name}.name) #{::Katello::InstalledPackage.table_name}.id, #{::Katello::InstalledPackage.table_name}.name"
  final_relation = ::Katello::InstalledPackage.all

  result = scoped_search(final_relation, sort_by, sort_order, options)
  respond_for_index(:collection => result, :template => "installed_packages")
end

#resource_classObject



123
124
125
# File 'app/controllers/katello/api/v2/host_packages_controller.rb', line 123

def resource_class
  Katello::InstalledPackage
end