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

36 lines
964 B
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(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
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|
ClusterProvisionWorker.perform_async(cluster.id) if cluster.persisted?
end
end
2017-10-23 04:36:35 -04:00
private
def create_cluster(cluster_params)
Clusters::Cluster.create(cluster_params)
2017-10-23 04:36:35 -04:00
end
# EE would override this method
def can_create_cluster?(project)
project.clusters.empty?
2017-11-18 14:18:42 -05:00
end
end
end