Commit 8e256a8b authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'ee-34758-refactor-cluster-services' into 'master'

EE: Refactor Cluster services to enable groups or projects

See merge request gitlab-org/gitlab-ee!7876
parents baac8a77 3eaeee9a
......@@ -42,7 +42,7 @@ class Projects::ClustersController < Projects::ApplicationController
def update
Clusters::UpdateService
.new(project, current_user, update_params)
.new(current_user, update_params)
.execute(cluster)
if cluster.valid?
......@@ -75,8 +75,8 @@ class Projects::ClustersController < Projects::ApplicationController
def create_gcp
@gcp_cluster = ::Clusters::CreateService
.new(project, current_user, create_gcp_cluster_params)
.execute(token_in_session)
.new(current_user, create_gcp_cluster_params)
.execute(project: project, access_token: token_in_session)
if @gcp_cluster.persisted?
redirect_to project_cluster_path(project, @gcp_cluster)
......@@ -91,8 +91,8 @@ class Projects::ClustersController < Projects::ApplicationController
def create_user
@user_cluster = ::Clusters::CreateService
.new(project, current_user, create_user_cluster_params)
.execute(token_in_session)
.new(current_user, create_user_cluster_params)
.execute(project: project, access_token: token_in_session)
if @user_cluster.persisted?
redirect_to project_cluster_path(project, @user_cluster)
......
# frozen_string_literal: true
module Clusters
class CreateService < BaseService
class CreateService
prepend EE::Clusters::CreateService
attr_reader :access_token
attr_reader :current_user, :params
def execute(access_token = nil)
@access_token = access_token
def initialize(user = nil, params = {})
@current_user, @params = user, params.dup
end
raise ArgumentError.new(_('Instance does not support multiple Kubernetes clusters')) unless can_create_cluster?
def execute(project:, access_token: nil)
raise ArgumentError.new(_('Instance does not support multiple Kubernetes clusters')) unless can_create_cluster?(project)
create_cluster.tap do |cluster|
cluster_params = params.merge(user: current_user, projects: [project])
cluster_params[:provider_gcp_attributes].try do |provider|
provider[:access_token] = access_token
end
create_cluster(cluster_params).tap do |cluster|
ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted?
end
end
private
def create_cluster
def create_cluster(cluster_params)
Clusters::Cluster.create(cluster_params)
end
def cluster_params
return @cluster_params if defined?(@cluster_params)
params[:provider_gcp_attributes].try do |provider|
provider[:access_token] = access_token
end
@cluster_params = params.merge(user: current_user, projects: [project])
end
def can_create_cluster?
# EE would override this method
def can_create_cluster?(project)
project.clusters.empty?
end
end
......
# frozen_string_literal: true
module Clusters
class UpdateService < BaseService
class UpdateService
attr_reader :current_user, :params
def initialize(user = nil, params = {})
@current_user, @params = user, params.dup
end
def execute(cluster)
cluster.update(params)
end
......
......@@ -4,7 +4,7 @@ module EE
extend ::Gitlab::Utils::Override
override :can_create_cluster?
def can_create_cluster?
def can_create_cluster?(project)
super || project.feature_available?(:multiple_clusters)
end
end
......
......@@ -6,7 +6,7 @@ describe Clusters::CreateService do
let(:user) { create(:user) }
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
subject { described_class.new(project, user, params).execute(access_token) }
subject { described_class.new(user, params).execute(project: project, access_token: access_token) }
before do
allow(project).to receive(:feature_available?).and_call_original
......
......@@ -5,7 +5,7 @@ describe Clusters::CreateService do
let(:project) { create(:project) }
let(:user) { create(:user) }
subject { described_class.new(project, user, params).execute(access_token) }
subject { described_class.new(user, params).execute(project: project, access_token: access_token) }
context 'when provider is gcp' do
context 'when project has no clusters' do
......
......@@ -2,7 +2,7 @@ require 'spec_helper'
describe Clusters::UpdateService do
describe '#execute' do
subject { described_class.new(cluster.project, cluster.user, params).execute(cluster) }
subject { described_class.new(cluster.user, params).execute(cluster) }
let(:cluster) { create(:cluster, :project, :provided_by_user) }
......
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