Skip to content

Commit 3a41cd7

Browse files
committed
add getServiceCredsByLabel
getServiceCredsByLabel is like getServiceCreds except that the label is used. * internal function getServiceByLabel added * docs update * tests update Note, other get*ByLabel functions may be desired. This is just a starting point. See discussion #3
1 parent 4c29ce1 commit 3a41cd7

5 files changed

Lines changed: 105 additions & 5 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ The returned object also has the following methods available:
106106
* `appEnv.getService(spec)`
107107
* `appEnv.getServiceURL(spec, replacements)`
108108
* `appEnv.getServiceCreds(spec)`
109+
* `appEnv.getServiceCredsByLabel(spec)`
109110

110111
If no value can be determined for `port`, and the `name` property on the
111112
`options` parameter is not set and cannot be determined,
@@ -285,7 +286,20 @@ If there is a service that matches the `spec` parameter, the value of it's
285286
`credentials` property on the service, an empty object - `{}` - will be
286287
returned.
287288

289+
**`appEnv.getServiceCredsByLabel(spec)`**
290+
--------------------------------------------------------------------------------
291+
292+
Returns the `credentials` object of a service by label.
293+
294+
The `spec` parameter is similar to that used by the
295+
`appEnv.getServiceURL()` method except matching by label instead of by name.
296+
If there is no service whose label matches the `spec` parameter,
297+
this method will return `null`.
288298

299+
If there is a service whose label matches the `spec` parameter, the value of
300+
it's `credentials` property will be returned. If for some reason, there is no
301+
`credentials` property on the service, an empty object - `{}` - will be
302+
returned.
289303

290304
testing with Cloud Foundry
291305
================================================================================

lib-src/cfenv.coffee

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ class AppEnv
6969

7070
# no matches
7171
return null
72+
#-----------------------------------------------------------------------------
73+
getServiceByLabel: (spec) ->
74+
75+
# set our matching function
76+
if _.isRegExp spec
77+
matches = (label) -> label.match spec
78+
else
79+
spec = "#{spec}"
80+
matches = (label) -> label is spec
81+
82+
services = @getServices()
83+
for label, service of services
84+
if matches label
85+
return service
86+
87+
# no matches
88+
return null
7289

7390
#-----------------------------------------------------------------------------
7491
getServiceURL: (spec, replacements={}) ->
@@ -106,6 +123,12 @@ class AppEnv
106123
service = @getService spec
107124
return null unless service?
108125

126+
return service.credentials || {}
127+
#-----------------------------------------------------------------------------
128+
getServiceCredsByLabel: (spec) ->
129+
service = @getServiceByLabel spec
130+
return null unless service?
131+
109132
return service.credentials || {}
110133

111134
#-------------------------------------------------------------------------------

lib/cfenv.js

Lines changed: 36 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test-core.coffee

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,37 @@ describe "appEnv", ->
241241
creds = appEnv.getServiceCreds "service-a"
242242
creds = JSON.stringify(creds)
243243
expect(creds).to.be '{"url":"foo"}'
244+
#-----------------------------------------------------------------------------
245+
it "local - getServiceCredsByLabel()", ->
246+
247+
#-------------------------------------------
248+
vcap = getVCAPServicesWithCreds "service-a",
249+
url: "foo"
250+
251+
appEnv = cfenv.getAppEnv {vcap}
252+
creds = appEnv.getServiceCredsByLabel "service-b"
253+
expect(creds).to.be null
254+
#-------------------------------------------
255+
vcap = getVCAPServicesWithCreds "service-a",
256+
url: "foo"
257+
258+
appEnv = cfenv.getAppEnv {vcap}
259+
creds = appEnv.getServiceCredsByLabel "service-a"
260+
expect(creds).to.eql {url:"foo"}
261+
#-------------------------------------------
262+
vcap = getVCAPServicesWithCreds "service-a",
263+
url: "foo"
264+
265+
appEnv = cfenv.getAppEnv {vcap}
266+
creds = appEnv.getServiceCredsByLabel /service.*/
267+
expect(creds).to.eql {url:"foo"}
268+
#-------------------------------------------
269+
vcap = getVCAPServicesWithCreds "service-a",
270+
url: "foo"
271+
272+
appEnv = cfenv.getAppEnv {vcap}
273+
creds = appEnv.getServiceCredsByLabel /disservice.*/
274+
expect(creds).to.be null
244275

245276
#-----------------------------------------------------------------------------
246277
it "remote - VCAP_APPLICATION", ->

0 commit comments

Comments
 (0)