Commit a367e65e authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'mk/add-package-file-registry-finder' into 'master'

Geo: Add PackageFileRegistryFinder to prepare for GraphQL API work

See merge request gitlab-org/gitlab!30137
parents e1ad2a4e 1adacde9
# frozen_string_literal: true
module Geo
# Used to provide registry data for GraphQL queries.
#
module FrameworkRegistryFinder
extend ActiveSupport::Concern
included do
include Gitlab::Allowable
delegate :registry_class, to: :replicator_class
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
return registry_class.none unless can?(current_user, :read_all_geo)
registry_entries = init_collection
registry_entries = by_id(registry_entries)
registry_entries.ordered
end
private
attr_reader :current_user, :params
def replicator_class
Gitlab::Geo::Replicator.for_class_name(self.class.name)
end
def init_collection
registry_class.all
end
def by_id(registry_entries)
return registry_entries if params[:ids].nil?
return registry_class.none if params[:ids].empty?
registry_entries.id_in(params[:ids])
end
end
end
end
# frozen_string_literal: true
module Geo
class PackageFileRegistryFinder
include FrameworkRegistryFinder
end
end
......@@ -16,6 +16,7 @@ class Geo::PackageFileRegistry < Geo::BaseRegistry
scope :failed, -> { with_state(:failed) }
scope :synced, -> { with_state(:synced) }
scope :retry_due, -> { where(arel_table[:retry_at].eq(nil).or(arel_table[:retry_at].lt(Time.now))) }
scope :ordered, -> { order(:id) }
state_machine :state, initial: :pending do
state :pending, value: STATE_VALUES[:pending]
......
......@@ -12,6 +12,8 @@ module Gitlab
class Replicator
include ::Gitlab::Geo::LogHelpers
CLASS_SUFFIXES = %w(RegistryFinder RegistriesResolver).freeze
attr_reader :model_record_id
delegate :model, to: :class
......@@ -63,6 +65,10 @@ module Gitlab
const_get("::Geo::#{replicable_name.camelize}Registry", false)
end
def self.registry_finder_class
const_get("::Geo::#{replicable_name.camelize}RegistryFinder", false)
end
# Given a `replicable_name`, return the corresponding replicator
#
# @param [String] replicable_name the replicable slug
......@@ -89,6 +95,21 @@ module Gitlab
model.count
end
# @example Given `Geo::PackageFileRegistryFinder`, this returns
# `::Geo::PackageFileReplicator`
# @example Given `Resolver::Geo::PackageFileRegistriesResolver`, this
# returns `::Geo::PackageFileReplicator`
#
# @return [Class] a Replicator subclass
def self.for_class_name(class_name)
name = class_name.demodulize
# Strip suffixes is dumb but will probably work for a while
CLASS_SUFFIXES.each { |suffix| name.delete_suffix!(suffix) }
const_get("::Geo::#{name}Replicator", false)
end
# @param [ActiveRecord::Base] model_record
# @param [Integer] model_record_id
def initialize(model_record: nil, model_record_id: nil)
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::PackageFileRegistryFinder do
it_behaves_like 'a framework registry finder', :package_file_registry
end
......@@ -22,9 +22,9 @@ describe Gitlab::Geo::Replicator do
context 'with defined events' do
before do
stub_const('DummyReplicator', Class.new(Gitlab::Geo::Replicator))
stub_const('Geo::DummyReplicator', Class.new(Gitlab::Geo::Replicator))
DummyReplicator.class_eval do
Geo::DummyReplicator.class_eval do
event :test
event :another_test
......@@ -37,7 +37,7 @@ describe Gitlab::Geo::Replicator do
end
context 'event DSL' do
subject { DummyReplicator }
subject { Geo::DummyReplicator }
describe '.supported_events' do
it 'expects :test event to be supported' do
......@@ -68,7 +68,7 @@ describe Gitlab::Geo::Replicator do
include Gitlab::Geo::ReplicableModel
with_replicator DummyReplicator
with_replicator Geo::DummyReplicator
end
end
......@@ -79,12 +79,12 @@ describe Gitlab::Geo::Replicator do
end
it 'instantiates a replicator into the model' do
expect(subject.replicator).to be_a(DummyReplicator)
expect(subject.replicator).to be_a(Geo::DummyReplicator)
end
end
describe '#publish' do
subject { DummyReplicator.new }
subject { Geo::DummyReplicator.new }
context 'when geo_self_service_framework feature is disabled' do
before do
......@@ -120,7 +120,7 @@ describe Gitlab::Geo::Replicator do
end
describe '#consume' do
subject { DummyReplicator.new }
subject { Geo::DummyReplicator.new }
it 'accepts valid attributes' do
expect { subject.consume(:test, user: 'something', other: 'something else') }.not_to raise_error
......@@ -132,5 +132,19 @@ describe Gitlab::Geo::Replicator do
subject.consume(:test, user: 'something', other: 'something else')
end
end
describe '.for_class_name' do
context 'when given a Geo RegistryFinder' do
it 'returns the corresponding Replicator class' do
expect(described_class.for_class_name('Geo::DummyRegistryFinder')).to eq(Geo::DummyReplicator)
end
end
context 'when given a Geo RegistriesResolver"' do
it 'returns the corresponding Replicator class' do
expect(described_class.for_class_name('Geo::DummyRegistriesResolver')).to eq(Geo::DummyReplicator)
end
end
end
end
end
# frozen_string_literal: true
RSpec.shared_examples 'a framework registry finder' do |registry_factory|
include ::EE::GeoHelpers
let!(:registry1) { create(registry_factory) }
let!(:registry2) { create(registry_factory) }
let!(:registry3) { create(registry_factory) }
let(:params) { {} }
subject(:registries) { described_class.new(user, params).execute }
describe '#execute' do
context 'when user cannot read all Geo' do
let_it_be(:user) { create(:user) }
it { is_expected.to be_empty }
end
context 'when user can read all Geo' do
let_it_be(:user) { create(:user, :admin) }
context 'with an ids param' do
let(:params) { { ids: [registry3.id, registry1.id] } }
it 'returns specified registries' do
expect(registries.to_a).to eq([registry1, registry3])
end
end
context 'without an ids param' do
it 'returns all registries' do
expect(registries.to_a).to eq([registry1, registry2, registry3])
end
end
end
end
end
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