2018-07-16 16:31:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-13 17:21:23 +00:00
|
|
|
module Clusters
|
2018-10-10 23:15:04 +00:00
|
|
|
class CreateService
|
|
|
|
attr_reader :current_user, :params
|
2017-10-01 08:48:21 +00:00
|
|
|
|
2018-10-10 23:15:04 +00:00
|
|
|
def initialize(user = nil, params = {})
|
|
|
|
@current_user, @params = user, params.dup
|
|
|
|
end
|
2017-10-05 19:33:29 +00:00
|
|
|
|
2018-10-19 01:42:30 +00:00
|
|
|
def execute(access_token: nil)
|
|
|
|
raise ArgumentError, 'Unknown clusterable provided' unless clusterable
|
2017-11-18 19:18:42 +00:00
|
|
|
|
2019-08-07 04:40:29 +00:00
|
|
|
cluster_params = params.merge(global_params).merge(clusterable_params)
|
|
|
|
|
2018-10-10 23:15:04 +00:00
|
|
|
cluster_params[:provider_gcp_attributes].try do |provider|
|
|
|
|
provider[:access_token] = access_token
|
|
|
|
end
|
|
|
|
|
2019-07-10 05:33:09 +00:00
|
|
|
cluster = Clusters::Cluster.new(cluster_params)
|
|
|
|
|
|
|
|
unless can_create_cluster?
|
|
|
|
cluster.errors.add(:base, _('Instance does not support multiple Kubernetes clusters'))
|
2017-10-03 14:44:06 +00:00
|
|
|
end
|
2017-10-23 08:36:35 +00:00
|
|
|
|
2020-04-14 15:09:44 +00:00
|
|
|
validate_management_project_permissions(cluster)
|
|
|
|
|
2019-07-10 05:33:09 +00:00
|
|
|
return cluster if cluster.errors.present?
|
2017-10-23 08:36:35 +00:00
|
|
|
|
2019-07-10 05:33:09 +00:00
|
|
|
cluster.tap do |cluster|
|
|
|
|
cluster.save && ClusterProvisionWorker.perform_async(cluster.id)
|
|
|
|
end
|
2017-10-23 08:36:35 +00:00
|
|
|
end
|
|
|
|
|
2019-07-10 05:33:09 +00:00
|
|
|
private
|
|
|
|
|
2018-10-19 01:42:30 +00:00
|
|
|
def clusterable
|
|
|
|
@clusterable ||= params.delete(:clusterable)
|
|
|
|
end
|
|
|
|
|
2019-08-07 04:40:29 +00:00
|
|
|
def global_params
|
2019-08-23 06:27:41 +00:00
|
|
|
{ user: current_user }
|
2019-08-07 04:40:29 +00:00
|
|
|
end
|
|
|
|
|
2018-10-19 01:42:30 +00:00
|
|
|
def clusterable_params
|
|
|
|
case clusterable
|
|
|
|
when ::Project
|
|
|
|
{ cluster_type: :project_type, projects: [clusterable] }
|
2018-10-23 03:51:29 +00:00
|
|
|
when ::Group
|
|
|
|
{ cluster_type: :group_type, groups: [clusterable] }
|
2019-04-10 02:13:43 +00:00
|
|
|
when Instance
|
|
|
|
{ cluster_type: :instance_type }
|
2018-11-06 23:36:58 +00:00
|
|
|
else
|
|
|
|
raise NotImplementedError
|
2018-10-19 01:42:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-10 23:15:04 +00:00
|
|
|
# EE would override this method
|
2018-10-19 01:42:30 +00:00
|
|
|
def can_create_cluster?
|
|
|
|
clusterable.clusters.empty?
|
2017-11-18 19:18:42 +00:00
|
|
|
end
|
2020-04-14 15:09:44 +00:00
|
|
|
|
|
|
|
def validate_management_project_permissions(cluster)
|
|
|
|
Clusters::Management::ValidateManagementProjectPermissionsService.new(current_user)
|
|
|
|
.execute(cluster, params[:management_project_id])
|
|
|
|
end
|
2017-09-28 09:11:17 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Clusters::CreateService.prepend_if_ee('EE::Clusters::CreateService')
|