From 6a7fe5ce9922270a12ec5d106b1a2858ca07da78 Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Mon, 11 Apr 2016 15:43:11 -0700 Subject: [PATCH] add getServiceCredsByLabel 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 https://github.com/cloudfoundry-community/node-cfenv/issues/3 --- README.md | 14 ++++++++++++++ lib-src/cfenv.coffee | 25 ++++++++++++++++++++++++- lib/cfenv.js | 31 +++++++++++++++++++++++++++++++ tests/test-core.coffee | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1368791..06cdc48 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, @@ -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 ================================================================================ diff --git a/lib-src/cfenv.coffee b/lib-src/cfenv.coffee index d9e964e..77029a8 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={}) -> @@ -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 || {} #------------------------------------------------------------------------------- @@ -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"); diff --git a/lib/cfenv.js b/lib/cfenv.js index df54034..a102e97 100644 --- a/lib/cfenv.js +++ b/lib/cfenv.js @@ -95,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) { @@ -137,6 +159,15 @@ return service.credentials || {}; }; + AppEnv.prototype.getServiceCredsByLabel = function(spec) { + var service; + service = this.getServiceByLabel(spec); + if (service == null) { + return null; + } + return service.credentials || {}; + }; + return AppEnv; })(); diff --git a/tests/test-core.coffee b/tests/test-core.coffee index 43ea2fb..f4b2242 100644 --- a/tests/test-core.coffee +++ b/tests/test-core.coffee @@ -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", -> @@ -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");