forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 3
/
host_collections_controller.rb
290 lines (254 loc) · 12.9 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
#
# 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::V1::HostCollectionsController < Api::V1::ApiController
before_filter :find_host_collection, :only => [:copy, :show, :update, :destroy, :destroy_content_hosts,
:add_content_hosts, :remove_content_hosts, :content_hosts, :history,
:history_show, :update_content_hosts]
before_filter :find_organization, :only => [:index, :create, :copy]
before_filter :authorize
after_filter :refresh_index, :only => [:create, :update]
def rules
any_readable = lambda { @organization && HostCollection.any_readable?(@organization) }
read_perm = lambda { @host_collection.readable? }
edit_perm = lambda { @host_collection.editable? }
create_perm = lambda { HostCollection.creatable?(@organization) }
destroy_perm = lambda { @host_collection.deletable? }
destroy_content_hosts_perm = lambda { @host_collection.content_hosts_deletable? }
{ :index => any_readable,
:show => read_perm,
:content_hosts => read_perm,
:create => create_perm,
:copy => create_perm,
:update => edit_perm,
:destroy => destroy_perm,
:destroy_content_hosts => destroy_content_hosts_perm,
:add_content_hosts => edit_perm,
:remove_content_hosts => edit_perm,
:history => read_perm,
:history_show => read_perm,
:update_content_hosts => edit_perm
}
end
def param_rules
{
:create => { :host_collection => [:name, :description, :system_ids, :max_content_hosts] },
:copy => { :host_collection => [:new_name, :description, :max_content_hosts] },
:update => { :host_collection => [:name, :description, :system_ids, :max_content_hosts] },
:add_content_hosts => { :host_collection => [:system_ids] },
:remove_content_hosts => { :host_collection => [:system_ids] },
:update_content_hosts => { :host_collection => [:environment_id, :content_view_id] }
}
end
respond_to :json
def_param_group :host_collection do
param :host_collection, Hash, :required => true, :action_aware => true do
param :name, String, :required => true, :desc => "Host collection name"
param :description, String
param :max_content_hosts, Integer, :desc => "Maximum number of content hosts in the host collection"
end
end
api :GET, "/organizations/:organization_id/host_collections", "List host collections"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :name, String, :desc => "Host collection name to filter by"
def index
query_string = params[:search]
filters = [:terms => {:id => HostCollection.readable.pluck(:id)}]
#downcase filtered analyzed field
filters << {:term => {:name => params[:name].downcase}} if params[:name]
options = {
:filters => filters
}
options.merge!(params.slice(:sort_by, :sort_order))
if params[:paged]
options[:page_size] = params[:page_size] || current_user.page_size
end
items = Glue::ElasticSearch::Items.new(HostCollection)
host_collections, total_count = items.retrieve(query_string, params[:offset], options)
if params[:paged]
host_collections = {
:host_collections => host_collections,
:subtotal => total_count,
:total => items.total_items
}
end
respond :collection => host_collections
end
api :GET, "/organizations/:organization_id/host_collections/:id", "Show a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
def show
respond
end
api :PUT, "/organizations/:organization_id/host_collections/:id", "Update a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
param_group :host_collection
def update
host_collection_param = params[:host_collection]
if host_collection_param[:system_ids]
host_collection_param[:system_ids] = system_uuids_to_ids(host_collection_param[:system_ids])
end
@host_collection.attributes = host_collection_param.slice(:name, :description, :system_ids, :max_content_hosts)
@host_collection.save!
respond
end
api :GET, "/organizations/:organization_id/host_collections/:id/content_hosts", "List content hosts in the host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
def content_hosts
respond_for_index :collection => @host_collection.systems.collect { |sys| { :id => sys.uuid, :name => sys.name } }
end
api :POST, "/organizations/:organization_id/host_collections/:id/add_content_hosts", "Add content hosts to the host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
param :host_collection, Hash, :required => true do
param :system_ids, Array, :desc => "Array of content host ids"
end
def add_content_hosts
ids = system_uuids_to_ids(params[:host_collection][:system_ids])
@systems = System.readable(@host_collection.organization).where(:id => ids)
@host_collection.system_ids = (@host_collection.system_ids + @systems.collect { |s| s.id }).uniq
@host_collection.save!
systems
end
api :POST, "/organizations/:organization_id/host_collections/:id/remove_content_hosts", "Remove content hosts from the host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
param :host_collection, Hash, :required => true do
param :system_ids, Array, :desc => "Array of system ids"
end
def remove_content_hosts
ids = system_uuids_to_ids(params[:host_collection][:system_ids])
system_ids = System.readable(@host_collection.organization).where(:id => ids).collect { |s| s.id }
@host_collection.system_ids = (@host_collection.system_ids - system_ids).uniq
@host_collection.save!
systems
end
api :GET, "/organizations/:organization_id/host_collections/:id/history", "History of jobs performed on a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
def history
jobs = @host_collection.refreshed_jobs
respond_for_index :collection => jobs
end
api :GET, "/organizations/:organization_id/host_collections/:id/history", "History of a job performed on a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
param :job_id, :identifier, :desc => "Id of a job for filtering"
def history_show
job = @host_collection.refreshed_jobs.where(:id => params[:job_id]).first
respond_for_show :resource => job
end
api :POST, "/organizations/:organization_id/host_collections", "Create a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param_group :host_collection
def create
host_collection_param = params[:host_collection]
if host_collection_param[:system_ids]
host_collection_param[:system_ids] = system_ids_to_uuids(host_collection_param[:system_ids])
end
@host_collection = HostCollection.new(host_collection_param)
@host_collection.organization = @organization
@host_collection.save!
respond
end
api :POST, "/organizations/:organization_id/host_collections/:id/copy", "Make copy of a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
param :host_collection, Hash, :required => true, :action_aware => true do
param :new_name, String, :required => true, :desc => "Host collection name"
param :description, String
param :max_content_hosts, Integer, :desc => "Maximum number of content hosts in the host collection"
end
def copy
if @organization.id != @host_collection.organization.id
fail HttpErrors::BadRequest,
_("Can't copy host collections to a different org: '%{org1}' != '%{org2}'") % { :org1 => @organization.id, :org2 => @host_collection.organization.id }
end
host_collection_param = params[:host_collection]
new_host_collection = HostCollection.new
new_host_collection.name = host_collection_param[:new_name]
new_host_collection.organization = @host_collection.organization
# Check API params and if not set use the existing host collection
if host_collection_param[:description]
new_host_collection.description = host_collection_param[:description]
else
new_host_collection.description = @host_collection.description
end
if host_collection_param[:max_content_hosts]
new_host_collection.max_content_hosts = host_collection_param[:max_content_hosts]
else
new_host_collection.max_content_hosts = @host_collection.max_content_hosts
end
new_host_collection.save!
new_host_collection.systems = @host_collection.systems
new_host_collection.save!
respond_for_create :resource => new_host_collection
end
api :DELETE, "/organizations/:organization_id/host_collections/:id", "Destroy a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
def destroy
@host_collection.destroy
respond :message => _("Deleted host collection '%s'") % params[:id]
end
api :DELETE, "/organizations/:organization_id/host_collections/:id/destroy_content_hosts",
"Destroy a host collection and its systems"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
def destroy_content_hosts
# this will destroy both the systems contained within the host collection as well as the host collection itself
system_names = []
@host_collection.systems.each do |system|
system_names.push(system.name)
system.destroy
end
@host_collection.destroy
result = _("Deleted host collection '%{s}' and it's %{n} systems.") % { :s => @host_collection.name, :n => system_names.length.to_s }
respond_for_destroy :message => result
end
api :PUT, "/organizations/:organization_id/host_collections/:id/update_content_hosts",
"Update systems within a host collection"
param :organization_id, :number, :desc => "organization identifier", :required => true
param :id, :identifier, :desc => "Id of the host collection", :required => true
param :host_collection, Hash do
param :content_view_id, :identifier, "id of the content view to set systems to"
param :environment_id, :identifier, "id of the enviornment to set systems to"
end
def update_content_hosts
unless params[:host_collection].blank?
ActiveRecord::Base.transaction do
@host_collection.systems.each do |system|
system.update_attributes!(params[:host_collection])
end
end
end
respond_for_show
end
private
def find_host_collection
@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 system_uuids_to_ids(ids)
system_ids = System.where(:uuid => ids).collect { |s| s.id }
fail Errors::NotFound.new(_("Systems [%s] not found.") % ids.join(',')) if system_ids.blank?
system_ids
end
# to make sure that the changes are reflected to elasticsearch immediately
# otherwise the index action doesn't have to know about the changes
def refresh_index
HostCollection.index.refresh if Katello.config.use_elasticsearch
end
end
end