aboutsummaryrefslogtreecommitdiffstats
path: root/lib/consul.js
diff options
context:
space:
mode:
authorAlex Shatov <alexs@att.com>2017-09-15 13:52:31 -0400
committerAlex Shatov <alexs@att.com>2017-09-15 13:52:31 -0400
commitb511ff360603599c43e066722d3902a55fac3f6d (patch)
tree7caa5c8ceb51568b444f0bd4ed9158fe92e8355e /lib/consul.js
parent3486bd1a77a8a3f198de171fda34c102406a26ea (diff)
4.2.0 - added the policy-update handling
* API version 4.1.0 - added /policy, /swagger-ui * new: policy update with queuing of execute-operations * more data on info - branch+commit-datetime * expecting consul ip to be in /etc/hosts * added swagger-ui and added policy to *API.yaml * common logging - more audits and metrics records - directly in promise_request Change-Id: I7d32f34100a16b5b7293ed5afe67f5c8c3098495 Issue-Id: DCAEGEN2-62 Signed-off-by: Alex Shatov <alexs@att.com>
Diffstat (limited to 'lib/consul.js')
-rw-r--r--lib/consul.js88
1 files changed, 42 insertions, 46 deletions
diff --git a/lib/consul.js b/lib/consul.js
index 5b76ffc..3a3257b 100644
--- a/lib/consul.js
+++ b/lib/consul.js
@@ -1,16 +1,16 @@
/*
-Copyright(c) 2017 AT&T Intellectual Property. All rights reserved.
+Copyright(c) 2017 AT&T Intellectual Property. All rights reserved.
-Licensed under the Apache License, Version 2.0 (the "License");
+Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
-Unless required by applicable law or agreed to in writing,
+Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-CONDITIONS OF ANY KIND, either express or implied.
+CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
*/
@@ -18,49 +18,45 @@ See the License for the specific language governing permissions and limitations
const KEY = '/v1/kv/';
const SERVICE = '/v1/catalog/service/';
-const DEFAULT_URL = 'http://localhost:8500';
+const CONSUL = 'consul';
+const CONSUL_URL = 'http://' + (process.env.CONSUL_HOST || CONSUL) + ':8500';
const doRequest = require('./promise_request').doRequest;
-module.exports = function(options) {
- const url = options.url || DEFAULT_URL;
-
- return {
-
- /* Fetch (a promise for) the decoded value of a single key from Consul KV store.
- * If the value is a string representation of a JSON object, return as an object.
- * If there is no such key, resolve to null.
- */
- getKey: function(key) {
- return doRequest({method: 'GET', uri: url + KEY + key + '?raw'})
- .then(function(res) {
- return res.json || res.body;
- })
- .catch(function(err) {
- if (err.status === 404) {
- /* Key wasn't found */
- return null;
- }
- else {
- /* Some other error, rethrow it */
- throw err;
- }
- });
- },
-
- /* Retrieve (a promise for) address:port information for a named service from the Consul service catalog.
- * If the service has tag(s), return the first one. (Should be the full URL of the service if it exists.
- * Since a service can be registered at multiple nodes, the result is an array.
- * If the service is not found, returns a zero-length array.
- */
- getService: function(serviceId) {
- return doRequest({method: 'GET', uri: url + SERVICE + serviceId})
- .then(function(res){
- return res.json.map(function(r) {
- /* Address for external service is in r.Address with r.ServiceAddress empty */
- return {address: r.ServiceAddress || r.Address, port: r.ServicePort, url: r.ServiceTags ? r.ServiceTags[0] : ""};
- });
- });
- }
- }
+module.exports = {
+ /* Fetch (a promise for) the decoded value of a single key from Consul KV store.
+ * If the value is a string representation of a JSON object, return as an object.
+ * If there is no such key, resolve to null.
+ */
+ getKey: function(key) {
+ return doRequest({method: 'GET', uri: CONSUL_URL + KEY + key + '?raw'}, null, CONSUL)
+ .then(function(res) {
+ return res.json || res.body;
+ })
+ .catch(function(err) {
+ if (err.status === 404) {
+ /* Key wasn't found */
+ return null;
+ }
+ else {
+ /* Some other error, rethrow it */
+ throw err;
+ }
+ });
+ },
+
+ /* Retrieve (a promise for) address:port information for a named service from the Consul service catalog.
+ * If the service has tag(s), return the first one. (Should be the full URL of the service if it exists.
+ * Since a service can be registered at multiple nodes, the result is an array.
+ * If the service is not found, returns a zero-length array.
+ */
+ getService: function(serviceId) {
+ return doRequest({method: 'GET', uri: CONSUL_URL + SERVICE + serviceId}, null, CONSUL)
+ .then(function(res){
+ return res.json.map(function(r) {
+ /* Address for external service is in r.Address with r.ServiceAddress empty */
+ return {address: r.ServiceAddress || r.Address, port: r.ServicePort, url: r.ServiceTags ? r.ServiceTags[0] : ""};
+ });
+ });
+ }
}; \ No newline at end of file