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,
|
|
|
|
nil => Ci::Runners::RunnerCreationStrategies::InstanceRunnerStrategy
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
attr_accessor :user, :type, :params, :strategy
|
|
|
|
|
|
|
|
def initialize(user:, type:, params:)
|
|
|
|
@user = user
|
|
|
|
@type = type
|
|
|
|
@params = params
|
|
|
|
@strategy = RUNNER_CLASS_MAPPING[type].new(user: user, type: type, params: params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
normalize_params
|
|
|
|
|
|
|
|
return ServiceResponse.error(message: 'Validation error') unless strategy.validate_params
|
|
|
|
return ServiceResponse.error(message: 'Insufficient permissions') unless strategy.authorized_user?
|
|
|
|
|
|
|
|
runner = ::Ci::Runner.new(params)
|
|
|
|
|
|
|
|
return ServiceResponse.success(payload: { runner: runner }) if runner.save
|
|
|
|
|
|
|
|
ServiceResponse.error(message: runner.errors.full_messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_params
|
|
|
|
params[:registration_type] = :authenticated_user
|
|
|
|
params[:runner_type] = type
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|