gitlab-org--gitlab-foss/lib/api/environments.rb

104 lines
3.5 KiB
Ruby
Raw Normal View History

2016-07-26 03:37:02 -04:00
module API
# Environments RESTfull API endpoints
class Environments < Grape::API
2016-12-07 20:09:18 -05:00
include ::API::Helpers::CustomValidators
include PaginationParams
2016-07-26 03:37:02 -04:00
before { authenticate! }
params do
requires :id, type: String, desc: 'The project ID'
end
2017-08-31 07:44:49 -04:00
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
desc 'Get all environments of the project' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities::Environment
end
2016-08-01 02:42:09 -04:00
params do
use :pagination
2016-08-01 02:42:09 -04:00
end
2016-07-26 03:37:02 -04:00
get ':id/environments' do
authorize! :read_environment, user_project
present paginate(user_project.environments), with: Entities::Environment
end
desc 'Creates a new environment' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities::Environment
end
params do
requires :name, type: String, desc: 'The name of the environment to be created'
optional :external_url, type: String, desc: 'URL on which this deployment is viewable'
2016-12-07 20:09:18 -05:00
optional :slug, absence: { message: "is automatically generated and cannot be changed" }
end
2016-07-26 03:37:02 -04:00
post ':id/environments' do
authorize! :create_environment, user_project
2016-11-14 08:44:27 -05:00
environment = user_project.environments.create(declared_params)
2016-07-26 03:37:02 -04:00
if environment.persisted?
2016-07-26 03:37:02 -04:00
present environment, with: Entities::Environment
else
render_validation_error!(environment)
end
end
desc 'Updates an existing environment' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities::Environment
end
params do
requires :environment_id, type: Integer, desc: 'The environment ID'
optional :name, type: String, desc: 'The new environment name'
optional :external_url, type: String, desc: 'The new URL on which this deployment is viewable'
2016-12-07 20:09:18 -05:00
optional :slug, absence: { message: "is automatically generated and cannot be changed" }
2016-07-26 03:37:02 -04:00
end
put ':id/environments/:environment_id' do
authorize! :update_environment, user_project
environment = user_project.environments.find(params[:environment_id])
2016-11-14 08:44:27 -05:00
update_params = declared_params(include_missing: false).extract!(:name, :external_url)
if environment.update(update_params)
2016-07-26 03:37:02 -04:00
present environment, with: Entities::Environment
else
render_validation_error!(environment)
end
end
desc 'Deletes an existing environment' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities::Environment
end
params do
requires :environment_id, type: Integer, desc: 'The environment ID'
end
delete ':id/environments/:environment_id' do
authorize! :update_environment, user_project
environment = user_project.environments.find(params[:environment_id])
2017-03-02 07:14:13 -05:00
destroy_conditionally!(environment)
end
desc 'Stops an existing environment' do
success Entities::Environment
end
params do
requires :environment_id, type: Integer, desc: 'The environment ID'
end
post ':id/environments/:environment_id/stop' do
authorize! :create_deployment, user_project
environment = user_project.environments.find(params[:environment_id])
environment.stop_with_action!(current_user)
status 200
present environment, with: Entities::Environment
end
2016-07-26 03:37:02 -04:00
end
end
end