Class: Clusters::AgentTokens::CreateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/clusters/agent_tokens/create_service.rb

Constant Summary collapse

ALLOWED_PARAMS =
%i[agent_id description name].freeze
ACTIVE_TOKENS_LIMIT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, current_user:, params:) ⇒ CreateService

Returns a new instance of CreateService.



11
12
13
14
15
# File 'app/services/clusters/agent_tokens/create_service.rb', line 11

def initialize(agent:, current_user:, params:)
  @agent = agent
  @current_user = current_user
  @params = params
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



9
10
11
# File 'app/services/clusters/agent_tokens/create_service.rb', line 9

def agent
  @agent
end

#current_userObject (readonly)

Returns the value of attribute current_user.



9
10
11
# File 'app/services/clusters/agent_tokens/create_service.rb', line 9

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'app/services/clusters/agent_tokens/create_service.rb', line 9

def params
  @params
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/clusters/agent_tokens/create_service.rb', line 17

def execute
  return error_no_permissions unless current_user.can?(:create_cluster, agent.project)
  return error_active_tokens_limit_reached if active_tokens_limit_reached?

  token = ::Clusters::AgentToken.new(filtered_params.merge(agent_id: agent.id, 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