From 5e3b175416b5e99bd0154dfa0b8cfef065f75946 Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Thu, 3 Aug 2023 12:38:15 +0100 Subject: [PATCH] feat: update application to use non paas and paas configuration services, add helper to determine if platform is paas feat: don't double-instantiate the configuration service feat: add self. to is_paas? definition, and require module in rack_attack chore: lint --- app/helpers/platform_helper.rb | 5 +++++ app/jobs/data_export_xml_job.rb | 2 +- app/models/forms/bulk_upload_lettings/upload_your_file.rb | 2 +- app/models/forms/bulk_upload_sales/upload_your_file.rb | 2 +- app/services/bulk_upload/downloader.rb | 2 +- config/initializers/rack_attack.rb | 8 ++++++-- config/initializers/sidekiq.rb | 6 ++++-- lib/tasks/data_import.rake | 2 +- lib/tasks/data_import_field.rake | 2 +- lib/tasks/full_import.rake | 8 ++++---- 10 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 app/helpers/platform_helper.rb diff --git a/app/helpers/platform_helper.rb b/app/helpers/platform_helper.rb new file mode 100644 index 000000000..08297f53b --- /dev/null +++ b/app/helpers/platform_helper.rb @@ -0,0 +1,5 @@ +module PlatformHelper + def self.is_paas? + !ENV["VCAP_SERVICES"].nil? + end +end diff --git a/app/jobs/data_export_xml_job.rb b/app/jobs/data_export_xml_job.rb index b26b65364..b882308ed 100644 --- a/app/jobs/data_export_xml_job.rb +++ b/app/jobs/data_export_xml_job.rb @@ -2,7 +2,7 @@ class DataExportXmlJob < ApplicationJob queue_as :default def perform(full_update: false) - storage_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"]) + storage_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"]) export_service = Exports::LettingsLogExportService.new(storage_service) export_service.export_xml_lettings_logs(full_update:) diff --git a/app/models/forms/bulk_upload_lettings/upload_your_file.rb b/app/models/forms/bulk_upload_lettings/upload_your_file.rb index 57ac017a3..bb7e9fad6 100644 --- a/app/models/forms/bulk_upload_lettings/upload_your_file.rb +++ b/app/models/forms/bulk_upload_lettings/upload_your_file.rb @@ -57,7 +57,7 @@ module Forms def storage_service @storage_service ||= if upload_enabled? Storage::S3Service.new( - Configuration::PaasConfigurationService.new, + PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"], ) else diff --git a/app/models/forms/bulk_upload_sales/upload_your_file.rb b/app/models/forms/bulk_upload_sales/upload_your_file.rb index 117e612b1..b08983d82 100644 --- a/app/models/forms/bulk_upload_sales/upload_your_file.rb +++ b/app/models/forms/bulk_upload_sales/upload_your_file.rb @@ -50,7 +50,7 @@ module Forms def storage_service @storage_service ||= if FeatureToggle.upload_enabled? Storage::S3Service.new( - Configuration::PaasConfigurationService.new, + PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"], ) else diff --git a/app/services/bulk_upload/downloader.rb b/app/services/bulk_upload/downloader.rb index 8fcd9ccca..f3d5117e4 100644 --- a/app/services/bulk_upload/downloader.rb +++ b/app/services/bulk_upload/downloader.rb @@ -38,7 +38,7 @@ private def s3_storage_service Storage::S3Service.new( - Configuration::PaasConfigurationService.new, + PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"], ) end diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index febd58a50..9b201b054 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -1,14 +1,18 @@ require "configuration/configuration_service" require "configuration/paas_configuration_service" +require "configuration/env_configuration_service" +require Rails.root.join("app/helpers/platform_helper") + +configuration_service = PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new if Rails.env.development? || Rails.env.test? Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new Rack::Attack.enabled = false elsif Rails.env.review? - redis_url = Configuration::PaasConfigurationService.new.redis_uris.to_a[0][1] + redis_url = configuration_service.redis_uris.to_a[0][1] Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: redis_url) else - redis_url = Configuration::PaasConfigurationService.new.redis_uris[:"dluhc-core-#{Rails.env}-redis"] + redis_url = configuration_service.redis_uris[:"dluhc-core-#{Rails.env}-redis"] Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: redis_url) end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index c8e38e1d7..bc1b578ef 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,8 +1,10 @@ require "sidekiq/web" require "sidekiq/cron/web" +configuration_service = PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new + if Rails.env.staging? || Rails.env.production? - redis_url = Configuration::PaasConfigurationService.new.redis_uris[:"dluhc-core-#{Rails.env}-redis"] + redis_url = configuration_service.redis_uris[:"dluhc-core-#{Rails.env}-redis"] Sidekiq.configure_server do |config| config.redis = { url: redis_url } @@ -14,7 +16,7 @@ if Rails.env.staging? || Rails.env.production? end if Rails.env.review? - redis_url = Configuration::PaasConfigurationService.new.redis_uris.to_a[0][1] + redis_url = configuration_service.redis_uris.to_a[0][1] Sidekiq.configure_server do |config| config.redis = { url: redis_url } diff --git a/lib/tasks/data_import.rake b/lib/tasks/data_import.rake index 0dede82a3..7e71772e3 100644 --- a/lib/tasks/data_import.rake +++ b/lib/tasks/data_import.rake @@ -5,7 +5,7 @@ namespace :core do path = args[:path] raise "Usage: rake core:data_import['data_type', 'path/to/xml_files']" if path.blank? || type.blank? - storage_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + storage_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) case type when "organisation" diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index 54d25517b..a30a2ec43 100644 --- a/lib/tasks/data_import_field.rake +++ b/lib/tasks/data_import_field.rake @@ -8,7 +8,7 @@ namespace :core do # We only allow a reduced list of known fields to be updatable case field when "tenancycode", "major_repairs", "lettings_allocation", "offered" - s3_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) archive_io = s3_service.get_file_io(path) archive_service = Storage::ArchiveService.new(archive_io) if archive_service.folder_present?("logs") diff --git a/lib/tasks/full_import.rake b/lib/tasks/full_import.rake index 28af8c4dc..4fea7ef93 100644 --- a/lib/tasks/full_import.rake +++ b/lib/tasks/full_import.rake @@ -6,7 +6,7 @@ namespace :import do institutions_csv_name = args[:institutions_csv_name] raise "Usage: rake import:initial['institutions_csv_name']" if institutions_csv_name.blank? - s3_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) csv = CSV.parse(s3_service.get_file_io(institutions_csv_name), headers: true) org_count = csv.length @@ -43,7 +43,7 @@ namespace :import do institutions_csv_name = args[:institutions_csv_name] raise "Usage: rake import:logs['institutions_csv_name']" if institutions_csv_name.blank? - s3_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) csv = CSV.parse(s3_service.get_file_io(institutions_csv_name), headers: true) org_count = csv.length @@ -77,7 +77,7 @@ namespace :import do institutions_csv_name = args[:institutions_csv_name] raise "Usage: rake import:trigger_invites['institutions_csv_name']" if institutions_csv_name.blank? - s3_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) csv = CSV.parse(s3_service.get_file_io(institutions_csv_name), headers: true) Rails.logger.info("Triggering user invite emails") @@ -98,7 +98,7 @@ namespace :import do institutions_csv_name = args[:institutions_csv_name] raise "Usage: rake import:generate_reports['institutions_csv_name']" if institutions_csv_name.blank? - s3_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) institutions_csv = CSV.parse(s3_service.get_file_io(institutions_csv_name), headers: true) Imports::ImportReportService.new(s3_service, institutions_csv).create_reports(institutions_csv_name)