diff --git a/app/services/paas_configuration_service.rb b/app/services/paas_configuration_service.rb new file mode 100644 index 000000000..f14ad34df --- /dev/null +++ b/app/services/paas_configuration_service.rb @@ -0,0 +1,44 @@ +class PaasConfigurationService + attr_reader :s3_buckets + + def initialize(logger = Rails.logger) + @logger = logger + @paas_config = read_pass_config + @s3_buckets = read_s3_buckets + end + + def config_present? + !ENV["VCAP_SERVICES"].nil? + end + + def s3_config_present? + config_present? && @paas_config.key?(:"aws-s3-bucket") + end + +private + + def read_pass_config + unless config_present? + @logger.warn("Could not find VCAP_SERVICES in the environment!") + return {} + end + + begin + JSON.parse(ENV["VCAP_SERVICES"], { symbolize_names: true }) + rescue StandardError + @logger.warn("Could not parse VCAP_SERVICES!") + end + end + + def read_s3_buckets + return [] unless s3_config_present? + + s3_buckets = {} + @paas_config[:"aws-s3-bucket"].each do |bucket_config| + if bucket_config.key?(:instance_name) + s3_buckets[bucket_config[:instance_name].to_sym] = bucket_config + end + end + s3_buckets + end +end diff --git a/spec/services/paas_configuration_service_spec.rb b/spec/services/paas_configuration_service_spec.rb new file mode 100644 index 000000000..d67b1ad45 --- /dev/null +++ b/spec/services/paas_configuration_service_spec.rb @@ -0,0 +1,72 @@ +require "rails_helper" + +RSpec.describe "PaasConfigurationService" do + context "when the paas configuration is unavailable" do + subject { PaasConfigurationService.new(logger) } + let(:logger) { double("logger") } + + before { allow(logger).to receive(:warn) } + + it "returns the configuration as not present" do + expect(subject.config_present?).to be(false) + end + + it "returns the S3 configuration as not present" do + expect(subject.s3_config_present?).to be(false) + end + + it "does not retrieve any S3 bucket configuration" do + expect(subject.s3_buckets).to be_empty + end + end + + context "when the paas configuration is present with S3 buckets" do + subject { PaasConfigurationService.new(double("logger")) } + let(:vcap_services) do + <<-JSON + {"aws-s3-bucket": [{"instance_name": "bucket_1"},{"instance_name": "bucket_2"}]} + JSON + end + + before do + allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services) + end + + it "returns the configuration as present" do + expect(subject.config_present?).to be(true) + end + + it "returns the S3 configuration as present" do + expect(subject.s3_config_present?).to be(true) + end + + it "does retrieve the S3 bucket configurations" do + s3_buckets = subject.s3_buckets + + expect(s3_buckets).to_not be_empty + expect(s3_buckets.count).to be(2) + expect(s3_buckets).to have_key(:bucket_1) + expect(s3_buckets).to have_key(:bucket_2) + end + end + + context "when the paas configuration is present without S3 buckets" do + subject { PaasConfigurationService.new(double("logger")) } + + before do + allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("{}") + end + + it "returns the configuration as present" do + expect(subject.config_present?).to be(true) + end + + it "returns the S3 configuration as not present" do + expect(subject.s3_config_present?).to be(false) + end + + it "does not retrieve any S3 bucket configuration" do + expect(subject.s3_buckets).to be_empty + end + end +end