all files / src/redux/actions/ clusters.js

78.89% Statements 157/199
59.09% Branches 26/44
69.64% Functions 39/56
81.48% Lines 154/189
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                                                                                                                                                                                                                                                                           
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RESTRICTED_CLUSTER_LOG = exports.APPEND_TO_CLUSTER_LOG = exports.UPDATE_CLUSTER_LOG = exports.TESTING_CLUSTER = exports.CLUSTER_APPLY_PRESET = exports.PENDING_CLUSTER_NETWORK = exports.SAVE_CLUSTER = exports.REMOVE_CLUSTER = exports.UPDATE_CLUSTER_STATUS = exports.UPDATE_CLUSTER_PRESETS = exports.UPDATE_CLUSTERS = exports.UPDATE_ACTIVE_CLUSTER = exports.UPDATE_EXISTING_CLUSTER = exports.REMOVE_CLUSTER_BY_ID = exports.ADD_EXISTING_CLUSTER = exports.ADD_CLUSTER = undefined;
exports.addCluster = addCluster;
exports.addExistingCluster = addExistingCluster;
exports.applyPreset = applyPreset;
exports.removeClusterById = removeClusterById;
exports.updateActiveCluster = updateActiveCluster;
exports.updateExistingCluster = updateExistingCluster;
exports.updateClusters = updateClusters;
exports.updateClusterPresets = updateClusterPresets;
exports.pendingNetworkCall = pendingNetworkCall;
exports.updateClusterLog = updateClusterLog;
exports.appendToClusterLog = appendToClusterLog;
exports.restrictedClusterLog = restrictedClusterLog;
exports.updateClusterStatus = updateClusterStatus;
exports.getClusterLog = getClusterLog;
exports.fetchCluster = fetchCluster;
exports.fetchClusters = fetchClusters;
exports.fetchClusterPresets = fetchClusterPresets;
exports.removeCluster = removeCluster;
exports.deleteCluster = deleteCluster;
exports.saveCluster = saveCluster;
exports.updateCluster = updateCluster;
exports.testCluster = testCluster;
exports.terminateCluster = terminateCluster;
 
var _network = require('./network');
 
var netActions = _interopRequireWildcard(_network);
 
var _taskflows = require('./taskflows');
 
var TaskflowActions = _interopRequireWildcard(_taskflows);
 
var _network2 = require('../../network');
 
var _network3 = _interopRequireDefault(_network2);
 
var _clusters = require('../../network/helpers/clusters');
 
var ClusterHelper = _interopRequireWildcard(_clusters);
 
var _ = require('..');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
var ADD_CLUSTER = exports.ADD_CLUSTER = 'ADD_CLUSTER';
var ADD_EXISTING_CLUSTER = exports.ADD_EXISTING_CLUSTER = 'ADD_EXISTING_CLUSTER';
var REMOVE_CLUSTER_BY_ID = exports.REMOVE_CLUSTER_BY_ID = 'REMOVE_CLUSTER_BY_ID';
var UPDATE_EXISTING_CLUSTER = exports.UPDATE_EXISTING_CLUSTER = 'UPDATE_EXISTING_CLUSTER';
var UPDATE_ACTIVE_CLUSTER = exports.UPDATE_ACTIVE_CLUSTER = 'UPDATE_ACTIVE_CLUSTER';
var UPDATE_CLUSTERS = exports.UPDATE_CLUSTERS = 'UPDATE_CLUSTERS';
var UPDATE_CLUSTER_PRESETS = exports.UPDATE_CLUSTER_PRESETS = 'UPDATE_CLUSTER_PRESETS';
var UPDATE_CLUSTER_STATUS = exports.UPDATE_CLUSTER_STATUS = 'UPDATE_CLUSTER_STATUS';
var REMOVE_CLUSTER = exports.REMOVE_CLUSTER = 'REMOVE_CLUSTER';
var SAVE_CLUSTER = exports.SAVE_CLUSTER = 'SAVE_CLUSTER';
var PENDING_CLUSTER_NETWORK = exports.PENDING_CLUSTER_NETWORK = 'PENDING_CLUSTER_NETWORK';
var CLUSTER_APPLY_PRESET = exports.CLUSTER_APPLY_PRESET = 'CLUSTER_APPLY_PRESET';
var TESTING_CLUSTER = exports.TESTING_CLUSTER = 'TESTING_CLUSTER';
var UPDATE_CLUSTER_LOG = exports.UPDATE_CLUSTER_LOG = 'UPDATE_CLUSTER_LOG';
var APPEND_TO_CLUSTER_LOG = exports.APPEND_TO_CLUSTER_LOG = 'APPEND_TO_CLUSTER_LOG';
var RESTRICTED_CLUSTER_LOG = exports.RESTRICTED_CLUSTER_LOG = 'RESTRICTED_CLUSTER_LOG';
 
