Commit 663d27e8 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'feature/create_cluster_destroy_service' into 'master'

Creates destroy cluster service

See merge request gitlab-org/gitlab!18220
parents 2fad0cea 076b1713
...@@ -92,13 +92,12 @@ class Clusters::ClustersController < Clusters::BaseController ...@@ -92,13 +92,12 @@ class Clusters::ClustersController < Clusters::BaseController
end end
def destroy def destroy
if cluster.destroy response = Clusters::DestroyService
flash[:notice] = _('Kubernetes cluster integration was successfully removed.') .new(current_user, destroy_params)
redirect_to clusterable.index_path, status: :found .execute(cluster)
else
flash[:notice] = _('Kubernetes cluster integration was not removed.') flash[:notice] = response[:message]
render :show redirect_to clusterable.index_path, status: :found
end
end end
def create_gcp def create_gcp
...@@ -143,6 +142,14 @@ class Clusters::ClustersController < Clusters::BaseController ...@@ -143,6 +142,14 @@ class Clusters::ClustersController < Clusters::BaseController
private private
def destroy_params
# To be uncomented on https://gitlab.com/gitlab-org/gitlab/merge_requests/16954
# This MR got split into other since it was too big.
#
# params.permit(:cleanup)
{}
end
def update_params def update_params
if cluster.provided_by_user? if cluster.provided_by_user?
params.require(:cluster).permit( params.require(:cluster).permit(
......
# frozen_string_literal: true
module Clusters
class DestroyService
attr_reader :current_user, :params
def initialize(user = nil, params = {})
@current_user, @params = user, params.dup
@response = {}
end
def execute(cluster)
cleanup? ? start_cleanup!(cluster) : destroy_cluster!(cluster)
@response
end
private
def cleanup?
Gitlab::Utils.to_boolean(params[:cleanup])
end
def start_cleanup!(cluster)
cluster.start_cleanup!
@response[:message] = _('Kubernetes cluster integration and resources are being removed.')
end
def destroy_cluster!(cluster)
cluster.destroy!
@response[:message] = _('Kubernetes cluster integration was successfully removed.')
end
end
end
...@@ -9679,7 +9679,7 @@ msgstr "" ...@@ -9679,7 +9679,7 @@ msgstr ""
msgid "Kubernetes cluster creation time exceeds timeout; %{timeout}" msgid "Kubernetes cluster creation time exceeds timeout; %{timeout}"
msgstr "" msgstr ""
msgid "Kubernetes cluster integration was not removed." msgid "Kubernetes cluster integration and resources are being removed."
msgstr "" msgstr ""
msgid "Kubernetes cluster integration was successfully removed." msgid "Kubernetes cluster integration was successfully removed."
......
# frozen_string_literal: true
require 'spec_helper'
describe Clusters::DestroyService do
describe '#execute' do
subject { described_class.new(cluster.user, params).execute(cluster) }
let!(:cluster) { create(:cluster, :project, :provided_by_user) }
context 'when correct params' do
shared_examples 'only removes cluster' do
it 'does not start cleanup' do
expect(cluster).not_to receive(:start_cleanup)
subject
end
it 'destroys the cluster' do
subject
expect { cluster.reload }.to raise_error ActiveRecord::RecordNotFound
end
end
context 'when params are empty' do
let(:params) { {} }
it_behaves_like 'only removes cluster'
end
context 'when cleanup param is false' do
let(:params) { { cleanup: 'false' } }
it_behaves_like 'only removes cluster'
end
context 'when cleanup param is true' do
let(:params) { { cleanup: 'true' } }
before do
allow(Clusters::Cleanup::AppWorker).to receive(:perform_async)
end
it 'does not destroy cluster' do
subject
expect(Clusters::Cluster.where(id: cluster.id).exists?).not_to be_falsey
end
it 'transition cluster#cleanup_status from cleanup_not_started to uninstalling_applications' do
expect { subject }.to change { cluster.cleanup_status_name }
.from(:cleanup_not_started)
.to(:cleanup_uninstalling_applications)
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