2021-04-06 20:09:26 -04:00
|
|
|
# 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?
|
|
|
|
|
2021-04-15 08:09:05 -04:00
|
|
|
integration.enabled = params[:enabled]
|
|
|
|
integration.save!
|
2021-04-06 20:09:26 -04:00
|
|
|
|
2021-04-15 08:09:05 -04:00
|
|
|
if integration.enabled?
|
|
|
|
ServiceResponse.success(message: s_('ClusterIntegration|Integration enabled'), payload: { integration: integration })
|
2021-04-06 20:09:26 -04:00
|
|
|
else
|
2021-04-15 08:09:05 -04:00
|
|
|
ServiceResponse.success(message: s_('ClusterIntegration|Integration disabled'), payload: { integration: integration })
|
2021-04-06 20:09:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-15 08:09:05 -04:00
|
|
|
def integration
|
2021-05-13 23:10:11 -04:00
|
|
|
@integration ||= \
|
|
|
|
case params[:application_type]
|
|
|
|
when 'prometheus'
|
|
|
|
cluster.find_or_build_integration_prometheus
|
|
|
|
when 'elastic_stack'
|
|
|
|
cluster.find_or_build_integration_elastic_stack
|
|
|
|
else
|
|
|
|
raise ArgumentError, "invalid application_type: #{params[:application_type]}"
|
|
|
|
end
|
2021-04-15 08:09:05 -04:00
|
|
|
end
|
|
|
|
|
2021-04-06 20:09:26 -04:00
|
|
|
def authorized?
|
|
|
|
Ability.allowed?(current_user, :admin_cluster, cluster)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|