Skip to content

Commit

Permalink
add getServiceCredsByLabel
Browse files Browse the repository at this point in the history
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
  • Loading branch information
srl295 committed Feb 2, 2017
1 parent 4c29ce1 commit 3a41cd7
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The returned object also has the following methods available:
* `appEnv.getService(spec)`
* `appEnv.getServiceURL(spec, replacements)`
* `appEnv.getServiceCreds(spec)`
* `appEnv.getServiceCredsByLabel(spec)`

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

**`appEnv.getServiceCredsByLabel(spec)`**
--------------------------------------------------------------------------------

Returns the `credentials` object of a service by label.

The `spec` parameter is similar to that used by the
`appEnv.getServiceURL()` method except matching by label instead of by name.
If there is no service whose label matches the `spec` parameter,
this method will return `null`.

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

testing with Cloud Foundry
================================================================================
Expand Down
23 changes: 23 additions & 0 deletions lib-src/cfenv.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ class AppEnv

# no matches
return null
#-----------------------------------------------------------------------------
getServiceByLabel: (spec) ->

# set our matching function
if _.isRegExp spec
matches = (label) -> label.match spec
else
spec = "#{spec}"
matches = (label) -> label is spec

services = @getServices()
for label, service of services
if matches label
return service

# no matches
return null

#-----------------------------------------------------------------------------
getServiceURL: (spec, replacements={}) ->
Expand Down Expand Up @@ -106,6 +123,12 @@ class AppEnv
service = @getService spec
return null unless service?

return service.credentials || {}
#-----------------------------------------------------------------------------
getServiceCredsByLabel: (spec) ->
service = @getServiceByLabel spec
return null unless service?

return service.credentials || {}

#-------------------------------------------------------------------------------
Expand Down
40 changes: 36 additions & 4 deletions lib/cfenv.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/test-core.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ describe "appEnv", ->
creds = appEnv.getServiceCreds "service-a"
creds = JSON.stringify(creds)
expect(creds).to.be '{"url":"foo"}'
#-----------------------------------------------------------------------------
it "local - getServiceCredsByLabel()", ->

#-------------------------------------------
vcap = getVCAPServicesWithCreds "service-a",
url: "foo"

appEnv = cfenv.getAppEnv {vcap}
creds = appEnv.getServiceCredsByLabel "service-b"
expect(creds).to.be null
#-------------------------------------------
vcap = getVCAPServicesWithCreds "service-a",
url: "foo"

appEnv = cfenv.getAppEnv {vcap}
creds = appEnv.getServiceCredsByLabel "service-a"
expect(creds).to.eql {url:"foo"}
#-------------------------------------------
vcap = getVCAPServicesWithCreds "service-a",
url: "foo"

appEnv = cfenv.getAppEnv {vcap}
creds = appEnv.getServiceCredsByLabel /service.*/
expect(creds).to.eql {url:"foo"}
#-------------------------------------------
vcap = getVCAPServicesWithCreds "service-a",
url: "foo"

appEnv = cfenv.getAppEnv {vcap}
creds = appEnv.getServiceCredsByLabel /disservice.*/
expect(creds).to.be null

#-----------------------------------------------------------------------------
it "remote - VCAP_APPLICATION", ->
Expand Down

0 comments on commit 3a41cd7

Please sign in to comment.