Class: Katello::Api::V2::HostCollectionsController

Inherits:
ApiController
  • Object
show all
Includes:
Concerns::FilteredAutoCompleteSearch
Defined in:
app/controllers/katello/api/v2/host_collections_controller.rb

Constant Summary

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

#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_hostsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 95

def add_hosts
  host_ids = params[:host_ids].map(&:to_i)

  @hosts = ::Host::Managed.where(id: host_ids)
  @editable_hosts = @hosts.authorized(:edit_hosts)

  already_added_host_ids = @host_collection.host_ids & host_ids
  unfound_host_ids = host_ids - @hosts.pluck(:id)

  @host_collection.host_ids = (@host_collection.host_ids +
                               @editable_hosts.pluck(:id)).uniq
  @host_collection.save!

  messages = format_bulk_action_messages(
      :success    => _("Successfully added %s Host(s)."),
      :error      => _("You were not allowed to add %s"),
      :models     => @hosts.pluck(:id) - already_added_host_ids,
      :authorized => @editable_hosts.pluck(:id) - already_added_host_ids
  )

  already_added_host_ids.each do |host_id|
    messages[:error] << _("Host with ID %s already exists in the host collection.") % host_id
  end

  unfound_host_ids.each do |host_id|
    messages[:error] << _("Host with ID %s not found.") % host_id
  end

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

#copyObject



171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 171

def copy
  new_host_collection                           = HostCollection.new
  new_host_collection.name                      = params[:host_collection][:name]
  new_host_collection.organization              = @host_collection.organization
  new_host_collection.description               = @host_collection.description
  new_host_collection.max_hosts                 = @host_collection.max_hosts
  new_host_collection.unlimited_hosts           = @host_collection.unlimited_hosts
  new_host_collection.hosts                     = @host_collection.hosts
  new_host_collection.save!
  respond_for_create :resource => new_host_collection
end

#createObject



76
77
78
79
80
81
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 76

def create
  @host_collection = HostCollection.new(host_collection_params_with_host_ids)
  @host_collection.organization = @organization
  @host_collection.save!
  respond_for_create(:resource => @host_collection)
end

#destroyObject



163
164
165
166
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 163

def destroy
  @host_collection.destroy
  respond_for_destroy
end

#indexObject



45
46
47
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 45

def index
  respond(:collection => scoped_search(index_relation.distinct, :name, :asc))
end

#index_relationObject



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

def index_relation
  if @host
    query = @host.host_collections

    if params[:available_for] == "host"
      query = Katello::HostCollection.readable.where(:organization_id => @organization.id)

      if @host.host_collections.count > 0
        query = query.where("#{Katello::HostCollection.table_name}.id NOT IN (?)", @host.host_collection_ids)
      end
    end
  elsif @activation_key
    query = @activation_key.host_collections
  elsif @organization
    query = HostCollection.readable.where(:organization_id => @organization.id)
  else
    query = HostCollection.readable
  end
  query = query.where(:name => params[:name]) if params[:name]
  query
end

#remove_hostsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 130

def remove_hosts
  host_ids = params[:host_ids].map(&:to_i)

  @hosts = ::Host::Managed.where(id: host_ids)
  @editable_hosts = @hosts.authorized(:edit_hosts)

  already_removed_host_ids = @hosts.pluck(:id) - @host_collection.host_ids
  unfound_host_ids = host_ids - @hosts.pluck(:id)

  @host_collection.host_ids -= @editable_hosts.pluck(:id)
  @host_collection.save!

  messages = format_bulk_action_messages(
      :success    => _("Successfully removed %s Host(s)."),
      :error      => _("You were not allowed to sync %s"),
      :models     => @hosts.pluck(:id) - already_removed_host_ids,
      :authorized => @editable_hosts.pluck(:id) - already_removed_host_ids
  )

  already_removed_host_ids.each do |host_id|
    messages[:error] << _("Host with ID %s does not exist in the host collection.") % host_id
  end

  unfound_host_ids.each do |host_id|
    messages[:error] << _("Host with ID %s not found.") % host_id
  end

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

#showObject



30
31
32
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 30

def show
  respond(:resource => @host_collection)
end

#updateObject



87
88
89
90
# File 'app/controllers/katello/api/v2/host_collections_controller.rb', line 87

def update
  @host_collection.update!(host_collection_params_with_host_ids)
  respond_for_show(:resource => @host_collection)
end