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