diff options
Diffstat (limited to 'app/services/clusters/agent_tokens/create_service.rb')
-rw-r--r-- | app/services/clusters/agent_tokens/create_service.rb | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/app/services/clusters/agent_tokens/create_service.rb b/app/services/clusters/agent_tokens/create_service.rb index 2539ffdc5ba..66a3cb04d98 100644 --- a/app/services/clusters/agent_tokens/create_service.rb +++ b/app/services/clusters/agent_tokens/create_service.rb @@ -2,16 +2,24 @@ module Clusters module AgentTokens - class CreateService < ::BaseContainerService + class CreateService ALLOWED_PARAMS = %i[agent_id description name].freeze + attr_reader :agent, :current_user, :params + + def initialize(agent:, current_user:, params:) + @agent = agent + @current_user = current_user + @params = params + end + def execute - return error_no_permissions unless current_user.can?(:create_cluster, container) + return error_no_permissions unless current_user.can?(:create_cluster, agent.project) - token = ::Clusters::AgentToken.new(filtered_params.merge(created_by_user: current_user)) + token = ::Clusters::AgentToken.new(filtered_params.merge(agent_id: agent.id, created_by_user: current_user)) if token.save - log_activity_event!(token) + log_activity_event(token) ServiceResponse.success(payload: { secret: token.token, token: token }) else @@ -29,7 +37,7 @@ module Clusters params.slice(*ALLOWED_PARAMS) end - def log_activity_event!(token) + def log_activity_event(token) Clusters::Agents::CreateActivityEventService.new( token.agent, kind: :token_created, @@ -42,3 +50,5 @@ module Clusters end end end + +Clusters::AgentTokens::CreateService.prepend_mod |