/
environments_controller.rb
205 lines (175 loc) · 8.74 KB
/
environments_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
#
# 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::EnvironmentsController < Api::V2::ApiController
resource_description do
resource_id 'lifecycle_environments'
description <<-DESC
# Description
An environment is a basic organization structure that groups systems,
products, repositories, etc. Every system belongs to one environment
and it's isolated inside so that it can see only content that is in its
environment.
## Chains
Environments are ordered into chains and their content (propducts,
repositories, tempaltes, packages) can be moved to an environment only from its
prior environment. You can have for example chain like:
Library -> Development -> Testing -> Production
Each change in an environment is done through a changeset in an action
called promotion.
## Library
Library is a special environment that has no ascendant: All the content
starts in this environment. More chains can start from the library environment but
no further branching of a chain is enabled.
DESC
api_version 'v2'
api_base_url "#{Katello.config.url_prefix}/api"
end
respond_to :json
before_filter :find_organization, :only => [:index, :create, :paths]
before_filter :find_optional_organization, :only => [:show, :update, :destroy]
before_filter :find_prior, :only => [:create]
before_filter :find_environment, :only => [:show, :update, :destroy, :repositories]
before_filter :find_content_view, :only => [:repositories]
before_filter :load_search_service, :only => [:index]
wrap_parameters :include => (KTEnvironment.attribute_names + %w(prior new_name))
api :GET, "/environments", N_("List environments in an organization")
api :GET, "/organizations/:organization_id/environments", N_("List environments in an organization")
param :organization_id, :number, :desc => N_("organization identifier"), :required => true
param :library, [true, false], :desc => N_("set true if you want to see only library environments")
param :name, String, :desc => N_("filter only environments containing this name")
def index
filters = []
ids = KTEnvironment.readable.pluck(:id)
filters << {:terms => {:id => ids}}
filters << {:terms => {:organization_id => [@organization.id]}}
filters << {:terms => {:name => [params[:name]]}} if params[:name]
filters << {:terms => {:library => [params[:library]]}} if params[:library].present?
options = {
:filters => filters,
:load_records? => true
}
respond_for_index(:collection => item_search(KTEnvironment, params, options))
end
api :GET, "/environments/:id", N_("Show an environment")
api :GET, "/organizations/:organization_id/environments/:environment_id", N_("Show an environment")
param :id, :number, :desc => N_("ID of the environment"), :required => true
param :organization_id, :number, :desc => N_("ID of the organization")
def show
respond
end
api :POST, "/environments", N_("Create an environment")
api :POST, "/organizations/:organization_id/environments", N_("Create an environment in an organization")
param :organization_id, :number, :desc => N_("name of organization"), :required => true
param :name, String, :desc => N_("name of the environment"), :required => true
param :description, String, :desc => N_("description of the environment")
param :prior, String, :required => true, :desc => <<-DESC
Name of an environment that is prior to the new environment in the chain. It has to be
either 'Library' or an environment at the end of a chain.
DESC
def create
create_params = environment_params
create_params[:label] = labelize_params(create_params)
create_params[:organization] = @organization
create_params[:prior] = @prior
@environment = KTEnvironment.create!(create_params)
@organization.kt_environments << @environment
@organization.save!
respond
end
api :PUT, "/environments/:id", N_("Update an environment")
api :PUT, "/organizations/:organization_id/environments/:id", N_("Update an environment in an organization")
param :id, :number, :desc => N_("ID of the environment"), :required => true
param :organization_id, :number, :desc => N_("name of the organization")
param :new_name, String, :desc => N_("new name to be given to the environment")
param :description, String, :desc => N_("description of the environment")
param :prior, String, :desc => <<-DESC
Name of an environment that is prior to the new environment in the chain. It has to be
either 'Library' or an environment at the end of a chain.
DESC
def update
fail HttpErrors::BadRequest, _("Can't update the '%s' environment") % "Library" if @environment.library?
update_params = environment_params
update_params[:name] = params[:environment][:new_name] if params[:environment][:new_name]
update_params[:label] = labelize_params(update_params) if update_params[:name]
@environment.update_attributes!(update_params)
respond
end
api :DELETE, "/environments/:id", N_("Destroy an environment")
api :DELETE, "/organizations/:organization_id/environments/:id", N_("Destroy an environment in an organization")
param :id, :number, :desc => N_("ID of the environment"), :required => true
param :organization_id, :number, :desc => N_("organization identifier")
def destroy
if @environment.is_deletable?
@environment.destroy
respond_for_destroy
else
fail HttpErrors::BadRequest, @environment.errors.full_messages.join(" ")
end
end
api :GET, "/organizations/:organization_id/environments/paths", N_("List environment paths")
param :organization_id, :number, :desc => N_("organization identifier")
param :permission_type, String, :desc => <<-DESC
The associated permission type. One of (readable | promotable)
Default: readable
DESC
def paths
env_paths = if params[:permission_type] == "promotable"
@organization.promotable_promotion_paths
else
@organization.readable_promotion_paths
end
paths = env_paths.inject([]) do |result, path|
result << { :environments => [@organization.library] + path }
end
paths = [{ :environments => [@organization.library] }] if paths.empty?
respond_for_index(:collection => paths, :template => :paths)
end
api :GET, "/organizations/:organization_id/environments/:id/repositories", "List repositories available in the environment"
param :id, :identifier, :desc => "environment identifier"
param :organization_id, String, :desc => "organization identifier"
param :content_view_id, :identifier, :desc => "content view identifier", :required => false
def repositories
if !@environment.library? && @content_view.nil?
fail HttpErrors::BadRequest,
_("Cannot retrieve repos from non-library environment '%s' without a content view.") % @environment.name
end
@repositories = @environment.products.readable(@organization).flat_map do |p|
p.repos(@environment, @content_view)
end
respond_for_index :collection => @repositories
end
protected
def find_environment
identifier = params.require(:id) || params.require(:environment).require(:id)
@environment = KTEnvironment.find(identifier)
fail HttpErrors::NotFound, _("Couldn't find environment '%s'") % identifier.to_s if @environment.nil?
@organization = @environment.organization
@environment
end
def find_prior
prior = params.require(:environment).require(:prior)
@prior = KTEnvironment.readable.find(prior)
fail HttpErrors::NotFound, _("Couldn't find prior-environment '%s'") % prior.to_s if @prior.nil?
@prior
end
def environment_params
attrs = [:name, :description]
attrs.push(:label, :prior) if params[:action] == "create"
parms = params.require(:environment).permit(*attrs)
parms
end
def find_content_view
@content_view = ContentView.readable.find_by_id(params[:content_view_id])
end
end
end