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
|
|
|
|
raise ArgumentError, _('Instance does not support multiple Kubernetes clusters') unless can_create_cluster?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
cluster_params = params.merge(user: current_user).merge(clusterable_params)
|
|
|
|
cluster_params[:provider_gcp_attributes].try do |provider|
|
|
|
|
provider[:access_token] = access_token
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
create_cluster(cluster_params).tap do |cluster|
|
2018-03-17 18:26:18 +05:30
|
|
|
ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def create_cluster(cluster_params)
|
2018-03-17 18:26:18 +05:30
|
|
|
Clusters::Cluster.create(cluster_params)
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def clusterable
|
|
|
|
@clusterable ||= params.delete(:clusterable)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def clusterable_params
|
|
|
|
case clusterable
|
|
|
|
when ::Project
|
|
|
|
{ cluster_type: :project_type, projects: [clusterable] }
|
2019-02-13 22:33:31 +05:30
|
|
|
when ::Group
|
|
|
|
{ cluster_type: :group_type, groups: [clusterable] }
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|