BigW Consortium Gitlab

environments.rb 2.74 KB
Newer Older
1 2 3
module API
  # Environments RESTfull API endpoints
  class Environments < Grape::API
4 5
    include PaginationParams

6 7
    before { authenticate! }

8 9 10
    params do
      requires :id, type: String, desc: 'The project ID'
    end
11
    resource :projects do
12 13 14 15
      desc 'Get all environments of the project' do
        detail 'This feature was introduced in GitLab 8.11.'
        success Entities::Environment
      end
16
      params do
17
        use :pagination
18
      end
19 20 21 22 23 24
      get ':id/environments' do
        authorize! :read_environment, user_project

        present paginate(user_project.environments), with: Entities::Environment
      end

25 26 27 28 29 30 31 32
      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'
      end
33 34 35
      post ':id/environments' do
        authorize! :create_environment, user_project

36
        environment = user_project.environments.create(declared_params)
37

38
        if environment.persisted?
39 40 41 42 43 44
          present environment, with: Entities::Environment
        else
          render_validation_error!(environment)
        end
      end

45 46 47 48 49 50 51 52
      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'
53 54 55 56 57
      end
      put ':id/environments/:environment_id' do
        authorize! :update_environment, user_project

        environment = user_project.environments.find(params[:environment_id])
58 59

        update_params = declared_params(include_missing: false).extract!(:name, :external_url)
60
        if environment.update(update_params)
61 62 63 64 65
          present environment, with: Entities::Environment
        else
          render_validation_error!(environment)
        end
      end
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

      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])

        present environment.destroy, with: Entities::Environment
      end
81 82 83
    end
  end
end