{"size": 1620, "ext": "js", "max_stars_repo_path": "lib/opensesame.js", "max_stars_repo_name": "cliffano/open-sesame", "max_stars_repo_stars_event_min_datetime": "2018-01-09T11:41:00.000Z", "max_stars_repo_stars_event_max_datetime": "2018-03-02T05:33:02.000Z", "max_issues_repo_path": "lib/opensesame.js", "max_issues_repo_name": "cliffano/open-sesame", "max_issues_repo_issues_event_min_datetime": "2018-06-08T00:56:36.000Z", "max_issues_repo_issues_event_max_datetime": "2018-06-08T00:56:36.000Z", "max_forks_repo_path": "lib/opensesame.js", "max_forks_repo_name": "cliffano/open-sesame", "max_forks_repo_forks_event_min_datetime": "2018-01-08T04:55:54.000Z", "max_forks_repo_forks_event_max_datetime": "2018-01-08T04:55:54.000Z"}
{"max_stars_count": 3, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 25.7142857143, "max_line_length": 98, "alphanum_fraction": 0.6382716049}
const async = require('async');
const Aws   = require('./service/aws.js');
const ipify = require('ipify');
const util  = require('util');

/**
 * class OpenSesame
 */
function OpenSesame(opts) {
  this.opts = opts || {};
  this.opts.port = this.opts.port || 22;
  this.opts.protocol = this.opts.protocol || 'tcp';
  this.opts.dryRun = this.opts.dryRun || false;
  this.opts.name = this.opts.name || 'open-sesame';
}

/**
 * Add public IP address to AWS security group inbound rules.
 *
 * @param {String} region: AWS region
 * @param {Array} secGroupIds: an array of AWS security group IDs
 * @param {Function} cb: standard cb(err, result) callback
 */
OpenSesame.prototype.aws = function (region, secGroupIds, cb) {

  var self = this;

  function createAddIpToSecGroupTask(aws, ip, secGroupId) {
    function task(cb) {
      aws.addIpToSecGroup(self.opts.protocol, ip, self.opts.port, secGroupId, self.opts.name, cb);
    }
    return task;
  }
  function addPublicIp(err, ip) {
    if (err) {
      cb(err);
    } else {
      var tasks = [];
      var aws = new Aws(region, self.opts);
      secGroupIds.forEach(function (secGroupId) {
        tasks.push(createAddIpToSecGroupTask(aws, ip, secGroupId));
      });
      async.series(tasks, cb);
    }
  }

  this._getPublicIp(addPublicIp);
};

OpenSesame.prototype._getPublicIp = function (cb) {
  console.log('--\nRetrieving public IP address...');
  ipify().then(ip => {
    console.log(util.format('IP is %s', ip));
    cb(null, ip);
  })
  .catch((err) => {
    console.error('Unable to retrieve public IP');
    cb(err);
  });
};

module.exports = OpenSesame;
