Skip to content

add getServiceCredsByLabel #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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