/* eslint-disable no-shadow */
 
function addCluster() {
  return { type: ADD_CLUSTER };
}
 
// called if we get a cluster sse, we add it to the mapById
function addExistingCluster(cluster) {
  return { type: ADD_EXISTING_CLUSTER, cluster: cluster };
}
 
function applyPreset(index, name) {
  return { type: CLUSTER_APPLY_PRESET, index: index, name: name };
}
 
// removes cluster from mapById
// (see removeCluster(index, cluster) belows)
function removeClusterById(id) {
  return { type: REMOVE_CLUSTER_BY_ID, id: id };
}
 
function updateActiveCluster(index) {
  return { type: UPDATE_ACTIVE_CLUSTER, index: index };
}
 
function updateExistingCluster(cluster) {
  return { type: UPDATE_EXISTING_CLUSTER, cluster: cluster };
}
 
function updateClusters(clusters) {
  return { type: UPDATE_CLUSTERS, clusters: clusters };
}
 
function updateClusterPresets(presets) {
  return { type: UPDATE_CLUSTER_PRESETS, presets: presets };
}
 
function pendingNetworkCall() {
  var pending = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
 
  return { type: PENDING_CLUSTER_NETWORK, pending: pending };
}
 
function updateClusterLog(id, log) {
  return { type: UPDATE_CLUSTER_LOG, id: id, log: log };
}
 
function appendToClusterLog(id, logEntry) {
  return { type: APPEND_TO_CLUSTER_LOG, id: id, logEntry: logEntry };
}
 
function restrictedClusterLog(id) {
  return { type: RESTRICTED_CLUSTER_LOG, id: id };
}
 
function updateTaskflowActionsForClusterEvent(cluster, status) {
  if (cluster.type !== 'ec2') {
    return;
  }
  var tfMapById = Object.assign({}, _.store.getState().taskflows.mapById);
  var keys = Object.keys(tfMapById);
  for (var i = 0; i < keys.length; i++) {
    var taskflow = tfMapById[keys[i]];
    if (!taskflow.flow.meta) {
      (0, _.dispatch)(TaskflowActions.fetchTaskflow(taskflow.flow._id));
    } else {
      (0, _.dispatch)(TaskflowActions.triggerUpdate(taskflow.flow._id));
    }
  }
}
 
function updateClusterStatus(id, status) {
  // for taskflows on ec2 the meta object is not as readily available
  // this is due to fewer jobs coming through SSE which triggers a fetch for trad clusters.
  updateTaskflowActionsForClusterEvent(_.store.getState().preferences.clusters.mapById[id], status);
  return { type: UPDATE_CLUSTER_STATUS, id: id, status: status };
}
 
function getClusterLog(id, offset) {
  return function (dispatch) {
    var action = netActions.addNetworkCall('cluster_log_' + id, 'Check cluster log');
    _network3.default.getClusterLog(id, offset).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      if (!offset) {
        // offset is 0 or undefined
        dispatch(updateClusterLog(id, resp.data.log));
      } else {
        dispatch(appendToClusterLog(id, resp.data.log));
      }
    }).catch(function (error) {
      dispatch(netActions.errorNetworkCall(action.id, error));
    });
    return action;
  };
}
 
function fetchCluster(id) {
  var taskflowIdToUpdate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
 
  return function (dispatch) {
    var action = netActions.addNetworkCall('fetch_cluster', 'Retreive cluster');
    _network3.default.getCluster(id).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(addExistingCluster(resp.data));
      Iif (taskflowIdToUpdate.length) {
        dispatch(TaskflowActions.triggerUpdate(taskflowIdToUpdate));
      }
    }, function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err));
    });
 
    return action;
  };
}
 
