Class: Katello::Api::V2::HostsBulkActionsController

Inherits:
ApiController
  • Object
show all
Includes:
Api::V2::BulkHostsExtension, Foreman::Renderer::Scope::Macros::Base, Concerns::Api::V2::ContentOverridesController, Concerns::Api::V2::MultiCVParamsHandling, ContentSourceHelper
Defined in:
app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb

Overview

this is Katello’s host bulk actions controller, not to be confused with Foreman’s rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods included from ContentSourceHelper

#configure_subman, #missing_content_source, #prepare_ssl_cert, #reconfigure_yggdrasild

Methods inherited from ApiController

#empty_search_query?, #full_result_response, #resource_class, #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

#applicable_errataObject



95
96
97
98
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 95

def applicable_errata
  respond_for_index(:collection => scoped_search(Katello::Erratum.applicable_to_hosts(@hosts), 'updated', 'desc',
                                                 :resource_class => Erratum))
end

#assign_content_view_environmentsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 150

def assign_content_view_environments
  # Filter to only hosts with content facets (registered with subscription-manager)
  registered_hosts = @hosts.select(&:content_facet)
  unregistered_count = @hosts.count - registered_hosts.count

  # The content_view_environments= setter handles reprioritization and Candlepin updates
  registered_hosts.each do |host|
    host.content_facet.content_view_environments = @content_view_environments
    host.save!
  end

  # Build response message
  success_message = n_("Updated content view environments for %{count} host",
                       "Updated content view environments for %{count} hosts",
                       registered_hosts.count) % { :count => registered_hosts.count }

  response = { :displayMessage => success_message }

  if unregistered_count > 0
    skipped_message = n_("Skipped %{count} unregistered host",
                         "Skipped %{count} unregistered hosts",
                         unregistered_count) % { :count => unregistered_count }
    response[:warningMessage] = skipped_message
  end

  render :json => response
end

#available_incremental_updatesObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 222

def available_incremental_updates
  fail HttpErrors::BadRequest, _("errata_ids is a required parameter") if params[:errata_ids].empty?
  version_environments = {}
  content_facets = Katello::Host::ContentFacet.with_non_installable_errata(@errata, @hosts)

  ContentViewEnvironment.for_content_facets(content_facets).each do |cve|
    version = cve.content_view_version
    version_environment = version_environments[version] || {:content_view_version => version, :environments => []}
    version_environment[:environments] << cve.environment unless version_environment[:environments].include?(cve.environment)
    version_environment[:next_version] ||= version.next_incremental_version
    version_environment[:content_host_count] ||= 0
    version_environment[:content_host_count] += content_facets.with_content_view_environments(cve).count

    if version.content_view.composite?
      version_environment[:components] = version.components_needing_errata(@errata)
    else
      version_environment[:components] = nil
    end
    version_environments[version] = version_environment
  end

  response = version_environments.values.map { |version| OpenStruct.new(version) }
  respond_for_index :collection => response, :template => :available_incremental_updates
end

#bulk_add_host_collectionsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 44

def bulk_add_host_collections
  unless params[:host_collection_ids].blank?
    display_messages = []

    @host_collections.each do |host_collection|
      pre_host_collection_count = host_collection.host_ids.count
      host_collection.host_ids =  (host_collection.host_ids + @hosts.map(&:id)).uniq
      host_collection.save!

      final_count = host_collection.host_ids.count - pre_host_collection_count
      msg = if final_count == 0
              _("All selected hosts were already members of host collection %{host_collection}.") %
                          {:host_collection => host_collection.name }
            else
              _("Added %{count} host(s) to host collection %{host_collection}.") %
                          {:count => final_count, :host_collection => host_collection.name }
            end
      display_messages << msg
    end
  end

  respond_for_show :template => 'bulk_action', :resource_name => 'common',
                   :resource => { 'displayMessages' => display_messages }
end

#bulk_remove_host_collectionsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 73

