guidesmiths/rascal

View on GitHub
lib/management/Client.js

Summary

Maintainability
A
0 mins
Test Coverage
A
93%
const debug = require('debug')('rascal:management:client');
const format = require('util').format;
const _ = require('lodash');
const defaultAgent = require('superagent');

function Client(suppliedAgent) {
  const agent = suppliedAgent || defaultAgent;
  const self = this;

  this.assertVhost = function (name, config, next) {
    debug('Asserting vhost: %s', name);
    const options = getVhostOptions(name, config);
    self._request('put', options.url, options.timeout, (err) => {
      if (!err) return next();
      const _err = err.status ? new Error(format('Failed to assert vhost: %s. %s returned status %d', name, config.loggableUrl, err.status)) : err;
      return next(_err);
    });
  };

  this.checkVhost = function (name, config, next) {
    debug('Checking vhost: %s', name);
    const options = getVhostOptions(name, config);
    self._request('get', options.url, options.timeout, (err) => {
      if (!err) return next();
      const _err = err.status ? new Error(format('Failed to check vhost: %s. %s returned status %d', name, config.loggableUrl, err.status)) : err;
      return next(_err);
    });
  };

  this.deleteVhost = function (name, config, next) {
    debug('Deleting vhost: %s', name);
    const options = getVhostOptions(name, config);
    self._request('delete', options.url, options.timeout, (err) => {
      if (!err) return next();
      const _err = err.status ? new Error(format('Failed to delete vhost: %s. %s returned status %d', name, config.loggableUrl, err.status)) : err;
      return next(_err);
    });
  };

  this._request = function (method, url, timeout, next) {
    agent[method](url)
      .timeout({ deadline: timeout })
      .then(() => {
        next();
      })
      .catch(next);
  };

  function getVhostOptions(name, config) {
    const url = format('%s/%s/%s', config.url, 'api/vhosts', name);
    return _.defaultsDeep({ url }, config.options);
  }
}

module.exports = Client;