gitlab-org--gitlab-foss/app/services/clusters/create_service.rb

71 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Clusters
class CreateService
attr_reader :current_user, :params
def initialize(user = nil, params = {})
@current_user, @params = user, params.dup
end
def execute(access_token: nil)
raise ArgumentError, 'Unknown clusterable provided' unless clusterable
2017-11-18 14:18:42 -05:00
cluster_params = params.merge(global_params).merge(clusterable_params)
cluster_params[:provider_gcp_attributes].try do |provider|
provider[:access_token] = access_token
end
cluster = Clusters::Cluster.new(cluster_params)
unless can_create_cluster?
cluster.errors.add(:base, _('Instance does not support multiple Kubernetes clusters'))
end
2017-10-23 04:36:35 -04:00
validate_management_project_permissions(cluster)
return cluster if cluster.errors.present?
2017-10-23 04:36:35 -04:00
cluster.tap do |cluster|
cluster.save && ClusterProvisionWorker.perform_async(cluster.id)
end
2017-10-23 04:36:35 -04:00
end
private
def clusterable
@clusterable ||= params.delete(:clusterable)
end
def global_params
{ user: current_user }
end
def clusterable_params
case clusterable
when ::Project
{ cluster_type: :project_type, projects: [clusterable] }
when ::Group
{ cluster_type: :group_type, groups: [clusterable] }
when Instance
{ cluster_type: :instance_type }
2018-11-06 18:36:58 -05:00
else
raise NotImplementedError
end
end
# EE would override this method
def can_create_cluster?
clusterable.clusters.empty?
2017-11-18 14:18:42 -05:00
end
def validate_management_project_permissions(cluster)
Clusters::Management::ValidateManagementProjectPermissionsService.new(current_user)
.execute(cluster, params[:management_project_id])
end
end
end
Clusters::CreateService.prepend_if_ee('EE::Clusters::CreateService')