From 5534190c89cff543464654c9226d1a4641214bce Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 7 Sep 2023 15:13:58 +0200 Subject: [PATCH] chore: build openstack client without calling now private service.client --- app/jobs/pjs_migration_job.rb | 13 ++++++++++++- app/lib/active_storage/downloadable_file.rb | 10 +++++++++- spec/lib/active_storage/downloadable_file_spec.rb | 4 +++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/jobs/pjs_migration_job.rb b/app/jobs/pjs_migration_job.rb index 4a44b59e4..06acaafd6 100644 --- a/app/jobs/pjs_migration_job.rb +++ b/app/jobs/pjs_migration_job.rb @@ -1,3 +1,5 @@ +require 'fog/openstack' + class PjsMigrationJob < ApplicationJob queue_as :pj_migration_jobs @@ -7,7 +9,6 @@ class PjsMigrationJob < ApplicationJob return if already_moved?(blob) service = blob.service - client = service.client container = service.container old_key = blob.key new_key = "#{blob.created_at.strftime('%Y/%m/%d')}/#{old_key[0..1]}/#{old_key}" @@ -28,4 +29,14 @@ class PjsMigrationJob < ApplicationJob def already_moved?(blob) blob.key.include?('/') end + + private + + def client + @client ||= begin + credentials = Rails.application.config.active_storage + .service_configurations['openstack']['credentials'] + Fog::OpenStack::Storage.new(credentials) + end + end end diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index c403495f1..d2902ebd8 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -1,3 +1,5 @@ +require 'fog/openstack' + class ActiveStorage::DownloadableFile def self.create_list_from_dossiers( dossiers, @@ -20,7 +22,6 @@ class ActiveStorage::DownloadableFile true else service = file.blob.service - client = service.client begin client.head_object(service.container, file.blob.key) true @@ -33,6 +34,13 @@ class ActiveStorage::DownloadableFile private + def self.client + credentials = Rails.application.config.active_storage + .service_configurations['openstack']['credentials'] + + Fog::OpenStack::Storage.new(credentials) + end + def self.bill_and_path(bill) [ bill, diff --git a/spec/lib/active_storage/downloadable_file_spec.rb b/spec/lib/active_storage/downloadable_file_spec.rb index 81bad0a86..7119ff164 100644 --- a/spec/lib/active_storage/downloadable_file_spec.rb +++ b/spec/lib/active_storage/downloadable_file_spec.rb @@ -35,11 +35,13 @@ describe ActiveStorage::DownloadableFile do let(:available_blob_key) { 'available' } let(:unavailable_blob_key) { 'broken' } let(:active_storage_client) { double } - let(:active_storage_service) { double(client: active_storage_client, container: object_storage_container) } + let(:active_storage_service) { double(container: object_storage_container) } before do require 'fog/openstack' Rails.application.config.active_storage.service = :openstack + + allow(ActiveStorage::DownloadableFile).to receive(:client).and_return(active_storage_client) end after { Rails.application.config.active_storage.service = :test }