Commit 8e45ef80 authored by Tiger's avatar Tiger

Move cluster agent GraphQL mutations and supporting services to core

Changelog: added
parent 9b64bd06
......@@ -31,6 +31,10 @@ module Types
mount_mutation Mutations::Boards::Lists::Update
mount_mutation Mutations::Boards::Lists::Destroy
mount_mutation Mutations::Branches::Create, calls_gitaly: true
mount_mutation Mutations::Clusters::Agents::Create
mount_mutation Mutations::Clusters::Agents::Delete
mount_mutation Mutations::Clusters::AgentTokens::Create
mount_mutation Mutations::Clusters::AgentTokens::Delete
mount_mutation Mutations::Commits::Create, calls_gitaly: true
mount_mutation Mutations::CustomEmoji::Create, feature_flag: :custom_emoji
mount_mutation Mutations::CustomEmoji::Destroy, feature_flag: :custom_emoji
......
......@@ -6,7 +6,6 @@ module Clusters
ALLOWED_PARAMS = %i[agent_id description name].freeze
def execute
return error_feature_not_available unless container.feature_available?(:cluster_agents)
return error_no_permissions unless current_user.can?(:create_cluster, container)
token = ::Clusters::AgentToken.new(filtered_params.merge(created_by_user: current_user))
......@@ -20,10 +19,6 @@ module Clusters
private
def error_feature_not_available
ServiceResponse.error(message: s_('ClusterAgent|This feature is only available for premium plans'))
end
def error_no_permissions
ServiceResponse.error(message: s_('ClusterAgent|User has insufficient permissions to create a token for this project'))
end
......
......@@ -4,7 +4,6 @@ module Clusters
module Agents
class CreateService < BaseService
def execute(name:)
return error_not_premium_plan unless project.feature_available?(:cluster_agents)
return error_no_permissions unless cluster_agent_permissions?
agent = ::Clusters::Agent.new(name: name, project: project, created_by_user: current_user)
......@@ -25,10 +24,6 @@ module Clusters
def error_no_permissions
error(s_('ClusterAgent|You have insufficient permissions to create a cluster agent for this project'))
end
def error_not_premium_plan
error(s_('ClusterAgent|This feature is only available for premium plans'))
end
end
end
end
......@@ -298,6 +298,8 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
end
resources :cluster_agents, only: [:show], param: :name
concerns :clusterable
namespace :serverless do
......
......@@ -6,10 +6,6 @@ module EE
extend ActiveSupport::Concern
prepended do
mount_mutation ::Mutations::Clusters::Agents::Create
mount_mutation ::Mutations::Clusters::Agents::Delete
mount_mutation ::Mutations::Clusters::AgentTokens::Create
mount_mutation ::Mutations::Clusters::AgentTokens::Delete
mount_mutation ::Mutations::ComplianceManagement::Frameworks::Destroy
mount_mutation ::Mutations::ComplianceManagement::Frameworks::Update
mount_mutation ::Mutations::ComplianceManagement::Frameworks::Create
......
......@@ -136,8 +136,6 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
resources :oncall_schedules, only: [:index], path: 'oncall_schedules'
resources :escalation_policies, only: [:index], path: 'escalation_policies'
end
resources :cluster_agents, only: [:show], param: :name
end
# End of the /-/ scope.
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Clusters::AgentTokens::CreateService do
subject(:service) { described_class.new(container: project, current_user: user, params: params) }
let_it_be(:user) { create(:user) }
let(:cluster_agent) { create(:cluster_agent) }
let(:project) { cluster_agent.project }
let(:params) { { agent_id: cluster_agent.id, description: 'token description', name: 'token name' } }
before do
stub_licensed_features(cluster_agents: false)
end
describe '#execute' do
subject { service.execute }
context 'without premium plan' do
it 'does not create a new token' do
expect { subject }.not_to change(Clusters::AgentToken, :count)
end
it 'returns missing license error' do
expect(subject.status).to eq(:error)
expect(subject.message).to eq('This feature is only available for premium plans')
end
context 'with premium plan' do
before do
stub_licensed_features(cluster_agents: true)
end
it 'does not create a new token due to user permissions' do
expect { subject }.not_to change(::Clusters::AgentToken, :count)
end
it 'returns permission errors', :aggregate_failures do
expect(subject.status).to eq(:error)
expect(subject.message).to eq('User has insufficient permissions to create a token for this project')
end
context 'with user permissions' do
before do
project.add_maintainer(user)
end
it 'creates a new token' do
expect { subject }.to change { ::Clusters::AgentToken.count }.by(1)
end
it 'returns success status', :aggregate_failures do
expect(subject.status).to eq(:success)
expect(subject.message).to be_nil
end
it 'returns token information', :aggregate_failures do
token = subject.payload[:token]
expect(subject.payload[:secret]).not_to be_nil
expect(token.created_by_user).to eq(user)
expect(token.description).to eq(params[:description])
expect(token.name).to eq(params[:name])
end
context 'when params are invalid' do
let(:params) { { agent_id: 'bad_id' } }
it 'does not create a new token' do
expect { subject }.not_to change(::Clusters::AgentToken, :count)
end
it 'returns validation errors', :aggregate_failures do
expect(subject.status).to eq(:error)
expect(subject.message).to eq(["Agent must exist", "Name can't be blank"])
end
end
end
end
end
end
end
......@@ -7321,9 +7321,6 @@ msgstr ""
msgid "ClusterAgents|You will need to create a token to connect to your agent"
msgstr ""
msgid "ClusterAgent|This feature is only available for premium plans"
msgstr ""
msgid "ClusterAgent|User has insufficient permissions to create a token for this project"
msgstr ""
......
......@@ -30,19 +30,8 @@ RSpec.describe Mutations::Clusters::AgentTokens::Create do
end
end
context 'without premium plan' do
context 'with user permissions' do
before do
stub_licensed_features(cluster_agents: false)
cluster_agent.project.add_maintainer(user)
end
it { expect(subject[:secret]).to be_nil }
it { expect(subject[:errors]).to eq(['This feature is only available for premium plans']) }
end
context 'with premium plan and user permissions' do
before do
stub_licensed_features(cluster_agents: true)
cluster_agent.project.add_maintainer(user)
end
......
......@@ -26,19 +26,8 @@ RSpec.describe Mutations::Clusters::Agents::Create do
end
end
context 'without premium plan' do
context 'with user permissions' do
before do
allow(License).to receive(:current).and_return(create(:license, plan: ::License::STARTER_PLAN))
project.add_maintainer(user)
end
it { expect(subject[:clusters_agent]).to be_nil }
it { expect(subject[:errors]).to eq(['This feature is only available for premium plans']) }
end
context 'with premium plan and user permissions' do
before do
allow(License).to receive(:current).and_return(create(:license, plan: ::License::PREMIUM_PLAN))
project.add_maintainer(user)
end
......
......@@ -31,21 +31,8 @@ RSpec.describe 'Create a new cluster agent token' do
end
end
context 'without premium plan' do
before do
stub_licensed_features(cluster_agents: false)
cluster_agent.project.add_maintainer(current_user)
end
it 'does not create a token and returns error message', :aggregate_failures do
expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Clusters::AgentToken, :count)
expect(mutation_response['errors']).to eq(['This feature is only available for premium plans'])
end
end
context 'with project permissions' do
before do
stub_licensed_features(cluster_agents: true)
cluster_agent.project.add_maintainer(current_user)
end
......
......@@ -28,21 +28,8 @@ RSpec.describe 'Create a new cluster agent' do
end
end
context 'without premium plan' do
context 'with user permissions' do
before do
allow(License).to receive(:current).and_return(create(:license, plan: ::License::STARTER_PLAN))
project.add_maintainer(current_user)
end
it 'does not create cluster agent and returns error message', :aggregate_failures do
expect { post_graphql_mutation(mutation, current_user: current_user) }.not_to change(Clusters::Agent, :count)
expect(mutation_response['errors']).to eq(['This feature is only available for premium plans'])
end
end
context 'with premium plan and user permissions' do
before do
allow(License).to receive(:current).and_return(create(:license, plan: ::License::PREMIUM_PLAN))
project.add_maintainer(current_user)
end
......
......@@ -30,9 +30,8 @@ RSpec.describe 'Delete a cluster agent' do
end
end
context 'with premium plan and project permissions' do
context 'with project permissions' do
before do
allow(License).to receive(:current).and_return(create(:license, plan: ::License::PREMIUM_PLAN))
project.add_maintainer(current_user)
end
......
......@@ -23,7 +23,6 @@ RSpec.describe 'Project.cluster_agents' do
before do
allow(Gitlab::Kas::Client).to receive(:new).and_return(double(get_connected_agents: []))
stub_licensed_features(cluster_agents: true)
end
it 'can retrieve cluster agents' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Clusters::AgentTokens::CreateService do
subject(:service) { described_class.new(container: project, current_user: user, params: params) }
let_it_be(:user) { create(:user) }
let(:cluster_agent) { create(:cluster_agent) }
let(:project) { cluster_agent.project }
let(:params) { { agent_id: cluster_agent.id, description: 'token description', name: 'token name' } }
describe '#execute' do
subject { service.execute }
it 'does not create a new token due to user permissions' do
expect { subject }.not_to change(::Clusters::AgentToken, :count)
end
it 'returns permission errors', :aggregate_failures do
expect(subject.status).to eq(:error)
expect(subject.message).to eq('User has insufficient permissions to create a token for this project')
end
context 'with user permissions' do
before do
project.add_maintainer(user)
end
it 'creates a new token' do
expect { subject }.to change { ::Clusters::AgentToken.count }.by(1)
end
it 'returns success status', :aggregate_failures do
expect(subject.status).to eq(:success)
expect(subject.message).to be_nil
end
it 'returns token information', :aggregate_failures do
token = subject.payload[:token]
expect(subject.payload[:secret]).not_to be_nil
expect(token.created_by_user).to eq(user)
expect(token.description).to eq(params[:description])
expect(token.name).to eq(params[:name])
end
context 'when params are invalid' do
let(:params) { { agent_id: 'bad_id' } }
it 'does not create a new token' do
expect { subject }.not_to change(::Clusters::AgentToken, :count)
end
it 'returns validation errors', :aggregate_failures do
expect(subject.status).to eq(:error)
expect(subject.message).to eq(["Agent must exist", "Name can't be blank"])
end
end
end
end
end
......@@ -7,27 +7,9 @@ RSpec.describe Clusters::Agents::CreateService do
let(:project) { create(:project, :public, :repository) }
let(:user) { create(:user) }
let(:license) { create(:license, plan: ::License::PREMIUM_PLAN) }
describe '#execute' do
context 'without premium plan' do
before do
allow(License).to receive(:current).and_return(create(:license, plan: ::License::STARTER_PLAN))
end
it 'returns missing plan error' do
expect(service.execute(name: 'without-license')).to eq({
status: :error,
message: 'This feature is only available for premium plans'
})
end
end
context 'without user permissions' do
before do
allow(License).to receive(:current).and_return(license)
end
it 'returns errors when user does not have permissions' do
expect(service.execute(name: 'missing-permissions')).to eq({
status: :error,
......@@ -36,14 +18,13 @@ RSpec.describe Clusters::Agents::CreateService do
end
end
context 'with premium plan and user permissions' do
context 'with user permissions' do
before do
allow(License).to receive(:current).and_return(license)
project.add_maintainer(user)
end
it 'creates a new clusters_agent' do
expect { service.execute(name: 'with-license-and-user') }.to change { ::Clusters::Agent.count }.by(1)
expect { service.execute(name: 'with-user') }.to change { ::Clusters::Agent.count }.by(1)
end
it 'returns success status', :aggregate_failures do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment