debian-mirror-gitlab/lib/api/clusters/agent_tokens.rb

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

100 lines
3.8 KiB
Ruby
Raw Normal View History

2022-07-16 23:28:13 +05:30
# frozen_string_literal: true
module API
module Clusters
class AgentTokens < ::API::Base
include PaginationParams
before { authenticate! }
feature_category :kubernetes_management
params do
2023-01-13 00:05:48 +05:30
requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
2022-07-16 23:28:13 +05:30
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
params do
requires :agent_id, type: Integer, desc: 'The ID of an agent'
end
resource ':id/cluster_agents/:agent_id' do
resource :tokens do
2023-01-13 00:05:48 +05:30
desc 'List tokens for an agent' do
detail 'This feature was introduced in GitLab 15.0. Returns a list of tokens for an agent.'
2022-07-16 23:28:13 +05:30
success Entities::Clusters::AgentTokenBasic
2023-01-13 00:05:48 +05:30
tags %w[cluster_agents]
2022-07-16 23:28:13 +05:30
end
params do
use :pagination
end
get do
2023-03-04 22:38:38 +05:30
agent = ::Clusters::AgentsFinder.new(user_project, current_user).find(params[:agent_id])
agent_tokens = ::Clusters::AgentTokensFinder.new(agent, current_user).execute
2022-07-16 23:28:13 +05:30
2023-01-13 00:05:48 +05:30
present paginate(agent_tokens), with: Entities::Clusters::AgentTokenBasic
2022-07-16 23:28:13 +05:30
end
desc 'Get a single agent token' do
2023-01-13 00:05:48 +05:30
detail 'This feature was introduced in GitLab 15.0. Gets a single agent token.'
2022-07-16 23:28:13 +05:30
success Entities::Clusters::AgentToken
2023-01-13 00:05:48 +05:30
tags %w[cluster_agents]
2022-07-16 23:28:13 +05:30
end
params do
requires :token_id, type: Integer, desc: 'The ID of the agent token'
end
get ':token_id' do
2022-07-23 23:45:48 +05:30
agent = ::Clusters::AgentsFinder.new(user_project, current_user).find(params[:agent_id])
2023-03-04 22:38:38 +05:30
token = ::Clusters::AgentTokensFinder.new(agent, current_user).find(params[:token_id])
2022-07-16 23:28:13 +05:30
present token, with: Entities::Clusters::AgentToken
end
desc 'Create an agent token' do
2023-01-13 00:05:48 +05:30
detail 'This feature was introduced in GitLab 15.0. Creates a new token for an agent.'
2022-07-16 23:28:13 +05:30
success Entities::Clusters::AgentTokenWithToken
2023-01-13 00:05:48 +05:30
tags %w[cluster_agents]
2022-07-16 23:28:13 +05:30
end
params do
requires :name, type: String, desc: 'The name for the token'
optional :description, type: String, desc: 'The description for the token'
end
post do
authorize! :create_cluster, user_project
token_params = declared_params(include_missing: false)
2022-07-23 23:45:48 +05:30
agent = ::Clusters::AgentsFinder.new(user_project, current_user).find(params[:agent_id])
2022-07-16 23:28:13 +05:30
result = ::Clusters::AgentTokens::CreateService.new(
container: agent.project, current_user: current_user, params: token_params.merge(agent_id: agent.id)
).execute
bad_request!(result[:message]) if result[:status] == :error
present result[:token], with: Entities::Clusters::AgentTokenWithToken
end
desc 'Revoke an agent token' do
2023-01-13 00:05:48 +05:30
detail 'This feature was introduced in GitLab 15.0. Revokes an agent token.'
tags %w[cluster_agents]
2022-07-16 23:28:13 +05:30
end
params do
requires :token_id, type: Integer, desc: 'The ID of the agent token'
end
delete ':token_id' do
authorize! :admin_cluster, user_project
2022-07-23 23:45:48 +05:30
agent = ::Clusters::AgentsFinder.new(user_project, current_user).find(params[:agent_id])
2023-03-04 22:38:38 +05:30
token = ::Clusters::AgentTokensFinder.new(agent, current_user).find(params[:token_id])
2022-07-16 23:28:13 +05:30
# Skipping explicit error handling and relying on exceptions
token.revoked!
status :no_content
end
end
end
end
end
end
end