Skip to content
Snippets Groups Projects
Verified Commit 4e2ef312 authored by Timo Furrer's avatar Timo Furrer :juggling:
Browse files

Implement delete service for agent url configuration

parent 5b7f9cf1
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !160989. Comments created here will be created in the context of that merge request.
# frozen_string_literal: true
module Clusters
module Agents
class DeleteUrlConfigurationService
attr_reader :agent, :current_user, :url_configuration
def initialize(agent:, current_user:, url_configuration:)
@agent = agent
@current_user = current_user
@url_configuration = url_configuration
end
def execute
return error_no_permissions unless current_user.can?(:admin_cluster, agent)
return error_receptive_agents_disabled unless receptive_agents_enabled?
if url_configuration.destroy
ServiceResponse.success
else
ServiceResponse.error(message: url_configuration.errors.full_messages)
end
end
private
delegate :project, to: :agent
def error_no_permissions
ServiceResponse.error(
message: s_('ClusterAgent|You have insufficient permissions to delete this agent url configuration'))
end
def receptive_agents_enabled?
Feature.enabled?(:cluster_agent_incoming_connections, project)
end
def error_receptive_agents_disabled
ServiceResponse.error(
message: s_('ClusterAgent|Receptive agents are disabled for this GitLab instance')
)
end
end
end
end
Clusters::Agents::DeleteUrlConfigurationService.prepend_mod
......@@ -12573,6 +12573,9 @@ msgstr ""
msgid "ClusterAgent|You have insufficient permissions to create an url configuration for this agent"
msgstr ""
 
msgid "ClusterAgent|You have insufficient permissions to delete this agent url configuration"
msgstr ""
msgid "ClusterAgent|You have insufficient permissions to delete this cluster agent"
msgstr ""
 
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Clusters::Agents::DeleteUrlConfigurationService, feature_category: :deployment_management do
describe '#execute' do
let(:agent) { create(:cluster_agent, is_receptive: true) }
let(:project) { agent.project }
let(:user) { create(:user) }
let!(:url_configuration) { create(:cluster_agent_url_configuration, agent: agent, created_by_user: user) }
subject(:service) { described_class.new(agent: agent, current_user: user, url_configuration: url_configuration) }
context 'without user permissions' do
it 'fails to delete when the user has no permissions', :aggregate_failures do
response = service.execute
expect(response.status).to eq(:error)
expect(response.message).to eq('You have insufficient permissions to delete this agent url configuration')
expect { url_configuration.reload }.not_to raise_error
end
end
context 'with user permissions' do
before do
project.add_maintainer(user)
end
it 'deletes a agent url configuration', :aggregate_failures do
expect { service.execute }.to change { ::Clusters::Agents::UrlConfiguration.count }.by(-1)
expect { url_configuration.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(agent.is_receptive).to be(false)
end
context 'when receptive agents are disabled' do
before do
stub_feature_flags(cluster_agent_incoming_connections: false)
end
it 'returns an error', :aggregate_failures do
response = service.execute
expect(response.status).to eq(:error)
expect(response.message).to eq('Receptive agents are disabled for this GitLab instance')
end
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment