Commit 567b86b2 authored by Nick Thomas's avatar Nick Thomas

Merge branch '32351-delete-controller' into 'master'

API to delete self-monitoring project and monitor status

See merge request gitlab-org/gitlab!21431
parents 87452acf 28c8c231
......@@ -5,6 +5,12 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
before_action :set_application_setting
before_action :whitelist_query_limiting, only: [:usage_data]
before_action :validate_self_monitoring_feature_flag_enabled, only: [
:create_self_monitoring_project,
:status_create_self_monitoring_project,
:delete_self_monitoring_project,
:status_delete_self_monitoring_project
]
before_action do
push_frontend_feature_flag(:self_monitoring_project)
......@@ -74,8 +80,6 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
end
def create_self_monitoring_project
return self_monitoring_project_not_implemented unless Feature.enabled?(:self_monitoring_project)
job_id = SelfMonitoringProjectCreateWorker.perform_async
render status: :accepted, json: {
......@@ -85,8 +89,6 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
end
def status_create_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
......@@ -97,23 +99,66 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController
end
if Gitlab::CurrentSettings.instance_administration_project_id.present?
render status: :ok, json: self_monitoring_data
return render status: :ok, json: self_monitoring_data
elsif SelfMonitoringProjectCreateWorker.in_progress?(job_id)
::Gitlab::PollingInterval.set_header(response, interval: 3_000)
render status: :accepted, json: { message: _('Job is in progress') }
return render status: :accepted, json: {
message: _('Job to create self-monitoring project is in progress')
}
end
render status: :bad_request, json: {
message: _('Self-monitoring project does not exist. Please check logs ' \
'for any error messages')
}
end
def delete_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
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
else
render status: :bad_request, json: {
message: _('Self-monitoring project does not exist. Please check logs ' \
'for any error messages')
if Gitlab::CurrentSettings.instance_administration_project_id.nil?
return 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)
return render status: :accepted, json: {
message: _('Job to delete self-monitoring project is in progress')
}
end
render status: :bad_request, json: {
message: _('Self-monitoring project was not deleted. Please check logs ' \
'for any error messages')
}
end
private
def validate_self_monitoring_feature_flag_enabled
self_monitoring_project_not_implemented unless Feature.enabled?(:self_monitoring_project)
end
def self_monitoring_data
{
project_id: Gitlab::CurrentSettings.instance_administration_project_id,
......
......@@ -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
......
......@@ -10314,9 +10314,6 @@ msgstr ""
msgid "Job has wrong arguments format."
msgstr ""
msgid "Job is in progress"
msgstr ""
msgid "Job is missing the `model_type` argument."
msgstr ""
......@@ -10326,6 +10323,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 ""
......@@ -16367,6 +16370,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