diff options
author | Thong Kuah <tkuah@gitlab.com> | 2018-10-11 02:15:04 +0300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2018-10-15 01:01:09 +0300 |
commit | 19e1024bb874cdb9d912dd7de3cbc8f26daad048 (patch) | |
tree | 74f7891ef2134feee46837af5866ff75e5b2373b | |
parent | da4f77957ccff7cdf22110ef56a4286bbc5a7749 (diff) |
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.
-rw-r--r-- | app/controllers/projects/clusters_controller.rb | 10 | ||||
-rw-r--r-- | app/services/clusters/create_service.rb | 34 | ||||
-rw-r--r-- | app/services/clusters/update_service.rb | 8 | ||||
-rw-r--r-- | spec/services/clusters/create_service_spec.rb | 2 | ||||
-rw-r--r-- | spec/services/clusters/update_service_spec.rb | 2 |
5 files changed, 30 insertions, 26 deletions
diff --git a/app/controllers/projects/clusters_controller.rb b/app/controllers/projects/clusters_controller.rb index bcdbf48bb35..62adc66fb09 100644 --- a/app/controllers/projects/clusters_controller.rb +++ b/app/controllers/projects/clusters_controller.rb @@ -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) diff --git a/app/services/clusters/create_service.rb b/app/services/clusters/create_service.rb index e3e0cfa462c..c6e955800af 100644 --- a/app/services/clusters/create_service.rb +++ b/app/services/clusters/create_service.rb @@ -1,36 +1,34 @@ # 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 diff --git a/app/services/clusters/update_service.rb b/app/services/clusters/update_service.rb index 98fdeec4fb1..25d26e761b1 100644 --- a/app/services/clusters/update_service.rb +++ b/app/services/clusters/update_service.rb @@ -1,7 +1,13 @@ # 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 diff --git a/spec/services/clusters/create_service_spec.rb b/spec/services/clusters/create_service_spec.rb index 1685dc748bd..3959295c13e 100644 --- a/spec/services/clusters/create_service_spec.rb +++ b/spec/services/clusters/create_service_spec.rb @@ -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 diff --git a/spec/services/clusters/update_service_spec.rb b/spec/services/clusters/update_service_spec.rb index 2d91a21035d..dcd75b6912d 100644 --- a/spec/services/clusters/update_service_spec.rb +++ b/spec/services/clusters/update_service_spec.rb @@ -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) } |