BigW Consortium Gitlab

environments.rb 3.48 KB
Newer Older
1 2 3
module API
  # Environments RESTfull API endpoints
  class Environments < Grape::API
Nick Thomas committed
4
    include ::API::Helpers::CustomValidators
5 6
    include PaginationParams

7 8
    before { authenticate! }

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

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

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'
Nick Thomas committed
33
        optional :slug, absence: { message: "is automatically generated and cannot be changed" }
34
      end
35 36 37
      post ':id/environments' do
        authorize! :create_environment, user_project

38
        environment = user_project.environments.create(declared_params)
39

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

47 48 49 50 51 52 53 54
      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'
Nick Thomas committed
55
        optional :slug, absence: { message: "is automatically generated and cannot be changed" }
56 57 58 59 60
      end
      put ':id/environments/:environment_id' do
        authorize! :update_environment, user_project

        environment = user_project.environments.find(params[:environment_id])
61 62

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

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

82
        environment.destroy
83
      end
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

      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
101 102 103
    end
  end
end