diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb index a154bbebe322..b223c8ec3cb0 100644 --- a/modules/storages/app/models/storages/storage.rb +++ b/modules/storages/app/models/storages/storage.rb @@ -92,14 +92,12 @@ class Storage < ApplicationRecord }.freeze, _prefix: :health def self.shorten_provider_type(provider_type) - case /Storages::(?'provider_name'.*)Storage/.match(provider_type) - in provider_name: - provider_name.underscore - else - raise ArgumentError, - "Unknown provider_type! Given: #{provider_type}. " \ - "Expected the following signature: Storages::{Name of the provider}Storage" - end + short, = PROVIDER_TYPE_SHORT_NAMES.find { |_, long| provider_type == long } + return short.to_s if short + + raise ArgumentError, + "Unknown provider_type! Given: #{provider_type}. " \ + "Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES." end def self.one_drive_without_ee_token?(provider_type) diff --git a/modules/storages/spec/models/storages/shared_base_storage_spec.rb b/modules/storages/spec/models/storages/shared_base_storage_spec.rb index e0dc9e75878b..795143404ede 100644 --- a/modules/storages/spec/models/storages/shared_base_storage_spec.rb +++ b/modules/storages/spec/models/storages/shared_base_storage_spec.rb @@ -49,20 +49,24 @@ end end - context "when provider_type does not match the signature" do + context "when provider_type is unknown" do it "raises an error", :aggregate_failures do expect do described_class.shorten_provider_type("Storages::Nextcloud") end.to raise_error("Unknown provider_type! Given: Storages::Nextcloud. " \ - "Expected the following signature: Storages::{Name of the provider}Storage") + "Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.") expect do described_class.shorten_provider_type("Storages:NextcloudStorage") end.to raise_error("Unknown provider_type! Given: Storages:NextcloudStorage. " \ - "Expected the following signature: Storages::{Name of the provider}Storage") + "Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.") expect do described_class.shorten_provider_type("Storages::NextcloudStorag") end.to raise_error("Unknown provider_type! Given: Storages::NextcloudStorag. " \ - "Expected the following signature: Storages::{Name of the provider}Storage") + "Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.") + expect do + described_class.shorten_provider_type("Storages::UnknownStorage") + end.to raise_error("Unknown provider_type! Given: Storages::UnknownStorage. " \ + "Known provider types are defined in Storages::Storage::PROVIDER_TYPE_SHORT_NAMES.") end end end