From 17e4c1e272855b03f63285ff7acffaf4c9aea6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Fri, 4 Mar 2022 14:59:28 +0000 Subject: [PATCH] Add support to read redis configuration --- app/services/paas_configuration_service.rb | 19 +++++- .../paas_configuration_service_spec.rb | 58 ++++++++++++++++++- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/services/paas_configuration_service.rb b/app/services/paas_configuration_service.rb index dc27a1b3b..008b4d2ab 100644 --- a/app/services/paas_configuration_service.rb +++ b/app/services/paas_configuration_service.rb @@ -1,10 +1,11 @@ class PaasConfigurationService - attr_reader :s3_buckets + attr_reader :s3_buckets, :redis_uris def initialize(logger = Rails.logger) @logger = logger @paas_config = read_pass_config @s3_buckets = read_s3_buckets + @redis_uris = read_redis_uris end def config_present? @@ -15,6 +16,10 @@ class PaasConfigurationService config_present? && @paas_config.key?(:"aws-s3-bucket") end + def redis_config_present? + config_present? && @paas_config.key?(:redis) + end + private def read_pass_config @@ -42,4 +47,16 @@ private end s3_buckets end + + def read_redis_uris + return {} unless redis_config_present? + + redis_uris = {} + @paas_config[:redis].each do |redis_config| + if redis_config.key?(:instance_name) + redis_uris[redis_config[:instance_name].to_sym] = redis_config.dig(:credentials, :uri) + end + end + redis_uris + end end diff --git a/spec/services/paas_configuration_service_spec.rb b/spec/services/paas_configuration_service_spec.rb index fa1e99d58..45db1533a 100644 --- a/spec/services/paas_configuration_service_spec.rb +++ b/spec/services/paas_configuration_service_spec.rb @@ -20,12 +20,15 @@ RSpec.describe PaasConfigurationService do expect(config_service.s3_buckets).to be_a(Hash) expect(config_service.s3_buckets).to be_empty end + + it "does not retrieve any redis configuration" do + expect(config_service.redis_uris).to be_a(Hash) + expect(config_service.redis_uris).to be_empty + end end context "when configuration is present but invalid" do - let(:vcap_services) do - { "aws-s3-bucket": [{ instance_name: "bucket_1" }, { instance_name: "bucket_2" }] } - end + let(:vcap_services) { "random text" } before do allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services) @@ -85,4 +88,53 @@ RSpec.describe PaasConfigurationService do expect(config_service.s3_buckets).to be_empty end end + + context "when the paas configuration is present with redis configurations" do + let(:vcap_services) do + <<-JSON + {"redis": [{"instance_name": "redis_1", "credentials": {"uri": "redis_uri" }}]} + JSON + end + + before do + allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return(vcap_services) + end + + it "returns the configuration as present" do + expect(config_service.config_present?).to be(true) + end + + it "returns the redis configuration as present" do + expect(config_service.redis_config_present?).to be(true) + end + + it "does retrieve the redis configurations" do + redis_uris = config_service.redis_uris + + expect(redis_uris).not_to be_empty + expect(redis_uris.count).to be(1) + expect(redis_uris).to have_key(:redis_1) + expect(redis_uris[:redis_1]).to eq("redis_uri") + end + end + + context "when the paas configuration is present without redis configuration" do + before do + allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("{}") + end + + it "returns the configuration as present" do + expect(config_service.config_present?).to be(true) + end + + it "returns the redis configuration as not present" do + expect(config_service.redis_config_present?).to be(false) + end + + it "does not retrieve any redis uris from configuration" do + expect(config_service.redis_uris).to be_a(Hash) + expect(config_service.redis_uris).to be_empty + end + end + end