2018-09-29 18:34:47 -04:00
# frozen_string_literal: true
2016-07-26 03:37:02 -04:00
module API
# Environments RESTfull API endpoints
2020-10-14 20:08:42 -04:00
class Environments < :: API :: Base
2016-11-21 14:15:46 -05:00
include PaginationParams
2016-07-26 03:37:02 -04:00
before { authenticate! }
2020-10-29 08:08:50 -04:00
feature_category :continuous_delivery
2022-04-29 08:10:13 -04:00
urgency :low
2020-10-29 08:08:50 -04:00
2016-07-29 06:14:36 -04:00
params do
2022-11-01 08:10:18 -04:00
requires :id , types : [ String , Integer ] , desc : 'The ID or URL-encoded path of the project owned by the authenticated user'
2016-07-29 06:14:36 -04:00
end
2018-11-08 07:18:17 -05:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2022-11-01 08:10:18 -04:00
desc 'List environments' do
detail 'Get all environments for a given project. This feature was introduced in GitLab 8.11.'
2016-07-29 06:14:36 -04:00
success Entities :: Environment
2022-11-01 08:10:18 -04:00
is_array true
failure [
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ environments ]
2016-07-29 06:14:36 -04:00
end
2016-08-01 02:42:09 -04:00
params do
2016-11-21 14:15:46 -05:00
use :pagination
2022-11-01 08:10:18 -04:00
optional :name , type : String , desc : 'Return the environment with this name. Mutually exclusive with search'
optional :search , type : String , desc : 'Return list of environments matching the search criteria. Mutually exclusive with name'
optional :states ,
type : String ,
values : Environment . valid_states . map ( & :to_s ) ,
desc : 'List all environments that match a specific state. Accepted values: `available`, `stopping`, or `stopped`. If no state value given, returns all environments'
2019-06-24 08:18:40 -04:00
mutually_exclusive :name , :search , message : 'cannot be used together'
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
2022-05-19 05:09:08 -04:00
environments = :: Environments :: EnvironmentsFinder . new ( user_project , current_user , declared_params ( include_missing : false ) ) . execute
2019-06-24 08:18:40 -04:00
present paginate ( environments ) , with : Entities :: Environment , current_user : current_user
2016-07-26 03:37:02 -04:00
end
2022-11-01 08:10:18 -04:00
desc 'Create a new environment' do
detail 'Creates a new environment with the given name and `external_url`. It returns `201` if the environment was successfully created, `400` for wrong parameters. This feature was introduced in GitLab 8.11.'
2016-07-29 06:14:36 -04:00
success Entities :: Environment
2022-11-01 08:10:18 -04:00
failure [
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' } ,
{ code : 422 , message : 'Unprocessable entity' }
]
tags %w[ environments ]
2016-07-29 06:14:36 -04:00
end
params do
2022-11-01 08:10:18 -04:00
requires :name , type : String , desc : 'The name of the environment'
optional :external_url , type : String , desc : 'Place to link to for this environment'
2022-10-14 20:10:32 -04:00
optional :slug , absence : { message : " is automatically generated and cannot be changed " } , documentation : { hidden : true }
2022-11-01 08:10:18 -04:00
optional :tier , type : String , values : Environment . tiers . keys , desc : 'The tier of the new environment. Allowed values are `production`, `staging`, `testing`, `development`, and `other`'
2016-07-29 06:14:36 -04:00
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
2016-07-29 06:14:36 -04:00
if environment . persisted?
2019-01-30 23:21:35 -05:00
present environment , with : Entities :: Environment , current_user : current_user
2016-07-26 03:37:02 -04:00
else
render_validation_error! ( environment )
end
end
2022-11-01 08:10:18 -04:00
desc 'Update an existing environment' do
detail 'Updates an existing environment name and/or `external_url`. It returns `200` if the environment was successfully updated. In case of an error, a status code `400` is returned. This feature was introduced in GitLab 8.11.'
2016-07-29 06:14:36 -04:00
success Entities :: Environment
2022-11-01 08:10:18 -04:00
failure [
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' } ,
{ code : 422 , message : 'Unprocessable entity' }
]
tags %w[ environments ]
2016-07-29 06:14:36 -04:00
end
params do
2022-11-01 08:10:18 -04:00
requires :environment_id , type : Integer , desc : 'The ID of the environment'
2021-08-26 17:11:25 -04:00
# TODO: disallow renaming via the API https://gitlab.com/gitlab-org/gitlab/-/issues/338897
optional :name , type : String , desc : 'DEPRECATED: Renaming environment can lead to errors, this will be removed in 15.0'
2016-07-29 06:14:36 -04:00
optional :external_url , type : String , desc : 'The new URL on which this deployment is viewable'
2022-10-14 20:10:32 -04:00
optional :slug , absence : { message : " is automatically generated and cannot be changed " } , documentation : { hidden : true }
2022-11-01 08:10:18 -04:00
optional :tier , type : String , values : Environment . tiers . keys , desc : 'The tier of the new environment. Allowed values are `production`, `staging`, `testing`, `development`, and `other`'
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
2022-05-02 23:10:17 -04:00
update_params = declared_params ( include_missing : false ) . extract! ( :name , :external_url , :tier )
2016-07-29 06:14:36 -04:00
if environment . update ( update_params )
2019-01-30 23:21:35 -05:00
present environment , with : Entities :: Environment , current_user : current_user
2016-07-26 03:37:02 -04:00
else
render_validation_error! ( environment )
end
end
2016-07-29 06:14:36 -04:00
2022-11-01 08:10:18 -04:00
desc 'Delete multiple stopped review apps' do
detail 'It schedules for deletion multiple environments that have already been stopped and are in the review app folder. The actual deletion is performed after 1 week from the time of execution. By default, it only deletes environments 30 days or older. You can change this default using the `before` parameter.'
2021-07-29 17:10:10 -04:00
success Entities :: EnvironmentBasic
2022-11-01 08:10:18 -04:00
failure [
{ code : 400 , message : 'Bad request' } ,
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' } ,
{ code : 409 , message : 'Conflict' }
]
tags %w[ environments ]
2021-03-11 22:08:56 -05:00
end
params do
2022-11-01 08:10:18 -04:00
optional :before , type : Time , desc : " The date before which environments can be deleted. Defaults to 30 days ago. Expected in ISO 8601 format (`YYYY-MM-DDTHH:MM:SSZ`) " , default : - > { 30 . days . ago }
optional :limit , type : Integer , desc : " Maximum number of environments to delete. Defaults to 100 " , default : 100 , values : 1 .. 1000
optional :dry_run , type : Boolean , desc : " Defaults to true for safety reasons. It performs a dry run where no actual deletion will be performed. Set to false to actually delete the environment " , default : true
2021-03-11 22:08:56 -05:00
end
delete " :id/environments/review_apps " do
authorize! :read_environment , user_project
result = :: Environments :: ScheduleToDeleteReviewAppsService . new ( user_project , current_user , params ) . execute
response = {
2021-07-29 17:10:10 -04:00
scheduled_entries : Entities :: EnvironmentBasic . represent ( result . scheduled_entries ) ,
unprocessable_entries : Entities :: EnvironmentBasic . represent ( result . unprocessable_entries )
2021-03-11 22:08:56 -05:00
}
if result . success?
status result . status
present response , current_user : current_user
else
render_api_error! ( response . merge! ( message : result . error_message ) , result . status )
end
end
2022-11-01 08:10:18 -04:00
desc 'Delete an environment' do
detail 'It returns 204 if the environment was successfully deleted, and 404 if the environment does not exist. This feature was introduced in GitLab 8.11.'
2016-07-29 06:14:36 -04:00
success Entities :: Environment
2022-11-01 08:10:18 -04:00
failure [
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ environments ]
2016-07-29 06:14:36 -04:00
end
params do
2022-11-01 08:10:18 -04:00
requires :environment_id , type : Integer , desc : 'The ID of the environment'
2016-07-29 06:14:36 -04:00
end
delete ':id/environments/:environment_id' do
2020-03-25 05:08:11 -04:00
authorize! :read_environment , user_project
2016-07-29 06:14:36 -04:00
environment = user_project . environments . find ( params [ :environment_id ] )
2020-03-25 05:08:11 -04:00
authorize! :destroy_environment , environment
2016-07-29 06:14:36 -04:00
2017-03-02 07:14:13 -05:00
destroy_conditionally! ( environment )
2016-07-29 06:14:36 -04:00
end
2017-02-28 08:55:21 -05:00
2022-11-01 08:10:18 -04:00
desc 'Stop an environment' do
detail 'It returns 200 if the environment was successfully stopped, and 404 if the environment does not exist.'
2017-02-28 08:55:21 -05:00
success Entities :: Environment
2022-11-01 08:10:18 -04:00
failure [
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ environments ]
2017-02-28 08:55:21 -05:00
end
params do
2022-11-01 08:10:18 -04:00
requires :environment_id , type : Integer , desc : 'The ID of the environment'
optional :force , type : Boolean , default : false , desc : 'Force environment to stop without executing `on_stop` actions'
2017-02-28 08:55:21 -05:00
end
post ':id/environments/:environment_id/stop' do
2018-07-10 04:11:04 -04:00
authorize! :read_environment , user_project
2017-02-28 08:55:21 -05:00
environment = user_project . environments . find ( params [ :environment_id ] )
2022-05-24 08:09:04 -04:00
:: Environments :: StopService . new ( user_project , current_user , declared_params ( include_missing : false ) )
. execute ( environment )
2017-02-28 08:55:21 -05:00
status 200
2019-01-30 23:21:35 -05:00
present environment , with : Entities :: Environment , current_user : current_user
2017-02-28 08:55:21 -05:00
end
2019-04-09 05:16:57 -04:00
2022-11-01 08:10:18 -04:00
desc 'Get a specific environment' do
2019-04-09 05:16:57 -04:00
success Entities :: Environment
2022-11-01 08:10:18 -04:00
failure [
{ code : 403 , message : 'Unauthenticated' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ environments ]
2019-04-09 05:16:57 -04:00
end
params do
2022-11-01 08:10:18 -04:00
requires :environment_id , type : Integer , desc : 'The ID of the environment'
2019-04-09 05:16:57 -04:00
end
get ':id/environments/:environment_id' do
authorize! :read_environment , user_project
environment = user_project . environments . find ( params [ :environment_id ] )
present environment , with : Entities :: Environment , current_user : current_user ,
except : [ :project , { last_deployment : [ :environment ] } ] ,
last_deployment : true
end
2016-07-26 03:37:02 -04:00
end
end
end