Commit a1b6ed24 authored by rpereira2's avatar rpereira2

API to delete self monitoring project

* An admin API to trigger an async job that will delete the self
monitoring project.

* Also move common request specs into shared_examples.
parent 02bd43de
......@@ -102,7 +102,9 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
elsif SelfMonitoringProjectCreateWorker.in_progress?(job_id)
::Gitlab::PollingInterval.set_header(response, interval: 3_000)
render status: :accepted, json: { message: _('Job is in progress') }
render status: :accepted, json: {
message: _('Job to create self-monitoring project is in progress')
}
else
render status: :bad_request, json: {
......@@ -112,6 +114,49 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
end
end
def delete_self_monitoring_project
return self_monitoring_project_not_implemented unless Feature.enabled?(:self_monitoring_project)
job_id = SelfMonitoringProjectDeleteWorker.perform_async
render status: :accepted, json: {
job_id: job_id,
monitor_status: status_delete_self_monitoring_project_admin_application_settings_path
}
end
def status_delete_self_monitoring_project
return self_monitoring_project_not_implemented unless Feature.enabled?(:self_monitoring_project)
job_id = params[:job_id].to_s
unless job_id.length <= PARAM_JOB_ID_MAX_SIZE
return render status: :bad_request, json: {
message: _('Parameter "job_id" cannot exceed length of %{job_id_max_size}' %
{ job_id_max_size: PARAM_JOB_ID_MAX_SIZE })
}
end
if Gitlab::CurrentSettings.instance_administration_project_id.nil?
render status: :ok, json: {
message: _('Self-monitoring project has been successfully deleted')
}
elsif SelfMonitoringProjectDeleteWorker.in_progress?(job_id)
::Gitlab::PollingInterval.set_header(response, interval: 3_000)
render status: :accepted, json: {
message: _('Job to delete self-monitoring project is in progress')
}
else
render status: :bad_request, json: {
message: _('Self-monitoring project was not deleted. Please check logs ' \
'for any error messages')
}
end
end
private
def self_monitoring_data
......
......@@ -344,6 +344,12 @@ module ApplicationSettingsHelper
'status_create_self_monitoring_project_path' =>
status_create_self_monitoring_project_admin_application_settings_path,
'delete_self_monitoring_project_path' =>
delete_self_monitoring_project_admin_application_settings_path,
'status_delete_self_monitoring_project_path' =>
status_delete_self_monitoring_project_admin_application_settings_path,
'self_monitoring_project_exists' =>
Gitlab::CurrentSettings.instance_administration_project.present?,
......
......@@ -119,6 +119,8 @@ namespace :admin do
post :create_self_monitoring_project
get :status_create_self_monitoring_project
delete :delete_self_monitoring_project
get :status_delete_self_monitoring_project
end
resources :labels
......
......@@ -10278,9 +10278,6 @@ msgstr ""
msgid "Job has wrong arguments format."
msgstr ""
msgid "Job is in progress"
msgstr ""
msgid "Job is missing the `model_type` argument."
msgstr ""
......@@ -10290,6 +10287,12 @@ msgstr ""
msgid "Job logs and artifacts"
msgstr ""
msgid "Job to create self-monitoring project is in progress"
msgstr ""
msgid "Job to delete self-monitoring project is in progress"
msgstr ""
msgid "Job was retried"
msgstr ""
......@@ -16322,6 +16325,12 @@ msgstr ""
msgid "Self-monitoring project does not exist. Please check logs for any error messages"
msgstr ""
msgid "Self-monitoring project has been successfully deleted"
msgstr ""
msgid "Self-monitoring project was not deleted. Please check logs for any error messages"
msgstr ""
msgid "Send a separate email notification to Developers."
msgstr ""
......
......@@ -76,6 +76,20 @@ describe ApplicationSettingsHelper do
)
end
it 'returns delete_self_monitoring_project_path' do
expect(helper.self_monitoring_project_data).to include(
'delete_self_monitoring_project_path' =>
delete_self_monitoring_project_admin_application_settings_path
)
end
it 'returns status_delete_self_monitoring_project_path' do
expect(helper.self_monitoring_project_data).to include(
'status_delete_self_monitoring_project_path' =>
status_delete_self_monitoring_project_admin_application_settings_path
)
end
it 'returns self_monitoring_project_exists false' do
expect(helper.self_monitoring_project_data).to include(
'self_monitoring_project_exists' => false
......
......@@ -60,7 +60,7 @@ describe 'Self-Monitoring project requests' do
end
it_behaves_like 'sets polling header and returns accepted' do
let(:in_progress_message) { 'Job is in progress' }
let(:in_progress_message) { 'Job to create self-monitoring project is in progress' }
end
end
......@@ -115,4 +115,110 @@ describe 'Self-Monitoring project requests' do
end
end
end
describe 'DELETE #delete_self_monitoring_project' do
let(:worker_class) { SelfMonitoringProjectDeleteWorker }
subject { delete delete_self_monitoring_project_admin_application_settings_path }
it_behaves_like 'not accessible to non-admin users'
context 'with admin user' do
before do
login_as(admin)
end
context 'with feature flag disabled' do
it_behaves_like 'not accessible if feature flag is disabled'
end
context 'with feature flag enabled' do
let(:status_api) { status_delete_self_monitoring_project_admin_application_settings_path }
it_behaves_like 'triggers async worker, returns sidekiq job_id with response accepted'
end
end
end
describe 'GET #status_delete_self_monitoring_project' do
let(:worker_class) { SelfMonitoringProjectDeleteWorker }
let(:job_id) { 'job_id' }
subject do
get status_delete_self_monitoring_project_admin_application_settings_path,
params: { job_id: job_id }
end
it_behaves_like 'not accessible to non-admin users'
context 'with admin user' do
before do
login_as(admin)
end
context 'with feature flag disabled' do
it_behaves_like 'not accessible if feature flag is disabled'
end
context 'with feature flag enabled' do
it_behaves_like 'handles invalid job_id'
context 'when job is in progress' do
before do
allow(worker_class).to receive(:in_progress?)
.with(job_id)
.and_return(true)
stub_application_setting(instance_administration_project_id: 1)
end
it_behaves_like 'sets polling header and returns accepted' do
let(:in_progress_message) { 'Job to delete self-monitoring project is in progress' }
end
end
context 'when self-monitoring project exists and job does not exist' do
before do
stub_application_setting(instance_administration_project_id: 1)
end
it 'returns bad_request' do
subject
aggregate_failures do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to eq(
'message' => 'Self-monitoring project was not deleted. Please check logs ' \
'for any error messages'
)
end
end
end
context 'when self-monitoring project does not exist' do
it 'does not need job_id' do
get status_delete_self_monitoring_project_admin_application_settings_path
aggregate_failures do
expect(response).to have_gitlab_http_status(:success)
expect(json_response).to eq(
'message' => 'Self-monitoring project has been successfully deleted'
)
end
end
it 'returns success with job_id' do
subject
aggregate_failures do
expect(response).to have_gitlab_http_status(:success)
expect(json_response).to eq(
'message' => 'Self-monitoring project has been successfully deleted'
)
end
end
end
end
end
end
end
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