2023-04-23 21:23:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
module Runners
|
|
|
|
class CreateRunnerService
|
|
|
|
RUNNER_CLASS_MAPPING = {
|
|
|
|
'instance_type' => Ci::Runners::RunnerCreationStrategies::InstanceRunnerStrategy,
|
2023-06-20 00:43:36 +05:30
|
|
|
'group_type' => Ci::Runners::RunnerCreationStrategies::GroupRunnerStrategy,
|
|
|
|
'project_type' => Ci::Runners::RunnerCreationStrategies::ProjectRunnerStrategy
|
2023-04-23 21:23:45 +05:30
|
|
|
}.freeze
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
def initialize(user:, params:)
|
2023-04-23 21:23:45 +05:30
|
|
|
@user = user
|
|
|
|
@params = params
|
2023-06-20 00:43:36 +05:30
|
|
|
@strategy = RUNNER_CLASS_MAPPING[params[:runner_type]].new(user: user, params: params)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
normalize_params
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
error = strategy.validate_params
|
|
|
|
return ServiceResponse.error(message: error, reason: :validation_error) if error
|
|
|
|
|
|
|
|
unless strategy.authorized_user?
|
|
|
|
return ServiceResponse.error(message: _('Insufficient permissions'), reason: :forbidden)
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
runner = ::Ci::Runner.new(params)
|
|
|
|
|
|
|
|
return ServiceResponse.success(payload: { runner: runner }) if runner.save
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
ServiceResponse.error(message: runner.errors.full_messages, reason: :save_error)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_params
|
|
|
|
params[:registration_type] = :authenticated_user
|
2023-05-27 22:25:52 +05:30
|
|
|
params[:active] = !params.delete(:paused) if params.key?(:paused)
|
2023-04-23 21:23:45 +05:30
|
|
|
params[:creator] = user
|
|
|
|
|
|
|
|
strategy.normalize_params
|
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :params, :strategy
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|