Commit da81bdb0 authored by Stan Hu's avatar Stan Hu

Refactor Backup::Manager to use ObjectStorage::Config

Since AzureRM is a constant in many places, centralize this
with the other providers.
parent 0b18c956
class DirectUploadsValidator
SUPPORTED_DIRECT_UPLOAD_PROVIDERS = %w(Google AWS AzureRM).freeze
SUPPORTED_DIRECT_UPLOAD_PROVIDERS = [ObjectStorage::Config::GOOGLE_PROVIDER,
ObjectStorage::Config::AWS_PROVIDER,
ObjectStorage::Config::AZURE_PROVIDER].freeze
ValidationError = Class.new(StandardError)
......@@ -24,7 +26,7 @@ class DirectUploadsValidator
def provider_loaded?(provider)
return false unless SUPPORTED_DIRECT_UPLOAD_PROVIDERS.include?(provider)
require 'fog/azurerm' if provider == 'AzureRM'
require 'fog/azurerm' if provider == ObjectStorage::Config::AZURE_PROVIDER
true
end
......
......@@ -47,7 +47,7 @@ module Backup
return
end
directory = connect_to_remote_directory(connection_settings)
directory = connect_to_remote_directory(Gitlab.config.backup.upload)
if directory.files.create(create_attributes)
progress.puts "done".color(:green)
......@@ -195,11 +195,11 @@ module Backup
@backup_file_list.map {|item| item.gsub("#{FILE_NAME_SUFFIX}", "")}
end
def connect_to_remote_directory(connection_settings)
settings = connection_settings.symbolize_keys
load_provider(settings)
# our settings use string keys, but Fog expects symbols
connection = ::Fog::Storage.new(settings)
def connect_to_remote_directory(options)
config = ObjectStorage::Config.new(options)
config.load_provider
connection = ::Fog::Storage.new(config.credentials)
# We only attempt to create the directory for local backups. For AWS
# and other cloud providers, we cannot guarantee the user will have
......@@ -211,14 +211,6 @@ module Backup
end
end
def load_provider(settings)
provider = settings[:provider]
return unless provider.present?
require 'fog/azurerm' if provider == 'AzureRM'
end
def remote_directory
Gitlab.config.backup.upload.remote_directory
end
......
......@@ -2,12 +2,26 @@
module ObjectStorage
class Config
AWS_PROVIDER = 'AWS'
AZURE_PROVIDER = 'AzureRM'
GOOGLE_PROVIDER = 'Google'
attr_reader :options
def initialize(options)
@options = options.to_hash.deep_symbolize_keys
end
def load_provider
if aws?
require 'fog/aws'
elsif google?
require 'fog/google'
elsif azure?
require 'fog/azurerm'
end
end
def credentials
@credentials ||= options[:connection] || {}
end
......@@ -30,7 +44,7 @@ module ObjectStorage
# AWS-specific options
def aws?
provider == 'AWS'
provider == AWS_PROVIDER
end
def use_iam_profile?
......@@ -61,11 +75,11 @@ module ObjectStorage
# End Azure-specific options
def google?
provider == 'Google'
provider == GOOGLE_PROVIDER
end
def azure?
provider == 'AzureRM'
provider == AZURE_PROVIDER
end
def fog_attributes
......
......@@ -2,6 +2,7 @@
require 'fast_spec_helper'
require 'rspec-parameterized'
require 'fog/core'
RSpec.describe ObjectStorage::Config do
using RSpec::Parameterized::TableSyntax
......@@ -35,6 +36,46 @@ RSpec.describe ObjectStorage::Config do
subject { described_class.new(raw_config.as_json) }
describe '#load_provider' do
before do
subject.load_provider
end
context 'with AWS' do
it 'registers AWS as a provider' do
expect(Fog.providers.keys).to include(:aws)
end
end
context 'with Google' do
let(:credentials) do
{
provider: 'Google',
google_storage_access_key_id: 'GOOGLE_ACCESS_KEY_ID',
google_storage_secret_access_key: 'GOOGLE_SECRET_ACCESS_KEY'
}
end
it 'registers Google as a provider' do
expect(Fog.providers.keys).to include(:google)
end
end
context 'with Azure' do
let(:credentials) do
{
provider: 'AzureRM',
azure_storage_account_name: 'azuretest',
azure_storage_access_key: 'ABCD1234'
}
end
it 'registers AzureRM as a provider' do
expect(Fog.providers.keys).to include(:azurerm)
end
end
end
describe '#credentials' do
it { expect(subject.credentials).to eq(credentials) }
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