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