Skip to content

Commit 6a7fe5c

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 b3eeec9 commit 6a7fe5c

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

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,
@@ -278,7 +279,20 @@ If there is a service that matches the `spec` parameter, the value of it's
278279
`credentials` property on the service, an empty object - `{}` - will be
279280
returned.
280281

282+
**`appEnv.getServiceCredsByLabel(spec)`**
283+
--------------------------------------------------------------------------------
284+
285+
Returns the `credentials` object of a service by label.
286+
287+
The `spec` parameter is similar to that used by the
288+
`appEnv.getServiceURL()` method except matching by label instead of by name.
289+
If there is no service whose label matches the `spec` parameter,
290+
this method will return `null`.
281291

292+
If there is a service whose label matches the `spec` parameter, the value of
293+
it's `credentials` property will be returned. If for some reason, there is no
294+
`credentials` property on the service, an empty object - `{}` - will be
295+
returned.
282296

283297
testing with Cloud Foundry
284298
================================================================================

lib-src/cfenv.coffee

Lines changed: 24 additions & 1 deletion
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={}) ->
@@ -104,6 +121,12 @@ class AppEnv
104121
service = @getService spec
105122
return null unless service?
106123

124+
return service.credentials || {}
125+
#-----------------------------------------------------------------------------
126+
getServiceCredsByLabel: (spec) ->
127+
service = @getServiceByLabel spec
128+
return null unless service?
129+
107130
return service.credentials || {}
108131

109132
#-------------------------------------------------------------------------------
@@ -215,7 +238,7 @@ throwError = (message) ->
215238
throw new Error message
216239

217240
#-------------------------------------------------------------------------------
218-
# Copyright IBM Corp. 2014
241+
# Copyright IBM Corp. 2014,2016
219242
# Copyright Patrick Mueller 2015
220243
#
221244
# Licensed under the Apache License, Version 2.0 (the "License");

lib/cfenv.js

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

tests/test-core.coffee

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,37 @@ describe "appEnv", ->
231231
creds = appEnv.getServiceCreds "service-a"
232232
creds = JSON.stringify(creds)
233233
expect(creds).to.be '{"url":"foo"}'
234+
#-----------------------------------------------------------------------------
235+
it "local - getServiceCredsByLabel()", ->
236+
237+
#-------------------------------------------
238+
vcap = getVCAPServicesWithCreds "service-a",
239+
url: "foo"
240+
241+
appEnv = cfenv.getAppEnv {vcap}
242+
creds = appEnv.getServiceCredsByLabel "service-b"
243+
expect(creds).to.be null
244+
#-------------------------------------------
245+
vcap = getVCAPServicesWithCreds "service-a",
246+
url: "foo"
247+
248+
appEnv = cfenv.getAppEnv {vcap}
249+
creds = appEnv.getServiceCredsByLabel "service-a"
250+
expect(creds).to.eql {url:"foo"}
251+
#-------------------------------------------
252+
vcap = getVCAPServicesWithCreds "service-a",
253+
url: "foo"
254+
255+
appEnv = cfenv.getAppEnv {vcap}
256+
creds = appEnv.getServiceCredsByLabel /service.*/
257+
expect(creds).to.eql {url:"foo"}
258+
#-------------------------------------------
259+
vcap = getVCAPServicesWithCreds "service-a",
260+
url: "foo"
261+
262+
appEnv = cfenv.getAppEnv {vcap}
263+
creds = appEnv.getServiceCredsByLabel /disservice.*/
264+
expect(creds).to.be null
234265

235266
#-----------------------------------------------------------------------------
236267
it "remote - VCAP_APPLICATION", ->
@@ -513,7 +544,7 @@ JS = (object) -> JSON.stringify object
513544
JL = (object) -> JSON.stringify object, null, 4
514545

515546
#-------------------------------------------------------------------------------
516-
# Copyright IBM Corp. 2014
547+
# Copyright IBM Corp. 2014,2016
517548
# Copyright Patrick Mueller 2015
518549
#
519550
# Licensed under the Apache License, Version 2.0 (the "License");

0 commit comments

Comments
 (0)