all files / src/utils/ ClusterPayload.js

14.71% Statements 5/34
0% Branches 0/24
0% Functions 0/3
14.71% Lines 5/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                                                                                                           
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = getClusterPayload;
function tradClusterPayload(id) {
  if (!id) {
    throw Error('missing id in traditional cluster payload');
  }
  return {
    _id: id
  };
}
 
function ec2ClusterPayload(name, machine) {
  var clusterSize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  var profileId = arguments[3];
  var clusterNames = arguments[4];
 
  if (typeof name === 'undefined') {
    throw Error('Missing required field: "name"');
  } else if (typeof machine === 'undefined') {
    throw Error('Missing required field: "machine"');
  } else if (typeof profileId === 'undefined') {
    throw Error('Missing required field: "profileId"');
  } else if (clusterNames.indexOf(name.trim()) !== -1) {
    throw Error('An EC2 instance with this name already exists');
  }
 
  var _clusterSize = Number(clusterSize);
  if (Number.isNaN(_clusterSize)) {
    _clusterSize = 1;
  } else if (clusterSize <= 0) {
    throw Error('Cluster must be greater than zero');
  }
 
  return {
    serverType: 'ec2',
    machine: machine,
    name: name.trim(),
    clusterSize: _clusterSize,
    profileId: profileId
  };
}
 
function getClusterPayload(type, options, clusterNames) {
  if (type === 'EC2') {
    var name = options.name,
        machine = options.machine,
        clusterSize = options.clusterSize,
        profile = options.profile,
        cluster = options.cluster;
 
    if (cluster) {
      return { _id: cluster };
    }
    return ec2ClusterPayload(name, machine, clusterSize, profile, clusterNames);
  }
  if (type === 'Traditional') {
    var _profile = options.profile;
 
    return tradClusterPayload(_profile);
  }
  return null;
}