UNPKG

tribune

Version:

Holy advocate to the Roman Senate, your Tribune will ensure the Consuls have your interests in mind.

184 lines (149 loc) 5.09 kB
const request = require('request'); class Consul { constructor(opts) { this._request = request.defaults(opts); this._agentUrl = opts.agentUrl; this._aclToken = opts.aclToken; this.service = { get : (...args) => this._getServices(...args), register : (...args) => this._registerService(...args), deregister: (...args) => this._deregisterService(...args), checks : (...args) => this._checkServices(...args) }; this.preparedQuery = { register : (...args) => this._registerPreparedQuery(...args), deregister: (...args) => this._deregisterPreparedQuery(...args), execute : (...args) => this._executePreparedQuery(...args), get : (...args) => this._getPreparedQueries(...args) }; } getLeader(cb = () => {}) { this._request.get(`${this._agentUrl}/v1/status/leader`, { json: true }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to get consul leader. Consul responded with a status ' + `code of ${res.statusCode}` )); } cb(null, res.body); }); } getPeers(cb = () => {}) { this._request.get(`${this._agentUrl}/v1/status/peers`, { json: true }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to get consul peers. Consul responded with a status ' + `code of ${res.statusCode}` )); } cb(null, res.body); }); } _getServices(cb = () => {}) { this._request.get(`${this._agentUrl}/v1/agent/services`, { json: true }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to get consul agent services. Tribune agent responded with a status ' + `code of ${res.statusCode}` )); } cb(null, res.body); }); } _registerService(body, cb = () => {}) { const qs = this._aclToken ? `?=${this._aclToken}` : ''; this._request.put(`${this._agentUrl}/v1/agent/service/register${qs}`, { json: body }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to register with Tribune. Tribune agent responded with a status ' + `code of ${res.statusCode}` )); } cb(null); }); } _deregisterService(id, cb = () => {}) { this._request.get(`${this._agentUrl}/v1/agent/service/deregister/${id}`, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to deregister service from Consul. Consul agent responded with a status ' + `code of ${res.statusCode}` )); } cb(null); }); } _checkServices(cb = () => {}) { this._request.get(`${this._agentUrl}/v1/agent/checks`, { json: true }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to get agent checks from Consul. Consul agent responded with a status ' + `code of ${res.statusCode}` )); } cb(null, res.body); }); } _registerPreparedQuery(body, cb = () => {}) { this._request.post(`${this._agentUrl}/v1/query`, { json: body }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to register prepared query. Consul responded with a status ' + `code of ${res.statusCode}` )); } cb(null, res.body.ID); }); } _deregisterPreparedQuery(id, cb = () => {}) { this._request.delete(`${this._agentUrl}/v1/query/${id}`, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to deregister prepared query. Consul responded with a status ' + `code of ${res.statusCode}` )); } cb(null); }); } _getPreparedQueries(cb = () => {}) { this._request.get(`${this._agentUrl}/v1/query`, { json: true }, (err, res) => { if (err) { return cb(err); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to get consul prepared queries. Consul responded ' + `with a status code of ${res.statusCode}` )); } cb(null, res.body); }); } _executePreparedQuery(service, limit, cb = () => {}) { this._request.get(`${this._agentUrl}/v1/query/${service}/execute`, { json: true, qs: { limit } }, (err, res) => { if (err) { return cb(err); } if (res.statusCode === 404) { return cb(null); } if (res.statusCode !== 200) { return cb(new Error( 'Failed to execute consul prepared query. Consul responded ' + `with a status code of ${res.statusCode}` )); } cb(null, res.body); }); } } module.exports = Consul;