Commit 8389f86e authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'mwaw/move_update_promethues_integration_to_operations' into 'master'

Add Prometheus Service to update action in Projects::Settings::Operations controller

Closes #200019

See merge request gitlab-org/gitlab!24296
parents 6bc898ad dde7be6f
......@@ -60,7 +60,7 @@ module Projects
# overridden in EE
def permitted_project_params
{
project_params = {
metrics_setting_attributes: [:external_dashboard_url],
error_tracking_setting_attributes: [
......@@ -72,6 +72,12 @@ module Projects
grafana_integration_attributes: [:token, :grafana_url, :enabled]
}
if Feature.enabled?(:settings_operations_prometheus_service, project)
project_params[:prometheus_integration_attributes] = [:manual_configuration, :api_url]
end
project_params
end
end
end
......
......@@ -322,6 +322,7 @@ class Project < ApplicationRecord
accepts_nested_attributes_for :error_tracking_setting, update_only: true
accepts_nested_attributes_for :metrics_setting, update_only: true, allow_destroy: true
accepts_nested_attributes_for :grafana_integration, update_only: true, allow_destroy: true
accepts_nested_attributes_for :prometheus_service, update_only: true
delegate :feature_available?, :builds_enabled?, :wiki_enabled?,
:merge_requests_enabled?, :forking_enabled?, :issues_enabled?,
......
......@@ -15,6 +15,7 @@ module Projects
error_tracking_params
.merge(metrics_setting_params)
.merge(grafana_integration_params)
.merge(prometheus_integration_params)
end
def metrics_setting_params
......@@ -77,6 +78,15 @@ module Projects
{ grafana_integration_attributes: attrs.merge(_destroy: destroy) }
end
def prometheus_integration_params
return {} unless attrs = params[:prometheus_integration_attributes]
service = project.find_or_initialize_service(::PrometheusService.to_param)
service.assign_attributes(attrs)
{ prometheus_service_attributes: service.attributes.except(*%w(id project_id created_at updated_at)) }
end
end
end
end
......
......@@ -196,6 +196,39 @@ describe Projects::Settings::OperationsController do
end
end
context 'prometheus integration' do
describe 'PATCH #update' do
let(:params) do
{
prometheus_integration_attributes: {
manual_configuration: '0',
api_url: 'https://gitlab.prometheus.rocks'
}
}
end
context 'feature flag :settings_operations_prometheus_service is enabled' do
before do
stub_feature_flags(settings_operations_prometheus_service: true)
end
it_behaves_like 'PATCHable'
end
context 'feature flag :settings_operations_prometheus_service is disabled' do
before do
stub_feature_flags(settings_operations_prometheus_service: false)
end
it_behaves_like 'PATCHable' do
let(:permitted_params) do
ActionController::Parameters.new(params.except(:prometheus_integration_attributes)).permit!
end
end
end
end
end
private
def project_params(project, params = {})
......
......@@ -289,5 +289,86 @@ describe Projects::Operations::UpdateService do
end
end
end
context 'prometheus integration' do
context 'prometheus params were passed into service' do
let(:prometheus_service) do
build_stubbed(:prometheus_service, project: project, properties: {
api_url: "http://example.prometheus.com",
manual_configuration: "0"
})
end
let(:prometheus_params) do
{
"type" => "PrometheusService",
"title" => nil,
"active" => true,
"properties" => { "api_url" => "http://example.prometheus.com", "manual_configuration" => "0" },
"instance" => false,
"push_events" => true,
"issues_events" => true,
"merge_requests_events" => true,
"tag_push_events" => true,
"note_events" => true,
"category" => "monitoring",
"default" => false,
"wiki_page_events" => true,
"pipeline_events" => true,
"confidential_issues_events" => true,
"commit_events" => true,
"job_events" => true,
"confidential_note_events" => true,
"deployment_events" => false,
"description" => nil,
"comment_on_event_enabled" => true
}
end
let(:params) do
{
prometheus_integration_attributes: {
api_url: 'http://new.prometheus.com',
manual_configuration: '1'
}
}
end
it 'uses Project#find_or_initialize_service to include instance defined defaults and pass them to Projects::UpdateService', :aggregate_failures do
project_update_service = double(Projects::UpdateService)
prometheus_update_params = prometheus_params.merge('properties' => {
'api_url' => 'http://new.prometheus.com',
'manual_configuration' => '1'
})
expect(project)
.to receive(:find_or_initialize_service)
.with('prometheus')
.and_return(prometheus_service)
expect(Projects::UpdateService)
.to receive(:new)
.with(project, user, { prometheus_service_attributes: prometheus_update_params })
.and_return(project_update_service)
expect(project_update_service).to receive(:execute)
subject.execute
end
end
context 'prometheus params were not passed into service' do
let(:params) { { something: :else } }
it 'does not pass any prometheus params into Projects::UpdateService', :aggregate_failures do
project_update_service = double(Projects::UpdateService)
expect(project).not_to receive(:find_or_initialize_service)
expect(Projects::UpdateService)
.to receive(:new)
.with(project, user, {})
.and_return(project_update_service)
expect(project_update_service).to receive(:execute)
subject.execute
end
end
end
end
end
......@@ -497,6 +497,63 @@ describe Projects::UpdateService do
update_project(project, user, { name: 'New name' })
end
end
context 'when updating nested attributes for prometheus service' do
context 'prometheus service exists' do
let(:prometheus_service_attributes) do
attributes_for(:prometheus_service,
project: project,
properties: { api_url: "http://new.prometheus.com", manual_configuration: "0" }
)
end
let!(:prometheus_service) do
create(:prometheus_service,
project: project,
properties: { api_url: "http://old.prometheus.com", manual_configuration: "0" }
)
end
it 'updates existing record' do
expect { update_project(project, user, prometheus_service_attributes: prometheus_service_attributes) }
.to change { prometheus_service.reload.api_url }
.from("http://old.prometheus.com")
.to("http://new.prometheus.com")
end
end
context 'prometheus service does not exist' do
context 'valid parameters' do
let(:prometheus_service_attributes) do
attributes_for(:prometheus_service,
project: project,
properties: { api_url: "http://example.prometheus.com", manual_configuration: "0" }
)
end
it 'creates new record' do
expect { update_project(project, user, prometheus_service_attributes: prometheus_service_attributes) }
.to change { ::PrometheusService.where(project: project).count }
.from(0)
.to(1)
end
end
context 'invalid parameters' do
let(:prometheus_service_attributes) do
attributes_for(:prometheus_service,
project: project,
properties: { api_url: nil, manual_configuration: "1" }
)
end
it 'does not create new record' do
expect { update_project(project, user, prometheus_service_attributes: prometheus_service_attributes) }
.not_to change { ::PrometheusService.where(project: project).count }
end
end
end
end
end
describe '#run_auto_devops_pipeline?' 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