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-10 19:15:04 -04:00
|
|
|
def execute(project:, access_token: nil)
|
|
|
|
raise ArgumentError.new(_('Instance does not support multiple Kubernetes clusters')) unless can_create_cluster?(project)
|
2017-11-18 14:18:42 -05:00
|
|
|
|
2018-10-10 19:15:04 -04:00
|
|
|
cluster_params = params.merge(user: current_user, projects: [project])
|
|
|
|
cluster_params[:provider_gcp_attributes].try do |provider|
|
|
|
|
provider[:access_token] = access_token
|
|
|
|
end
|
|
|
|
|
|
|
|
create_cluster(cluster_params).tap do |cluster|
|
2017-10-03 17:24:32 -04:00
|
|
|
ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted?
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
2017-09-28 05:11:17 -04:00
|
|
|
end
|
2017-10-23 04:36:35 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-10-10 19:15:04 -04:00
|
|
|
def create_cluster(cluster_params)
|
2017-11-02 10:19:11 -04:00
|
|
|
Clusters::Cluster.create(cluster_params)
|
2017-10-23 04:36:35 -04:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:15:04 -04:00
|
|
|
# EE would override this method
|
|
|
|
def can_create_cluster?(project)
|
2017-12-05 07:48:55 -05:00
|
|
|
project.clusters.empty?
|
2017-11-18 14:18:42 -05:00
|
|
|
end
|
2017-09-28 05:11:17 -04:00
|
|
|
end
|
|
|
|
end
|