Skip to content

Commit 18a63d2

Browse files
author
Christopher Frost
committed
Session replication should accept host as well as hostname
Redis provides host and not hostname as a credential for session replication. The buildpack currently only accepts hostname. This commit adds support for either 'host' or 'hostname' but not both to be provided in the credentials. See: https://groups.google.com/a/cloudfoundry.org/d/msg/vcap-dev/vIHzLxZHQ2A/YyT-a6va-cYJ [#73611494]
1 parent a6cef1b commit 18a63d2

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

lib/java_buildpack/component/services.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def initialize(raw)
3333
# +filter+ matches exactly one service, +false+ otherwise.
3434
#
3535
# @param [Regexp, String] filter a +RegExp+ or +String+ to match against the name, label, and tags of the services
36-
# @param [String] required_credentials an optional list of keys that must exist in the credentials payload of
37-
# the candidate service
36+
# @param [String] required_credentials an optional list of keys or groups of keys, where at one key from the
37+
# group, must exist in the credentials payload of the candidate service
3838
# @return [Boolean] +true+ if the +filter+ matches exactly one service with the required credentials, +false+
3939
# otherwise.
4040
def one_service?(filter, *required_credentials)
@@ -68,7 +68,9 @@ def find_service(filter)
6868
private
6969

7070
def credentials?(candidate, required_keys)
71-
required_keys.all? { |k| candidate.key? k }
71+
required_keys.all? do |k|
72+
k.kind_of?(Array) ? k.one? { |g| candidate.key?(g) } : candidate.key?(k)
73+
end
7274
end
7375

7476
def matcher(filter)

lib/java_buildpack/container/tomcat/tomcat_redis_store.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def release
4444

4545
# (see JavaBuildpack::Component::VersionedDependencyComponent#supports?)
4646
def supports?
47-
@application.services.one_service? FILTER, KEY_HOST_NAME, KEY_PORT, KEY_PASSWORD
47+
@application.services.one_service? FILTER, [KEY_HOST_NAME, KEY_HOST], KEY_PORT, KEY_PASSWORD
4848
end
4949

5050
private
@@ -55,6 +55,8 @@ def supports?
5555

5656
KEY_HOST_NAME = 'hostname'.freeze
5757

58+
KEY_HOST = 'host'.freeze
59+
5860
KEY_PASSWORD = 'password'.freeze
5961

6062
KEY_PORT = 'port'.freeze
@@ -76,7 +78,7 @@ def add_store(manager)
7678

7779
manager.add_element 'Store',
7880
'className' => REDIS_STORE_CLASS_NAME,
79-
'host' => credentials[KEY_HOST_NAME],
81+
'host' => credentials[KEY_HOST_NAME] || credentials[KEY_HOST],
8082
'port' => credentials[KEY_PORT],
8183
'database' => @configuration['database'],
8284
'password' => credentials[KEY_PASSWORD],

spec/java_buildpack/component/services_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
expect(services.one_service?(/test-tag/, 'uri')).to be
5959
end
6060

61+
it 'should return true from one_service? if there is a matching service with required group credentials' do
62+
expect(services.one_service? 'test-tag', %w(uri other)).to be
63+
expect(services.one_service?(/test-tag/, %w(uri other))).to be
64+
end
65+
6166
it 'should return nil from find_service? if there is no service that matches' do
6267
expect(services.find_service 'bad-test').to be_nil
6368
expect(services.find_service(/bad-test/)).to be_nil

spec/java_buildpack/container/tomcat/tomcat_redis_store_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
context do
3737

3838
before do
39-
allow(services).to receive(:one_service?).with(/session-replication/, 'hostname', 'port', 'password')
39+
allow(services).to receive(:one_service?).with(/session-replication/, %w(hostname host), 'port', 'password')
4040
.and_return(true)
4141
allow(services).to receive(:find_service).and_return('credentials' => { 'hostname' => 'test-host',
4242
'port' => 'test-port',
@@ -68,6 +68,22 @@
6868

6969
end
7070

71+
context do
72+
73+
before do
74+
allow(services).to receive(:one_service?).with(/session-replication/, %w(hostname host), 'port', 'password')
75+
.and_return(true)
76+
allow(services).to receive(:find_service).and_return('credentials' => { 'host' => 'test-host',
77+
'port' => 'test-port',
78+
'password' => 'test-password' })
79+
end
80+
81+
it 'should detect with a session-replication service' do
82+
expect(component.detect).to eq("tomcat-redis-store=#{version}")
83+
end
84+
85+
end
86+
7187
it 'should do nothing during release' do
7288
component.release
7389
end

0 commit comments

Comments
 (0)