debian-mirror-gitlab/app/services/clusters/create_service.rb

57 lines
1.4 KiB
Ruby
Raw Normal View History

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
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
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
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
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
end
end