forked from Katello/katello
/
host_collections_controller.rb
225 lines (194 loc) · 9.54 KB
/
host_collections_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
126
127
128
129
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
module Katello
class Api::V2::HostCollectionsController < Api::V2::ApiController
before_filter :find_host_collection, :only => [:copy, :show, :update, :destroy, :destroy_systems,
:add_systems, :remove_systems, :systems,
:add_activation_keys, :remove_activation_keys]
before_filter :find_activation_key
before_filter :find_system
before_filter :find_optional_organization, :only => [:index]
before_filter :find_organization, :only => [:create]
before_filter :load_search_service, :only => [:index, :systems]
wrap_parameters :include => (HostCollection.attribute_names + %w(system_ids))
def_param_group :host_collection do
param :name, String, :required => true, :desc => N_("Host Collection name")
param :system_ids, Array, :required => false, :desc => N_("List of system uuids to be in the host collection")
param :description, String
param :max_content_hosts, Integer, :desc => N_("Maximum number of content hosts in the host collection")
end
api :GET, "/host_collections/:id", N_("Show a host collection")
param :id, :identifier, :desc => N_("Id of the host collection"), :required => true
def show
respond
end
api :GET, "/host_collections", N_("List host collections")
api :GET, "/organizations/:organization_id/host_collections"
api :GET, "/activation_keys/:activation_key_id/host_collections"
api :GET, "/systems/:system_id/host_collections"
param_group :search, Api::V2::ApiController
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
param :name, String, :desc => N_("host collection name to filter by")
param :activation_key_id, :identifier, :desc => N_("activation key identifier")
param :system_id, :identifier, :desc => N_("system identifier")
def index
subscriptions = if @system
filter_system
elsif @activation_key
filter_activation_key
else
filter_organization
end
respond_for_index(:collection => subscriptions)
end
api :POST, "/host_collections", N_("Create a host collection")
api :POST, "/organizations/:organization_id/host_collections", N_("Create a host collection")
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
param :system_uuids, Array, :required => false, :desc => N_("List of system uuids to replace the content hosts in host collection")
param_group :host_collection
def create
@host_collection = HostCollection.new(host_collection_params_with_system_uuids)
@host_collection.organization = @organization
@host_collection.save!
respond
end
api :PUT, "/host_collections/:id", N_("Update a host collection")
param :id, :identifier, :desc => N_("Id of the host collection"), :required => true
param :system_uuids, Array, :required => false, :desc => N_("List of system uuids to be in the host collection")
param_group :host_collection
def update
@host_collection.update_attributes!(host_collection_params_with_system_uuids)
respond
end
# TODO: switch to systems controller index w/ @adprice pull-request
api :GET, "/host_collections/:id/systems", N_("List content hosts in the host collection")
param :id, :identifier, :desc => N_("Id of the host collection"), :required => true
def systems
options = {
:filters => [{:term => {:host_collection_ids => @host_collection.id }}],
:load_records? => true
}
respond_for_index(:collection => item_search(System, params, options))
end
api :PUT, "/host_collections/:id/add_systems", N_("Add systems to the host collection")
param :id, :identifier, :desc => N_("Id of the host collection"), :required => true
param :system_ids, Array, :desc => N_("Array of system ids")
def add_systems
ids = System.uuids_to_ids(params[:system_ids])
@systems = System.editable.where(:id => ids)
@editable_systems = @systems.editable
@host_collection.system_ids = (@host_collection.system_ids + @editable_systems.collect { |s| s.id }).uniq
@host_collection.save!
System.index.refresh
messages = format_bulk_action_messages(
:success => _("Successfully added %s Content Host(s)."),
:error => _("You were not allowed to add %s"),
:models => @systems,
:authorized => @editable_systems
)
respond_for_show :template => 'bulk_action', :resource_name => 'common',
:resource => { 'displayMessages' => messages }
end
api :PUT, "/host_collections/:id/remove_systems", N_("Remove systems from the host collection")
param :id, :identifier, :desc => N_("Id of the host collection"), :required => true
param :system_ids, Array, :desc => N_("Array of system ids")
def remove_systems
ids = System.uuids_to_ids(params[:system_ids])
@systems = System.editable.where(:id => ids)
@editable_systems = @systems.editable
@host_collection.system_ids = (@host_collection.system_ids - @editable_systems.collect { |s| s.id }).uniq
@host_collection.save!
System.index.refresh
messages = format_bulk_action_messages(
:success => _("Successfully removed %s Content Host(s)."),
:error => _("You were not allowed to sync %s"),
:models => @systems,
:authorized => @editable_systems
)
respond_for_show :template => 'bulk_action', :resource_name => 'common',
:resource => { 'displayMessages' => messages }
end
api :DELETE, "/host_collections/:id", N_("Destroy a host collection")
param :id, :identifier, :desc => N_("Id of the host collection"), :required => true
def destroy
@host_collection.destroy
respond_for_destroy
end
api :POST, "/host_collections/:id/copy", N_("Make copy of a host collection")
param :id, :identifier, :desc => N_("ID of the host collection"), :required => true
param :name, String, :required => true, :desc => N_("New host collection name")
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_content_hosts = @host_collection.max_content_hosts
new_host_collection.systems = @host_collection.systems
new_host_collection.save!
respond_for_create :resource => new_host_collection
end
private
def filter_system
filters = [:terms => {:id => @system.host_collections.pluck("#{Katello::HostCollection.table_name}.id")}]
options = {
:filters => filters,
:load_records? => true
}
item_search(HostCollection, params, options)
end
def filter_activation_key
filters = [:terms => {:id => @activation_key.host_collections.pluck("#{Katello::HostCollection.table_name}.id")}]
options = {
:filters => filters,
:load_records? => true
}
item_search(HostCollection, params, options)
end
def filter_organization
find_organization
ids = HostCollection.readable.where(:organization_id => @organization.id).pluck("#{Katello::HostCollection.table_name}.id")
filters = [:terms => {:id => ids}]
filters << {:term => {:name => params[:name].downcase}} if params[:name]
options = {
:filters => filters,
:load_records? => true
}
item_search(HostCollection, params, options)
end
def find_host_collection
@organization = @system.organization if @system
@organization = @activation_key.organization if @activation_key
@host_collection = HostCollection.where(:id => params[:id]).first
fail HttpErrors::NotFound, _("Couldn't find host collection '%s'") % params[:id] if @host_collection.nil?
end
def host_collection_params
attrs = [:name, :description, :max_content_hosts, { :system_ids => [] }]
params.fetch(:host_collection).permit(*attrs)
end
def host_collection_params_with_system_uuids
result = host_collection_params
if params['system_uuids']
systems_from_uuid = System.uuids_to_ids(params['system_uuids'])
result['system_ids'] = result['system_ids'] ? result['system_ids'] + systems_from_uuid : systems_from_uuid
end
result
end
def find_system
@system = System.find_by_uuid!(params[:system_id]) if params[:system_id]
@organization = @system.organization if @system
end
def find_activation_key
@activation_key = ActivationKey.find_by_id!(params[:activation_key_id]) if params[:activation_key_id]
@organization = @activation_key.organization if @activation_key
end
end
end