summaryrefslogtreecommitdiff
path: root/app/controllers/clusters/applications_controller.rb
blob: c533fe007d75961db49b8b4133402f4179060fbf (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
# frozen_string_literal: true

class Clusters::ApplicationsController < Clusters::BaseController
  before_action :cluster
  before_action :authorize_create_cluster!, only: [:create]
  before_action :authorize_update_cluster!, only: [:update]
  before_action :authorize_admin_cluster!, only: [:destroy]

  def create
    request_handler do
      Clusters::Applications::CreateService
        .new(@cluster, current_user, cluster_application_params)
        .execute(request)
    end
  end

  def update
    request_handler do
      Clusters::Applications::UpdateService
        .new(@cluster, current_user, cluster_application_params)
        .execute(request)
    end
  end

  def destroy
    request_handler do
      Clusters::Applications::DestroyService
        .new(@cluster, current_user, cluster_application_destroy_params)
        .execute(request)
    end
  end

  private

  def request_handler
    yield

    head :no_content
  rescue Clusters::Applications::BaseService::InvalidApplicationError
    render_404
  rescue StandardError
    head :bad_request
  end

  def cluster
    @cluster ||= clusterable.clusters.find(params[:id]) || render_404
  end

  def cluster_application_params
    params.permit(:application, :hostname, :pages_domain_id, :email, :stack, :modsecurity_enabled, :modsecurity_mode, :host, :port, :protocol, :waf_log_enabled, :cilium_log_enabled)
  end

  def cluster_application_destroy_params
    params.permit(:application)
  end
end