diff options
author | Z.J. van de Weg <zegerjan@gitlab.com> | 2016-07-26 10:37:02 +0300 |
---|---|---|
committer | Z.J. van de Weg <zegerjan@gitlab.com> | 2016-07-29 14:54:45 +0300 |
commit | 84cd2120952e7ee4095cb4b5d7c959f2c11610c5 (patch) | |
tree | 3f4f3498543311c4291f88285778c9d71c9cc21e /lib | |
parent | be9aa7f19474424991923f128053e2523fa166d8 (diff) |
Add API support for environments
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/entities.rb | 4 | ||||
-rw-r--r-- | lib/api/environments.rb | 87 |
3 files changed, 92 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 3d7d67510a8..9c960d74495 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -32,6 +32,7 @@ module API mount ::API::CommitStatuses mount ::API::Commits mount ::API::DeployKeys + mount ::API::Environments mount ::API::Files mount ::API::GroupMembers mount ::API::Groups diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 4eb95d8a215..3e21b7a0b8a 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -496,6 +496,10 @@ module API expose :key, :value end + class Environment < Grape::Entity + expose :id, :name, :external_url + end + class RepoLicense < Grape::Entity expose :key, :name, :nickname expose :featured, as: :popular diff --git a/lib/api/environments.rb b/lib/api/environments.rb new file mode 100644 index 00000000000..66a047f72fc --- /dev/null +++ b/lib/api/environments.rb @@ -0,0 +1,87 @@ +module API + # Environments RESTfull API endpoints + class Environments < Grape::API + before { authenticate! } + + resource :projects do + # Get all labels of the project + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # GET /projects/:id/environments + get ':id/environments' do + authorize! :read_environment, user_project + + present paginate(user_project.environments), with: Entities::Environment + end + + # Creates a new environment + # + # Parameters: + # id (required) - The ID of a project + # name (required) - The name of the environment to be created + # external_url (optional) - URL on which this deployment is viewable + # + # Example Request: + # POST /projects/:id/labels + post ':id/environments' do + authorize! :create_environment, user_project + required_attributes! [:name] + + attrs = attributes_for_keys [:name, :external_url] + environment = user_project.environments.find_by(name: attrs[:name]) + + conflict!('Environment already exists') if environment + + environment = user_project.environments.create(attrs) + + if environment.valid? + present environment, with: Entities::Environment + else + render_validation_error!(environment) + end + end + + # Deletes an existing environment + # + # Parameters: + # id (required) - The ID of a project + # environment_id (required) - The name of the environment to be deleted + # + # Example Request: + # DELETE /projects/:id/environments/:environment_id + delete ':id/environments/:environment_id' do + authorize! :admin_environment, user_project + + environment = user_project.environments.find(params[:environment_id]) + + present environment.destroy, with: Entities::Environment + end + + # Updates an existing environment + # + # Parameters: + # id (required) - The ID of a project + # environment_id (required) - The ID of the environment + # name (optional) - The name of the label to be deleted + # external_url (optional) - The new name of the label + # + # Example Request: + # PUT /projects/:id/environments/:environment_id + put ':id/environments/:environment_id' do + authorize! :update_environment, user_project + + environment = user_project.environments.find(params[:environment_id]) + + attrs = attributes_for_keys [:name, :external_url] + + if environment.update(attrs) + present environment, with: Entities::Environment + else + render_validation_error!(environment) + end + end + end + end +end |