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

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

PARAM_ACTIONS =
{
  :install_content => {
    :package => ::Actions::Katello::Host::Package::Install,
    :package_group => ::Actions::Katello::Host::PackageGroup::Install,
    :errata => :install_errata
  },
  :update_content => {
    :package => ::Actions::Katello::Host::Package::Update,
    :package_group => ::Actions::Katello::Host::PackageGroup::Install
  },
  :remove_content => {
    :package => ::Actions::Katello::Host::Package::Remove,
    :package_group => ::Actions::Katello::Host::PackageGroup::Remove
  }
}.with_indifferent_access

Instance Method Summary collapse

Methods included from ContentSourceHelper

#configure_subman, #missing_content_source, #prepare_ssl_cert, #reconfigure_yggdrasild

Methods inherited from ApiController

#check_katello_agent_not_disabled, #deprecate_katello_agent, #empty_search_query?, #full_result_response, #katello_agent_removal_release, #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

#add_subscriptionsObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 181

def add_subscriptions
  if @organization.simple_content_access?
    fail HttpErrors::BadRequest, _("The specified organization is in Simple Content Access mode. Attaching subscriptions is disabled")
  end

  pools_with_quantities = params.require(:subscriptions).map do |sub_params|
    PoolWithQuantities.new(Pool.find(sub_params['id']), sub_params['quantity'])
  end

  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::AttachSubscriptions, @hosts, pools_with_quantities)
  respond_for_async :resource => task
end

#applicable_errataObject



107
108
109
110
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 107

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

#auto_attachObject



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

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

#available_incremental_updatesObject



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
299
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 273

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.in_content_views_and_environments(
      content_views: [cve.content_view],
      lifecycle_environments: [cve.environment]
    ).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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 62

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
      display_messages << _("Successfully added %{count} content host(s) to 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

#bulk_remove_host_collectionsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 85

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 << _("Successfully removed %{count} content 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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 318

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

  lifecycle_environment = KTEnvironment.readable.find(params[:environment_id])
  content_view = Katello::ContentView.readable.find(params[:content_view_id])
  content_source = SmartProxy.authorized(:view_smart_proxies).find(params[:content_source_id])
  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.assign_single_environment(
      :content_view_id => content_view.id,
      :environment_id => lifecycle_environment.id
    )
    host.content_facet.content_source = content_source

    host.update_candlepin_associations
  end

  render plain: template
end

#content_overridesObject



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

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



153
154
155
156
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 153

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

#environment_content_viewObject



223
224
225
226
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 223

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

#install_contentObject



126
127
128
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 126

def install_content
  content_action
end

#installable_errataObject



115
116
117
118
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 115

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

#module_streamsObject



304
305
306
307
308
309
310
311
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 304

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),
                    template: '../../../api/v2/module_streams/name_streams')
end

#release_versionObject



231
232
233
234
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 231

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

#remove_contentObject



147
148
149
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 147

def remove_content
  content_action
end

#remove_subscriptionsObject



164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 164

def remove_subscriptions
  #combine the quantities for duplicate pools into PoolWithQuantities objects
  pool_id_quantities = params.require(:subscriptions).inject({}) do |new_hash, subscription|
    new_hash[subscription['id']] ||= PoolWithQuantities.new(Pool.find(subscription['id']))
    new_hash[subscription['id']].quantities << subscription['quantity']
    new_hash
  end
  task = async_task(::Actions::BulkAction, ::Actions::Katello::Host::RemoveSubscriptions, @hosts, pool_id_quantities.values)
  respond_for_async :resource => task
end

#resolve_tracesObject



246
247
248
249
250
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 246

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

  render json: result
end

#system_purposeObject



258
259
260
261
262
263
264
265
266
267
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 258

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

  respond_for_async :resource => task
end

#tracesObject



238
239
240
241
# File 'app/controllers/katello/api/v2/hosts_bulk_actions_controller.rb', line 238

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

#update_contentObject



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

def update_content
  content_action
end