function fetchClusters(type) {
  // this can be called excessively from a log or status event, this acts as our debounce
  Iif (_.store.getState().preferences.clusters.pending) {
    return { type: 'NOOP' };
  }
  return function (dispatch) {
    var action = netActions.addNetworkCall('fetch_clusters', 'Retreive clusters');
    dispatch(pendingNetworkCall(true));
    _network3.default.listClusters(type).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(updateClusters(resp.data));
      dispatch(pendingNetworkCall(false));
    }, function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err));
      dispatch(pendingNetworkCall(false));
    });
 
    return action;
  };
}
 
function fetchClusterPresets() {
  return function (dispatch) {
    var action = netActions.addNetworkCall('fetch_cluster_presets', 'Retreive cluster presets');
    _network3.default.getClusterPresets().then(function (presets) {
      dispatch(netActions.successNetworkCall(action.id, presets));
      dispatch(updateClusterPresets(presets.data));
    }, function (error) {
      dispatch(netActions.errorNetworkCall(action.id, error));
    });
 
    return action;
  };
}
 
// removes a cluster from the preferences page and deletes it if has _id
function removeCluster(index, cluster) {
  if (!cluster || !cluster._id) {
    return { type: REMOVE_CLUSTER, index: index };
  }
 
  return function (dispatch) {
    var action = netActions.addNetworkCall('remove_cluster', 'Remove cluster');
 
    _network3.default.deleteCluster(cluster._id).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(fetchClusters());
    }, function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err, 'form'));
    });
 
    return action;
  };
}
 
// deletes a cluster by id, different from removeCluster(index, cluster) above
function deleteCluster(id) {
  return function (dispatch) {
    var action = netActions.addNetworkCall('delete_cluster', 'Delete cluster');
 
    _network3.default.deleteCluster(id).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(removeClusterById(id));
    }, function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err, 'form'));
    });
 
    return action;
  };
}
 
function saveCluster(index, cluster) {
  var pushToServer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
 
  var saveAction = { type: SAVE_CLUSTER, index: index, cluster: cluster };
  if (pushToServer) {
    var action = netActions.addNetworkCall('save_cluster', 'Save cluster');
    ClusterHelper.saveCluster(cluster).then(function (resp) {
      (0, _.dispatch)(netActions.successNetworkCall(action.id, resp));
    }, function (err) {
      (0, _.dispatch)(netActions.errorNetworkCall(action.id, err, 'form'));
    });
  }
  return saveAction;
}
 
function updateCluster(cluster) {
  return function (dispatch) {
    var action = netActions.addNetworkCall('save_cluster', 'Save cluster');
    _network3.default.updateCluster(cluster).then(function (resp) {
      dispatch(updateExistingCluster(resp.data));
      dispatch(netActions.successNetworkCall(action.id, resp));
    }).catch(function (err) {
      console.log(err);
      dispatch(netActions.errorNetworkCall(action.id, err, 'form'));
    });
    return action;
  };
}
 
function testCluster(index, cluster) {
  if (!cluster._id) {
    return { type: 'NOOP', message: 'Cluster is not available on server yet' };
  }
  return function (dispatch) {
    var action = netActions.addNetworkCall('test_cluster', 'Test cluster');
    dispatch({ type: TESTING_CLUSTER, index: index });
    _network3.default.testCluster(cluster._id).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
    }, function (error) {
      dispatch(netActions.errorNetworkCall(action.id, error, 'form'));
    });
    return action;
  };
}
 
function terminateCluster(id) {
  return function (dispatch) {
    var action = netActions.addNetworkCall('terminate_cluster_' + id, 'terminate cluster');
    _network3.default.terminateCluster(id).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
    }).catch(function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err, 'form'));
    });
    return action;
  };
}
 
// Auto trigger actions on authentication change...
_network3.default.onAuthChange(function (authenticated) {
  if (!authenticated) {
    (0, _.dispatch)(updateClusters([]));
    (0, _.dispatch)(updateClusterPresets({}));
  }
});