Skip to content

Commit 69a5282

Browse files
author
Glyn Normington
committed
Merge 58830044-user-provide-newrelic-service to master
[Completes #58830044]
2 parents 47d091e + 445fadd commit 69a5282

2 files changed

Lines changed: 74 additions & 12 deletions

File tree

lib/java_buildpack/util/service_utils.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,33 @@ class ServiceUtils
2929
def self.find_service(services, filter)
3030
service = nil
3131

32-
types = services.keys.select { |key| key =~ filter }
33-
fail "Exactly one service type matching '#{filter.source}' can be bound. Found #{types.length}." if types.length > 1
32+
matching_services = services.select { |key, service_instances| key =~ filter || service_instances_match(service_instances, filter) }
33+
fail "Exactly one service type matching '#{filter.source}' can be bound. Found #{matching_services.length}." if matching_services.length > 1
3434

35-
if types.length > 0
36-
instances = services[types[0]]
35+
unless matching_services.empty?
36+
instances = matching_services.values[0]
3737
fail "Exactly one service instance matching '#{filter.source}' can be bound. Found #{instances.length}." if instances.length != 1
38-
3938
service = instances[0]
4039
end
4140

4241
service
4342
end
4443

44+
private
45+
46+
def self.service_instances_match(service_instances, filter)
47+
service_instances.any? do |service_instance|
48+
match = service_instance['name'] =~ filter || service_instance['label'] =~ filter
49+
50+
unless match
51+
tags = service_instance['tags']
52+
match = tags.any? { |tag| match = tag =~ filter } unless tags.nil?
53+
end
54+
55+
match
56+
end
57+
end
58+
4559
end
4660

4761
end

spec/java_buildpack/util/service_utils_spec.rb

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,45 @@ module JavaBuildpack::Util
2323

2424
let(:vcap_services) do
2525
{
26-
'newrelic-n/a' => [
27-
{ 'name' => 'new-relic' }
28-
],
29-
'elephantsql-n/a' => [
30-
{ 'name' => 'db1' },
31-
{ 'name' => 'db2' }
32-
]
26+
'newrelic-n/a' => [
27+
{ 'name' => 'new-relic' }
28+
],
29+
'elephantsql-n/a' => [
30+
{ 'name' => 'db1' },
31+
{ 'name' => 'db2' }
32+
]
33+
}
34+
end
35+
36+
let(:vcap_services_with_name) do
37+
{
38+
'name-n/a' => [
39+
{ 'name' => 'xnewrelicx' }
40+
]
41+
}
42+
end
43+
44+
let(:vcap_services_with_label) do
45+
{
46+
'name-n/a' => [
47+
{ 'label' => 'xnewrelicx' }
48+
]
49+
}
50+
end
51+
52+
let(:vcap_services_with_tags) do
53+
{
54+
'name-n/a' => [
55+
{ 'tags' => %w(y xnewrelicx z) }
56+
]
57+
}
58+
end
59+
60+
let(:vcap_services_with_plan) do
61+
{
62+
'name-n/a' => [
63+
{ 'plan' => 'xnewrelicx' }
64+
]
3365
}
3466
end
3567

@@ -49,6 +81,22 @@ module JavaBuildpack::Util
4981
expect(ServiceUtils.find_service(vcap_services, /newrelic/)).to eq(vcap_services['newrelic-n/a'][0])
5082
end
5183

84+
it 'should return the contents of the service if name matched' do
85+
expect(ServiceUtils.find_service(vcap_services_with_name, /newrelic/)).to eq(vcap_services_with_name['name-n/a'][0])
86+
end
87+
88+
it 'should return the contents of the service if label matched' do
89+
expect(ServiceUtils.find_service(vcap_services_with_label, /newrelic/)).to eq(vcap_services_with_label['name-n/a'][0])
90+
end
91+
92+
it 'should return the contents of the service if a tag matched' do
93+
expect(ServiceUtils.find_service(vcap_services_with_tags, /newrelic/)).to eq(vcap_services_with_tags['name-n/a'][0])
94+
end
95+
96+
it 'should return nil if plan would have matched' do
97+
expect(ServiceUtils.find_service(vcap_services_with_plan, /newrelic/)).to be_nil
98+
end
99+
52100
end
53101

54102
end

0 commit comments

Comments
 (0)