diff --git a/README.md b/README.md index 1bfb770..d64d50b 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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 ================================================================================ diff --git a/lib-src/cfenv.coffee b/lib-src/cfenv.coffee index cd9e67a..8e41b95 100644 --- a/lib-src/cfenv.coffee +++ b/lib-src/cfenv.coffee @@ -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={}) -> @@ -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 || {} #------------------------------------------------------------------------------- diff --git a/lib/cfenv.js b/lib/cfenv.js index b7b3b57..3ee52b5 100644 --- a/lib/cfenv.js +++ b/lib/cfenv.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.12.2 +// Generated by CoffeeScript 1.10.0 (function() { var AppEnv, URL, _, cfenv, fs, getApp, getBind, getName, getPort, getServices, getURLs, pkg, ports, throwError, yaml; @@ -25,6 +25,7 @@ AppEnv = (function() { function AppEnv(options) { + var error; if (options == null) { options = {}; } @@ -94,6 +95,28 @@ return null; }; + AppEnv.prototype.getServiceByLabel = function(spec) { + var label, matches, service, services; + if (_.isRegExp(spec)) { + matches = function(label) { + return label.match(spec); + }; + } else { + spec = "" + spec; + matches = function(label) { + return label === spec; + }; + } + services = this.getServices(); + for (label in services) { + service = services[label]; + if (matches(label)) { + return service; + } + } + return null; + }; + AppEnv.prototype.getServiceURL = function(spec, replacements) { var credentials, key, password, purl, service, url, userid, value; if (replacements == null) { @@ -139,12 +162,21 @@ return service.credentials || {}; }; + AppEnv.prototype.getServiceCredsByLabel = function(spec) { + var service; + service = this.getServiceByLabel(spec); + if (service == null) { + return null; + } + return service.credentials || {}; + }; + return AppEnv; })(); getApp = function(appEnv, options) { - var e, envValue, locValue, ref, string; + var e, envValue, error, locValue, ref, string; string = process.env.VCAP_APPLICATION; envValue = {}; if (string != null) { @@ -166,7 +198,7 @@ }; getServices = function(appEnv, options) { - var e, envValue, locValue, ref, string; + var e, envValue, error, locValue, ref, string; string = process.env.VCAP_SERVICES; envValue = {}; if (string != null) { @@ -204,7 +236,7 @@ }; getName = function(appEnv, options) { - var pObject, pString, ref, val, yObject, yString; + var error, pObject, pString, ref, val, yObject, yString; if (options.name != null) { return options.name; } diff --git a/lib/server.js b/lib/server.js index cdd23d0..17b0812 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.12.2 +// Generated by CoffeeScript 1.10.0 (function() { var JL, JS, cfenv, generateDump, http; diff --git a/tests/test-core.coffee b/tests/test-core.coffee index e113279..bd5784e 100644 --- a/tests/test-core.coffee +++ b/tests/test-core.coffee @@ -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", ->