UNPKG

fog-controller

Version:

Fog Controller project @ iotracks.com

186 lines (165 loc) 9.68 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _satellite = require('./../models/satellite'); var _satellite2 = _interopRequireDefault(_satellite); var _baseManager = require('./../managers/baseManager'); var _baseManager2 = _interopRequireDefault(_baseManager); var _appUtils = require('./../utils/appUtils'); var _appUtils2 = _interopRequireDefault(_appUtils); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * @file satelliteManager.js * @author Zishan Iqbal * @description */ var SatelliteManager = function (_BaseManager) { _inherits(SatelliteManager, _BaseManager); function SatelliteManager() { _classCallCheck(this, SatelliteManager); return _possibleConstructorReturn(this, (SatelliteManager.__proto__ || Object.getPrototypeOf(SatelliteManager)).apply(this, arguments)); } _createClass(SatelliteManager, [{ key: 'getEntity', value: function getEntity() { return _satellite2.default; } }, { key: 'findAll', value: function findAll() { return this.getEntity().findAll(); } }, { key: 'findBySatelliteId', value: function findBySatelliteId(satelliteId) { return _satellite2.default.find({ where: { id: satelliteId } }); } }, { key: 'findBySatelliteIds', value: function findBySatelliteIds(satelliteIds) { return _satellite2.default.findAll({ where: { id: { $in: satelliteIds } } }); } }, { key: 'findBySatelliteNameDomainAndPublicIP', value: function findBySatelliteNameDomainAndPublicIP(satelliteName, satelliteDomain, satellitePublicIP) { return _satellite2.default.find({ where: { $or: [{ name: { $like: satelliteName } }, { domain: satelliteDomain }, { publicIP: satellitePublicIP }] } }); } }, { key: 'createSatellite', value: function createSatellite(name, domain, publicIP, certFile) { var _this2 = this; if (name && domain && publicIP) { if (_appUtils2.default.isValidName(name)) { if (_appUtils2.default.isValidDomain(domain)) { if (_appUtils2.default.isValidPublicIP(publicIP)) { this.findBySatelliteNameDomainAndPublicIP(name, domain, publicIP).then(function (satellite) { if (!satellite) { if (!certFile) { _this2.create({ name: name, domain: domain, publicIP: publicIP, cert: '' }).then(function (satellite) { console.log('ComSat Created : ' + satellite.name); }); } else if (_appUtils2.default.isFileExists(certFile)) { var cert = _fs2.default.readFileSync(certFile, "utf-8"); if (_appUtils2.default.isValidCertificate(cert)) { _this2.create({ name: name, domain: domain, publicIP: publicIP, cert: _appUtils2.default.trimCertificate(cert) }).then(function (satellite) { console.log('ComSat Created : ' + satellite.name); }); } else { console.log('Error: Provided certificate is invalid. Try again with correct certificate.'); } } else { console.log('Error: Incorrect certificate file path. Try again with correct certificate file path'); } } else { console.log('Error: Following ComSat already exists with similar configurations: \n ' + satellite.name + ' (' + satellite.domain + ') ' + satellite.publicIP); } }); } else { console.log('ComSat publicIP is invalid. Try again with different ComSat publicIP.'); console.log('Please provide values in following order:\n fog-controller comsat -add <name> <domain> <publicIP> [<certFile>]'); } } else { console.log('ComSat domain is invalid. Try again with different ComSat domain.'); console.log('Please provide values in following order:\n fog-controller comsat -add <name> <domain> <publicIP> [<certFile>]'); } } else { console.log('ComSat name is invalid. Try again with different ComSat name.'); console.log('Please provide values in following order:\n fog-controller comsat -add <name> <domain> <publicIP> [<certFile>]'); } } else { console.log('Please provide values in following order:\n fog-controller comsat -add <name> <domain> <publicIP> [<certFile>]'); } } }, { key: 'removeSatellite', value: function removeSatellite(id) { if (id) { this.findBySatelliteId(id).then(function (satellite) { if (satellite) { satellite.destroy(); console.log('Success: ComSat deleted'); } else { console.log('Error: Can not find ComSat having "' + id + '" as ID'); } }); } else { console.log('Please provide values in following order: fog-controller comsat -remove <id>'); } } }, { key: 'list', value: function list() { this.find().then(function (satellite) { if (satellite && satellite.length > 0) { console.log('\n\tID | ComSat Name | Domain | Public IP'); for (var i = 0; i < satellite.length; i++) { console.log('\t' + satellite[i].id + ' | ' + satellite[i].name + ' | ' + satellite[i].domain + ' | ' + satellite[i].publicIP); } } else { console.log('No ComSat found'); } }); } }]); return SatelliteManager; }(_baseManager2.default); var instance = new SatelliteManager(); exports.default = instance;