Commit 19e1024b authored by Thong Kuah's avatar Thong Kuah

Pass in project as kwarg for CreateService

This enables us to have other options in the future such as passing in
`group:`.

Also, remove project arg from UpdateService, as un-used.

This will help group cluster controller to re-use this services.
parent da4f7795
......@@ -40,7 +40,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?
......@@ -73,8 +73,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)
......@@ -89,8 +89,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
attr_reader :access_token
class CreateService
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
......
......@@ -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