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 ...@@ -40,7 +40,7 @@ class Projects::ClustersController < Projects::ApplicationController
def update def update
Clusters::UpdateService Clusters::UpdateService
.new(project, current_user, update_params) .new(current_user, update_params)
.execute(cluster) .execute(cluster)
if cluster.valid? if cluster.valid?
...@@ -73,8 +73,8 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -73,8 +73,8 @@ class Projects::ClustersController < Projects::ApplicationController
def create_gcp def create_gcp
@gcp_cluster = ::Clusters::CreateService @gcp_cluster = ::Clusters::CreateService
.new(project, current_user, create_gcp_cluster_params) .new(current_user, create_gcp_cluster_params)
.execute(token_in_session) .execute(project: project, access_token: token_in_session)
if @gcp_cluster.persisted? if @gcp_cluster.persisted?
redirect_to project_cluster_path(project, @gcp_cluster) redirect_to project_cluster_path(project, @gcp_cluster)
...@@ -89,8 +89,8 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -89,8 +89,8 @@ class Projects::ClustersController < Projects::ApplicationController
def create_user def create_user
@user_cluster = ::Clusters::CreateService @user_cluster = ::Clusters::CreateService
.new(project, current_user, create_user_cluster_params) .new(current_user, create_user_cluster_params)
.execute(token_in_session) .execute(project: project, access_token: token_in_session)
if @user_cluster.persisted? if @user_cluster.persisted?
redirect_to project_cluster_path(project, @user_cluster) redirect_to project_cluster_path(project, @user_cluster)
......
# frozen_string_literal: true # frozen_string_literal: true
module Clusters module Clusters
class CreateService < BaseService class CreateService
attr_reader :access_token attr_reader :current_user, :params
def execute(access_token = nil) def initialize(user = nil, params = {})
@access_token = access_token @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? ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted?
end end
end end
private private
def create_cluster def create_cluster(cluster_params)
Clusters::Cluster.create(cluster_params) Clusters::Cluster.create(cluster_params)
end end
def cluster_params # EE would override this method
return @cluster_params if defined?(@cluster_params) def can_create_cluster?(project)
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?
project.clusters.empty? project.clusters.empty?
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
module Clusters 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) def execute(cluster)
cluster.update(params) cluster.update(params)
end end
......
...@@ -5,7 +5,7 @@ describe Clusters::CreateService do ...@@ -5,7 +5,7 @@ describe Clusters::CreateService do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } 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 provider is gcp' do
context 'when project has no clusters' do context 'when project has no clusters' do
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe Clusters::UpdateService do describe Clusters::UpdateService do
describe '#execute' 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) } 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