1# frozen_string_literal: true
2
3module Clusters
4  class IntegrationsController < ::Clusters::BaseController
5    before_action :cluster
6    before_action :authorize_admin_cluster!, only: [:create_or_update]
7
8    def create_or_update
9      service_response = Clusters::Integrations::CreateService
10        .new(container: clusterable, cluster: cluster, current_user: current_user, params: cluster_integration_params)
11        .execute
12
13      if service_response.success?
14        redirect_to cluster.show_path(params: { tab: 'integrations' }), notice: service_response.message
15      else
16        redirect_to cluster.show_path(params: { tab: 'integrations' }), alert: service_response.message
17      end
18    end
19
20    private
21
22    def clusterable
23      raise NotImplementedError
24    end
25
26    def cluster_integration_params
27      params.permit(integration: [:enabled, :application_type]).require(:integration)
28    end
29
30    def cluster
31      @cluster ||= clusterable.clusters.find(params[:cluster_id]).present(current_user: current_user)
32    end
33  end
34end
35