def bulk_remove_host_collections
  display_messages = []

  unless params[:host_collection_ids].blank?
    @host_collections.each do |host_collection|
      pre_host_collection_count = host_collection.host_ids.count
      host_collection.host_ids =  (host_collection.host_ids - @hosts.map(&:id)).uniq
      host_collection.save!

      final_count = pre_host_collection_count - host_collection.host_ids.count
      display_messages << _("Removed %{count} host(s) from host collection %{host_collection}.") %
          {:count => final_count, :host_collection => host_collection.name }
    end
  end

  respond_for_show :template => 'bulk_action', :resource_name => 'common',
                   :resource => { 'displayMessages' => display_messages }
end

#change_content_sourceObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 264

def change_content_source
  hosts = ::Host.where(id: params[:host_ids])
  throw_resource_not_found(name: 'host', id: params[:host_ids]) unless hosts.any?

  organization = validate_hosts_organization(hosts)
  content_source = SmartProxy.authorized(:view_smart_proxies).find(params[:content_source_id])

  # Ensure at least one parameter is provided
  if params[:content_view_environments].blank? && params[:content_view_environment_ids].blank?
    fail HttpErrors::UnprocessableEntity, _("Either content_view_environments or content_view_environment_ids must be provided")
  end

  content_view_environments = ::Katello::ContentViewEnvironment.fetch_content_view_environments(
    labels: params[:content_view_environments],
    ids: params[:content_view_environment_ids],
    organization: organization
  )

  if content_view_environments.blank?
    handle_errors(labels: params[:content_view_environments],
                  ids: params[:content_view_environment_ids])
  end

  # Generate template for manual host updates
  template = prepare_ssl_cert(foreman_server_ca_cert) + configure_subman(content_source) + reconfigure_yggdrasild(hosts.first)

  hosts.each do |host|
    next unless host.content_facet
    host.content_facet.content_source = content_source
    host.content_facet.content_view_environments = content_view_environments
    host.save! # Persist content_source change
  end

  render plain: template
end

#content_overridesObject



123
124
125
126
127
128
129
130
131
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 123

def content_overrides
  content_overrides = params[:content_overrides] || []
  content_override_values = content_overrides.map do |content_override_params|
    validate_content_overrides_enabled(content_override_params)
  end

  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::UpdateContentOverrides, @hosts, content_override_values, false)
  respond_for_async :resource => task
end

#destroy_hostsObject



110
111
112
113
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 110

def destroy_hosts
  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::Destroy, @hosts)
  respond_for_async :resource => task
end

#environment_content_viewObject



137
138
139
140
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 137

def environment_content_view
  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::UpdateContentView, @hosts, @view.id, @environment.id)
  respond_for_async :resource => task
end

#installable_errataObject



103
104
105
106
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 103

def installable_errata
  respond_for_index(:collection => scoped_search(Katello::Erratum.installable_for_hosts(@hosts), 'updated', 'desc',
                                                 :resource_class => Erratum))
end

#module_streamsObject



250
251
252
253
254
255
256
257
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 250

def module_streams
  options = {}
  options[:group] = [:name, :stream]
  options[:resource_class] = Katello::ModuleStream
  host_module_streams = Katello::ModuleStream.available_for_hosts(@hosts)
  respond_for_index(collection: scoped_search(host_module_streams, :name, :asc, options),
                    full_template: 'katello/api/v2/module_streams/name_streams')
end

#release_versionObject



181
182
183
184
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 181

def release_version
  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::UpdateReleaseVersion, @hosts, params["release_version"])
  respond_for_async :resource => task
end

#resolve_tracesObject



197
198
199
200
201
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 197

def resolve_traces
  result = Katello::HostTraceManager.resolve_traces(@traces)

  render json: result
end

#system_purposeObject



208
209
210
211
212
213
214
215
216
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 208

def system_purpose
  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::UpdateSystemPurpose,
    @hosts,
    params[:service_level],
    params[:purpose_role],
    params[:purpose_usage])

  respond_for_async :resource => task
end

#tracesObject



188
189
190
191
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 188

def traces
  collection = scoped_search(Katello::HostTracer.where(host_id: @hosts.pluck(:id)), 'application', 'desc', resource_class: Katello::HostTracer)
  respond_for_index(:collection => collection, :full_template => 'katello/api/v2/host_tracer/index')
end