Commit 028bfb11 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'sh-load-azure-backup' into 'master'

Dynamically load Azure dependency for backups

See merge request gitlab-org/gitlab!41649
parents 0e0c7376 da81bdb0
class DirectUploadsValidator 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) ValidationError = Class.new(StandardError)
...@@ -24,7 +26,7 @@ class DirectUploadsValidator ...@@ -24,7 +26,7 @@ class DirectUploadsValidator
def provider_loaded?(provider) def provider_loaded?(provider)
return false unless SUPPORTED_DIRECT_UPLOAD_PROVIDERS.include?(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 true
end end
......
...@@ -47,7 +47,7 @@ module Backup ...@@ -47,7 +47,7 @@ module Backup
return return
end end
directory = connect_to_remote_directory(connection_settings) directory = connect_to_remote_directory(Gitlab.config.backup.upload)
if directory.files.create(create_attributes) if directory.files.create(create_attributes)
progress.puts "done".color(:green) progress.puts "done".color(:green)
...@@ -195,9 +195,11 @@ module Backup ...@@ -195,9 +195,11 @@ module Backup
@backup_file_list.map {|item| item.gsub("#{FILE_NAME_SUFFIX}", "")} @backup_file_list.map {|item| item.gsub("#{FILE_NAME_SUFFIX}", "")}
end end
def connect_to_remote_directory(connection_settings) def connect_to_remote_directory(options)
# our settings use string keys, but Fog expects symbols config = ObjectStorage::Config.new(options)
connection = ::Fog::Storage.new(connection_settings.symbolize_keys) config.load_provider
connection = ::Fog::Storage.new(config.credentials)
# We only attempt to create the directory for local backups. For AWS # We only attempt to create the directory for local backups. For AWS
# and other cloud providers, we cannot guarantee the user will have # and other cloud providers, we cannot guarantee the user will have
......
...@@ -2,12 +2,26 @@ ...@@ -2,12 +2,26 @@
module ObjectStorage module ObjectStorage
class Config class Config
AWS_PROVIDER = 'AWS'
AZURE_PROVIDER = 'AzureRM'
GOOGLE_PROVIDER = 'Google'
attr_reader :options attr_reader :options
def initialize(options) def initialize(options)
@options = options.to_hash.deep_symbolize_keys @options = options.to_hash.deep_symbolize_keys
end end
def load_provider
if aws?
require 'fog/aws'
elsif google?
require 'fog/google'
elsif azure?
require 'fog/azurerm'
end
end
def credentials def credentials
@credentials ||= options[:connection] || {} @credentials ||= options[:connection] || {}
end end
...@@ -30,7 +44,7 @@ module ObjectStorage ...@@ -30,7 +44,7 @@ module ObjectStorage
# AWS-specific options # AWS-specific options
def aws? def aws?
provider == 'AWS' provider == AWS_PROVIDER
end end
def use_iam_profile? def use_iam_profile?
...@@ -61,11 +75,11 @@ module ObjectStorage ...@@ -61,11 +75,11 @@ module ObjectStorage
# End Azure-specific options # End Azure-specific options
def google? def google?
provider == 'Google' provider == GOOGLE_PROVIDER
end end
def azure? def azure?
provider == 'AzureRM' provider == AZURE_PROVIDER
end end
def fog_attributes def fog_attributes
......
...@@ -416,5 +416,28 @@ RSpec.describe Backup::Manager do ...@@ -416,5 +416,28 @@ RSpec.describe Backup::Manager do
subject.upload subject.upload
end end
end end
context 'with AzureRM provider' do
before do
stub_backup_setting(
upload: {
connection: {
provider: 'AzureRM',
azure_storage_account_name: 'test-access-id',
azure_storage_access_key: 'secret'
},
remote_directory: 'directory',
multipart_chunk_size: nil,
encryption: nil,
encryption_key: nil,
storage_class: nil
}
)
end
it 'loads the provider' do
expect { subject.upload }.not_to raise_error
end
end
end end
end end
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rspec-parameterized' require 'rspec-parameterized'
require 'fog/core'
RSpec.describe ObjectStorage::Config do RSpec.describe ObjectStorage::Config do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
...@@ -35,6 +36,46 @@ RSpec.describe ObjectStorage::Config do ...@@ -35,6 +36,46 @@ RSpec.describe ObjectStorage::Config do
subject { described_class.new(raw_config.as_json) } 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 describe '#credentials' do
it { expect(subject.credentials).to eq(credentials) } it { expect(subject.credentials).to eq(credentials) }
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