Commit 8aa4f221 authored by Arturo Herrero's avatar Arturo Herrero

Initialize service from instance

find_or_initialize_service has the following precedence:
- Retrieve the existing service
- Build service from instance-level
- Build service from template
- Build service
parent ca34500c
...@@ -1270,12 +1270,18 @@ class Project < ApplicationRecord ...@@ -1270,12 +1270,18 @@ class Project < ApplicationRecord
service = find_service(services, name) service = find_service(services, name)
return service if service return service if service
template = find_service(services_templates, name) instance = find_service(services_instances, name)
if template if instance
Service.build_from_integration(id, template) Service.build_from_integration(id, instance)
else else
public_send("build_#{name}_service") # rubocop:disable GitlabSecurity/PublicSend 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 end
end end
...@@ -2573,7 +2579,11 @@ class Project < ApplicationRecord ...@@ -2573,7 +2579,11 @@ class Project < ApplicationRecord
end end
def services_templates def services_templates
@services_templates ||= Service.where(template: true) @services_templates ||= Service.templates
end
def services_instances
@services_instances ||= Service.instances
end end
def ensure_pages_metadatum def ensure_pages_metadatum
......
...@@ -5241,12 +5241,10 @@ describe Project do ...@@ -5241,12 +5241,10 @@ describe Project do
end end
end end
describe "#find_or_initialize_services" do describe '#find_or_initialize_services' do
subject { build(:project) }
it 'returns only enabled services' do it 'returns only enabled services' do
allow(Service).to receive(:available_services_names).and_return(%w(prometheus pushover)) allow(Service).to receive(:available_services_names).and_return(%w[prometheus pushover])
allow(subject).to receive(:disabled_services).and_return(%w(prometheus)) allow(subject).to receive(:disabled_services).and_return(%w[prometheus])
services = subject.find_or_initialize_services services = subject.find_or_initialize_services
...@@ -5255,11 +5253,9 @@ describe Project do ...@@ -5255,11 +5253,9 @@ describe Project do
end end
end end
describe "#find_or_initialize_service" do describe '#find_or_initialize_service' do
subject { build(:project) }
it 'avoids N+1 database queries' do it 'avoids N+1 database queries' do
allow(Service).to receive(:available_services_names).and_return(%w(prometheus pushover)) allow(Service).to receive(:available_services_names).and_return(%w[prometheus pushover])
control_count = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_service('prometheus') }.count control_count = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_service('prometheus') }.count
...@@ -5269,10 +5265,36 @@ describe Project do ...@@ -5269,10 +5265,36 @@ describe Project do
end end
it 'returns nil if service is disabled' do it 'returns nil if service is disabled' do
allow(subject).to receive(:disabled_services).and_return(%w(prometheus)) allow(subject).to receive(:disabled_services).and_return(%w[prometheus])
expect(subject.find_or_initialize_service('prometheus')).to be_nil expect(subject.find_or_initialize_service('prometheus')).to be_nil
end 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
end
context 'with an instance-level and template integrations' do
before do
create(:prometheus_service, :instance, api_url: 'https://prometheus.instance.com/')
create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/')
end
it 'builds the service from the instance if exists' do
expect(subject.find_or_initialize_service('prometheus').api_url).to eq('https://prometheus.instance.com/')
end
end
context 'with an instance-level and template integrations' do
before do
create(:prometheus_service, :template, api_url: 'https://prometheus.template.com/')
end
it 'builds the service from the template if instance does not exists' do
expect(subject.find_or_initialize_service('prometheus').api_url).to eq('https://prometheus.template.com/')
end
end
end end
describe '.for_group' do 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