Commit 076b1713 authored by João Cunha's avatar João Cunha Committed by Jan Provaznik

Creates destroy cluster service

The service will later on receive params and extend its functionality
to either simply remove the cluster or remove cluster and its
resources depending on the params sent by the frontend
parent 2fad0cea
......@@ -92,13 +92,12 @@ class Clusters::ClustersController < Clusters::BaseController
end
def destroy
if cluster.destroy
flash[:notice] = _('Kubernetes cluster integration was successfully removed.')
response = Clusters::DestroyService
.new(current_user, destroy_params)
.execute(cluster)
flash[:notice] = response[:message]
redirect_to clusterable.index_path, status: :found
else
flash[:notice] = _('Kubernetes cluster integration was not removed.')
render :show
end
end
def create_gcp
......@@ -143,6 +142,14 @@ class Clusters::ClustersController < Clusters::BaseController
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
if cluster.provided_by_user?
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 ""
msgid "Kubernetes cluster creation time exceeds timeout; %{timeout}"
msgstr ""
msgid "Kubernetes cluster integration was not removed."
msgid "Kubernetes cluster integration and resources are being removed."
msgstr ""
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