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 Apr 11, 2016
1 parent b3eeec9 commit 6a7fe5c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 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 @@ -278,7 +279,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
25 changes: 24 additions & 1 deletion 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 @@ -104,6 +121,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 Expand Up @@ -215,7 +238,7 @@ throwError = (message) ->
throw new Error message

#-------------------------------------------------------------------------------
# Copyright IBM Corp. 2014
# Copyright IBM Corp. 2014,2016
# Copyright Patrick Mueller 2015
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
31 changes: 31 additions & 0 deletions lib/cfenv.js

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

33 changes: 32 additions & 1 deletion tests/test-core.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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 Expand Up @@ -513,7 +544,7 @@ JS = (object) -> JSON.stringify object
JL = (object) -> JSON.stringify object, null, 4

#-------------------------------------------------------------------------------
# Copyright IBM Corp. 2014
# Copyright IBM Corp. 2014,2016
# Copyright Patrick Mueller 2015
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down

0 comments on commit 6a7fe5c

Please sign in to comment.