summaryrefslogtreecommitdiff
path: root/lib/api/environments.rb
blob: 532baec42c7a561b1f82dba43e7f58c74c77591f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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.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! :update_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