debian-mirror-gitlab/app/services/clusters/agent_tokens/create_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.4 KiB
Ruby
Raw Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
module Clusters
module AgentTokens
2023-05-27 22:25:52 +05:30
class CreateService
2021-11-18 22:05:49 +05:30
ALLOWED_PARAMS = %i[agent_id description name].freeze
2023-05-27 22:25:52 +05:30
attr_reader :agent, :current_user, :params
def initialize(agent:, current_user:, params:)
@agent = agent
@current_user = current_user
@params = params
end
2021-11-18 22:05:49 +05:30
def execute
2023-05-27 22:25:52 +05:30
return error_no_permissions unless current_user.can?(:create_cluster, agent.project)
2021-11-18 22:05:49 +05:30
2023-05-27 22:25:52 +05:30
token = ::Clusters::AgentToken.new(filtered_params.merge(agent_id: agent.id, created_by_user: current_user))
2021-11-18 22:05:49 +05:30
if token.save
2023-05-27 22:25:52 +05:30
log_activity_event(token)
2022-01-26 12:08:38 +05:30
2021-11-18 22:05:49 +05:30
ServiceResponse.success(payload: { secret: token.token, token: token })
else
ServiceResponse.error(message: token.errors.full_messages)
end
end
private
def error_no_permissions
ServiceResponse.error(message: s_('ClusterAgent|User has insufficient permissions to create a token for this project'))
end
def filtered_params
params.slice(*ALLOWED_PARAMS)
end
2022-01-26 12:08:38 +05:30
2023-05-27 22:25:52 +05:30
def log_activity_event(token)
2022-03-02 08:16:31 +05:30
Clusters::Agents::CreateActivityEventService.new(
token.agent,
2022-01-26 12:08:38 +05:30
kind: :token_created,
level: :info,
recorded_at: token.created_at,
user: current_user,
agent_token: token
2022-03-02 08:16:31 +05:30
).execute
2022-01-26 12:08:38 +05:30
end
2021-11-18 22:05:49 +05:30
end
end
end
2023-05-27 22:25:52 +05:30
Clusters::AgentTokens::CreateService.prepend_mod