Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_service.rb « agent_tokens « clusters « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b8a0e46a6cb0db57ab79c80ffea2bcab36ef8b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

module Clusters
  module AgentTokens
    class CreateService < ::BaseContainerService
      ALLOWED_PARAMS = %i[agent_id description name].freeze

      def execute
        return error_no_permissions unless current_user.can?(:create_cluster, container)

        token = ::Clusters::AgentToken.new(filtered_params.merge(created_by_user: current_user))

        if token.save
          log_activity_event!(token)

          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

      def log_activity_event!(token)
        token.agent.activity_events.create!(
          kind: :token_created,
          level: :info,
          recorded_at: token.created_at,
          user: current_user,
          agent_token: token
        )
      end
    end
  end
end