Skip to content

Commit 17db82f

Browse files
committed
add getServiceCreds(), update packages, v 1.0.0
1 parent b5351d4 commit 17db82f

5 files changed

Lines changed: 80 additions & 6 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ The returned object also has the following methods available:
109109
* `appEnv.getServices()`
110110
* `appEnv.getService(spec)`
111111
* `appEnv.getServiceURL(spec, replacements)`
112+
* `appEnv.getServiceCreds(spec)`
112113

113114
If no value can be determined for `port`, and the `name` property on the
114115
`options` parameter is not set and cannot be determined,
@@ -194,7 +195,7 @@ Returns the service object from VCAP_SERVICES or null if not found.
194195

195196

196197

197-
`appEnv.getServiceURL(spec, replacements)`
198+
**`appEnv.getServiceURL(spec, replacements)`**
198199
--------------------------------------------------------------------------------
199200

200201
Returns a service URL by name.
@@ -265,6 +266,21 @@ Since the `appEnv.getServiceURL()` method operates against the
265266
`appEnv.services` property, you can fudge this object if that makes your
266267
life easier.
267268

269+
**`appEnv.getServiceCreds(spec)`**
270+
--------------------------------------------------------------------------------
271+
272+
Returns the `credentials` object of a service by name.
273+
274+
The `spec` parameter is the same as that used by the `appEnv.getServiceURL()`
275+
method. If there is no service that matches the `spec` parameter, this method
276+
will return `null`.
277+
278+
If there is a service that matches the `spec` parameter, the value of it's
279+
`credentials` property will be returned. If for some reason, there is no
280+
`credentials` property on the service, an empty object - `{}` - will be
281+
returned.
282+
283+
268284

269285
testing with Cloud Foundry
270286
================================================================================

lib-src/cfenv.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ class AppEnv
9999

100100
return URL.format purl
101101

102+
#-----------------------------------------------------------------------------
103+
getServiceCreds: (spec) ->
104+
service = @getService spec
105+
return null unless service?
106+
107+
return service.credentials || {}
108+
102109
#-------------------------------------------------------------------------------
103110
getApp = (appEnv, options) ->
104111
string = process.env.VCAP_APPLICATION

lib/cfenv.js

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

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cfenv",
33
"main": "./lib/cfenv",
44
"description": "easy access to your Cloud Foundry application environment",
5-
"version": "0.2.0",
5+
"version": "1.0.0",
66
"author": "pmuellr",
77
"license": "Apache-2.0",
88
"homepage": "https://github.com/cloudfoundry-community/node-cfenv",
@@ -11,13 +11,13 @@
1111
"url": "https://github.com/cloudfoundry-community/node-cfenv.git"
1212
},
1313
"dependencies": {
14-
"js-yaml": "3.0.x",
14+
"js-yaml": "3.2.x",
1515
"ports": "1.1.x",
16-
"underscore": "1.6.x"
16+
"underscore": "1.7.x"
1717
},
1818
"devDependencies": {
19-
"coffee-script": "1.7.x",
20-
"mocha": "1.19.x",
19+
"coffee-script": "1.8.x",
20+
"mocha": "1.21.x",
2121
"expect.js": "0.3.x"
2222
}
2323
}

tests/test-core.coffee

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ describe "appEnv", ->
190190

191191
expect(url).to.be "org-protocol://org-host:666/org-path"
192192

193+
#-----------------------------------------------------------------------------
194+
it "local - getServiceCreds()", ->
195+
196+
#-------------------------------------------
197+
vcap = getVCAPServicesWithCreds "service-a",
198+
url: "foo"
199+
200+
appEnv = cfenv.getAppEnv {vcap}
201+
creds = appEnv.getServiceCreds "service-b"
202+
expect(creds).to.be null
203+
204+
#-------------------------------------------
205+
vcap = getVCAPServicesWithCreds "service-a",
206+
url: "foo"
207+
208+
vcap["services"]["service-a-label"][0].credentials = null
209+
210+
appEnv = cfenv.getAppEnv {vcap}
211+
creds = appEnv.getServiceCreds "service-a"
212+
creds = JSON.stringify(creds)
213+
expect(creds).to.be '{}'
214+
215+
#-------------------------------------------
216+
vcap = getVCAPServicesWithCreds "service-a",
217+
url: "foo"
218+
219+
delete vcap["services"]["service-a-label"][0].credentials
220+
221+
appEnv = cfenv.getAppEnv {vcap}
222+
creds = appEnv.getServiceCreds "service-a"
223+
creds = JSON.stringify(creds)
224+
expect(creds).to.be '{}'
225+
226+
#-------------------------------------------
227+
vcap = getVCAPServicesWithCreds "service-a",
228+
url: "foo"
229+
230+
appEnv = cfenv.getAppEnv {vcap}
231+
creds = appEnv.getServiceCreds "service-a"
232+
creds = JSON.stringify(creds)
233+
expect(creds).to.be '{"url":"foo"}'
234+
193235
#-----------------------------------------------------------------------------
194236
it "remote - VCAP_APPLICATION", ->
195237

0 commit comments

Comments
 (0)