summaryrefslogtreecommitdiff
path: root/app/services/clusters/integrations/create_service.rb
blob: f9e9dd3e457d4a474b803fd6ca361c8987d793c1 (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
# frozen_string_literal: true

module Clusters
  module Integrations
    class CreateService < BaseContainerService
      attr_accessor :cluster

      def initialize(container:, cluster:, current_user: nil, params: {})
        @cluster = cluster

        super(container: container, current_user: current_user, params: params)
      end

      def execute
        return ServiceResponse.error(message: 'Unauthorized') unless authorized?

        integration.enabled = params[:enabled]
        integration.save!

        if integration.enabled?
          ServiceResponse.success(message: s_('ClusterIntegration|Integration enabled'), payload: { integration: integration })
        else
          ServiceResponse.success(message: s_('ClusterIntegration|Integration disabled'), payload: { integration: integration })
        end
      end

      private

      def integration
        case params[:application_type]
        when 'prometheus'
          cluster.find_or_build_integration_prometheus
        else
          raise ArgumentError, "invalid application_type: #{params[:application_type]}"
        end
      end

      def authorized?
        Ability.allowed?(current_user, :admin_cluster, cluster)
      end
    end
  end
end