Skip to content

Commit

Permalink
implement and spec GCP stored path substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Apr 1, 2024
1 parent 747c205 commit a595a86
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
33 changes: 20 additions & 13 deletions app/models/storage_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class StorageProvider < ApplicationRecord

def raise_unimplemented_storage_type_error!
raise NotImplementedError,
"StorageProvider storage_type #{storage_provider.storage_type} "\
"StorageProvider storage_type #{self.storage_type} "\
'is not implemented yet.'
end

Expand Down Expand Up @@ -59,25 +59,32 @@ def perform_transfer(pending_transfer, stored_object_key, metadata:, tags: {})
end

def local_path_to_stored_path(local_path)
if self.storage_type == 'aws'
local_path_key_map = AWS_CONFIG[:local_path_key_map]
matching_local_path_prefix = local_path_key_map.keys.find do |local_file_prefix|
local_path.start_with?(local_file_prefix.to_s)
end

if matching_local_path_prefix.nil?
raise "Could not find #{self.storage_type} storage provider "\
"mapping for #{local_path}"
end
matching_local_path_prefix = local_path_key_map.keys.find do |local_file_prefix|
local_path.start_with?(local_file_prefix.to_s)
end

return local_path.sub(matching_local_path_prefix.to_s, local_path_key_map[matching_local_path_prefix])
if matching_local_path_prefix.nil?
raise "Could not find #{self.storage_type} storage provider "\
"mapping for #{local_path}"
end

raise_unimplemented_storage_type_error!
local_path.sub(matching_local_path_prefix.to_s, local_path_key_map[matching_local_path_prefix])
end

private

def local_path_key_map
@local_path_key_map ||=
case self.storage_type
when 'aws'
AWS_CONFIG[:local_path_key_map]
when 'gcp'
GCP_CONFIG[:local_path_key_map]
else
raise_unimplemented_storage_type_error!
end
end

def aws_upload_file_opts(pending_transfer, metadata:, tags:)
{
overwrite: false, # This will raise an Atc::Exceptions::ObjectExists error if the object exists
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/storage_providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,16 @@
)
end
end

trait :cul do
initialize_with do
storage_type = StorageProvider.storage_types[:cul]
container_name = '/digital/working'
StorageProvider.find_by(storage_type: storage_type, container_name: container_name) || StorageProvider.create(
storage_type: storage_type,
container_name: container_name
)
end
end
end
end
30 changes: 30 additions & 0 deletions spec/models/storage_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
)
end
end

describe '#local_path_to_stored_path' do
let(:local_path) { '/digital/preservation/a/b/c' }
let(:stored_path) { 'a/b/c' }

it 'substitutes as expected' do
expect(storage_provider.local_path_to_stored_path(local_path)).to eql(stored_path)
end
end
end

context 'GCP provider type' do
Expand Down Expand Up @@ -107,6 +116,27 @@
)
end
end

describe '#local_path_to_stored_path' do
let(:local_path) { '/digital/preservation/a/b/c' }
let(:stored_path) { 'a/b/c' }

it 'substitutes as expected' do
expect(storage_provider.local_path_to_stored_path(local_path)).to eql(stored_path)
end
end
end

context 'CUL provider type' do
subject(:storage_provider) { FactoryBot.build(:storage_provider, :cul) }

describe '#local_path_to_stored_path' do
let(:local_path) { '/digital/preservation/a/b/c' }

it 'substitutes as expected' do
expect { storage_provider.local_path_to_stored_path(local_path) }.to raise_error(NotImplementedError)
end
end
end

describe 'validations' do
Expand Down

0 comments on commit a595a86

Please sign in to comment.