Class: Clusters::CreateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/clusters/create_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, params = {}) ⇒ CreateService

Returns a new instance of CreateService.


7
8
9
# File 'app/services/clusters/create_service.rb', line 7

def initialize(user = nil, params = {})
  @current_user, @params = user, params.dup
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user


5
6
7
# File 'app/services/clusters/create_service.rb', line 5

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params


5
6
7
# File 'app/services/clusters/create_service.rb', line 5

def params
  @params
end

Instance Method Details

#execute(access_token: nil) ⇒ Object

Raises:

  • (ArgumentError)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/clusters/create_service.rb', line 11

def execute(access_token: nil)
  raise ArgumentError, 'Unknown clusterable provided' unless clusterable

  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)

  validate_management_project_permissions(cluster)

  return cluster if cluster.errors.present?

  cluster.tap do |cluster|
    cluster.save && ClusterProvisionWorker.perform_async(cluster.id)
  end
end