Commit f0832955 authored by Arturo Herrero's avatar Arturo Herrero

Simplify find_or_initialize_service precedence

Extract build_from_instance_or_template to simplify the precedence when
building the service.
parent 8aa4f221
......@@ -1267,22 +1267,7 @@ class Project < ApplicationRecord
def find_or_initialize_service(name)
return if disabled_services.include?(name)
service = find_service(services, name)
return service if service
instance = find_service(services_instances, name)
if instance
Service.build_from_integration(id, instance)
else
template = find_service(services_templates, name)
if template
Service.build_from_integration(id, template)
else
public_send("build_#{name}_service") # rubocop:disable GitlabSecurity/PublicSend
end
end
find_service(services, name) || build_from_instance_or_template(name) || public_send("build_#{name}_service") # rubocop:disable GitlabSecurity/PublicSend
end
# rubocop: disable CodeReuse/ServiceClass
......@@ -2450,6 +2435,22 @@ class Project < ApplicationRecord
services.find { |service| service.to_param == name }
end
def build_from_instance_or_template(name)
instance = find_service(services_instances, name)
return Service.build_from_integration(id, instance) if instance
template = find_service(services_templates, name)
return Service.build_from_integration(id, template) if template
end
def services_templates
@services_templates ||= Service.templates
end
def services_instances
@services_instances ||= Service.instances
end
def closest_namespace_setting(name)
namespace.closest_setting(name)
end
......@@ -2578,14 +2579,6 @@ class Project < ApplicationRecord
end
end
def services_templates
@services_templates ||= Service.templates
end
def services_instances
@services_instances ||= Service.instances
end
def ensure_pages_metadatum
pages_metadatum || create_pages_metadatum!
rescue ActiveRecord::RecordNotUnique
......
......@@ -5264,15 +5264,22 @@ describe Project do
expect { subject.find_or_initialize_service('prometheus') }.not_to exceed_query_limit(control_count)
end
it 'returns nil if service is disabled' do
it 'returns nil if integration is disabled' do
allow(subject).to receive(:disabled_services).and_return(%w[prometheus])
expect(subject.find_or_initialize_service('prometheus')).to be_nil
end
it 'builds the service if instance or template does not exists' do
expect(subject.find_or_initialize_service('prometheus')).to be_a(PrometheusService)
expect(subject.find_or_initialize_service('prometheus').api_url).to be_nil
context 'with an existing integration' do
subject { create(:project) }
before do
create(:prometheus_service, project: subject, api_url: 'https://prometheus.project.com/')
end
it 'retrieves the integration' do
expect(subject.find_or_initialize_service('prometheus').api_url).to eq('https://prometheus.project.com/')
end
end
context 'with an instance-level and template integrations' do
......@@ -5295,6 +5302,13 @@ describe Project do
expect(subject.find_or_initialize_service('prometheus').api_url).to eq('https://prometheus.template.com/')
end
end
context 'without an exisiting integration, nor instance-level or template' do
it 'builds the service if instance or template does not exists' do
expect(subject.find_or_initialize_service('prometheus')).to be_a(PrometheusService)
expect(subject.find_or_initialize_service('prometheus').api_url).to be_nil
end
end
end
describe '.for_group' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment