2018-09-25 16:28:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { reject } from 'rsvp';
|
|
|
|
import Route from '@ember/routing/route';
|
|
|
|
import { getOwner } from '@ember/application';
|
2018-10-01 18:04:34 +00:00
|
|
|
import { task, timeout } from 'ember-concurrency';
|
2018-04-03 14:16:57 +00:00
|
|
|
import Ember from 'ember';
|
|
|
|
import ClusterRoute from 'vault/mixins/cluster-route';
|
|
|
|
import ModelBoundaryRoute from 'vault/mixins/model-boundary-route';
|
|
|
|
|
|
|
|
const POLL_INTERVAL_MS = 10000;
|
|
|
|
|
2018-08-16 17:48:24 +00:00
|
|
|
export default Route.extend(ModelBoundaryRoute, ClusterRoute, {
|
2018-09-25 16:28:26 +00:00
|
|
|
namespaceService: service('namespace'),
|
|
|
|
version: service(),
|
2019-01-18 22:04:40 +00:00
|
|
|
permissions: service(),
|
2018-09-25 16:28:26 +00:00
|
|
|
store: service(),
|
|
|
|
auth: service(),
|
|
|
|
currentCluster: service(),
|
|
|
|
modelTypes: computed(function() {
|
|
|
|
return ['node', 'secret', 'secret-engine'];
|
|
|
|
}),
|
|
|
|
globalNamespaceModels: computed(function() {
|
|
|
|
return ['node', 'cluster'];
|
|
|
|
}),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
queryParams: {
|
|
|
|
namespaceQueryParam: {
|
|
|
|
refreshModel: true,
|
|
|
|
},
|
|
|
|
},
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
getClusterId(params) {
|
|
|
|
const { cluster_name } = params;
|
|
|
|
const cluster = this.modelFor('vault').findBy('name', cluster_name);
|
|
|
|
return cluster ? cluster.get('id') : null;
|
|
|
|
},
|
|
|
|
|
2018-08-16 17:48:24 +00:00
|
|
|
clearNonGlobalModels() {
|
|
|
|
// this method clears all of the ember data cached models except
|
|
|
|
// the model types blacklisted in `globalNamespaceModels`
|
|
|
|
let store = this.store;
|
|
|
|
let modelsToKeep = this.get('globalNamespaceModels');
|
2018-09-25 16:28:26 +00:00
|
|
|
for (let model of getOwner(this)
|
|
|
|
.lookup('data-adapter:main')
|
|
|
|
.getModelTypes()) {
|
2018-08-16 17:48:24 +00:00
|
|
|
let { name } = model;
|
|
|
|
if (modelsToKeep.includes(name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
store.unloadAll(name);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
beforeModel() {
|
|
|
|
const params = this.paramsFor(this.routeName);
|
2018-08-16 17:48:24 +00:00
|
|
|
this.clearNonGlobalModels();
|
|
|
|
this.get('namespaceService').setNamespace(params.namespaceQueryParam);
|
2018-04-03 14:16:57 +00:00
|
|
|
const id = this.getClusterId(params);
|
|
|
|
if (id) {
|
2018-05-10 21:44:17 +00:00
|
|
|
this.get('auth').setCluster(id);
|
2019-01-18 22:04:40 +00:00
|
|
|
this.get('permissions').getPaths.perform();
|
2018-05-10 21:44:17 +00:00
|
|
|
return this.get('version').fetchFeatures();
|
2018-04-03 14:16:57 +00:00
|
|
|
} else {
|
2018-09-25 16:28:26 +00:00
|
|
|
return reject({ httpStatus: 404, message: 'not found', path: params.cluster_name });
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
model(params) {
|
|
|
|
const id = this.getClusterId(params);
|
2018-05-29 14:14:31 +00:00
|
|
|
return this.get('store').findRecord('cluster', id);
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
|
2018-10-01 18:04:34 +00:00
|
|
|
poll: task(function*() {
|
2018-10-02 15:05:34 +00:00
|
|
|
while (true) {
|
|
|
|
// when testing, the polling loop causes promises to never settle so acceptance tests hang
|
|
|
|
// to get around that, we just disable the poll in tests
|
|
|
|
if (Ember.testing) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-01 18:04:34 +00:00
|
|
|
yield timeout(POLL_INTERVAL_MS);
|
|
|
|
try {
|
|
|
|
yield this.controller.model.reload();
|
|
|
|
yield this.transitionToTargetRoute();
|
|
|
|
} catch (e) {
|
|
|
|
// we want to keep polling here
|
|
|
|
}
|
2018-10-02 15:05:34 +00:00
|
|
|
}
|
2018-10-01 18:04:34 +00:00
|
|
|
})
|
|
|
|
.cancelOn('deactivate')
|
|
|
|
.keepLatest(),
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
afterModel(model) {
|
|
|
|
this._super(...arguments);
|
2018-10-01 18:04:34 +00:00
|
|
|
this.get('currentCluster').setCluster(model);
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
// Check that namespaces is enabled and if not,
|
|
|
|
// clear the namespace by transition to this route w/o it
|
|
|
|
if (this.get('namespaceService.path') && !this.get('version.hasNamespaces')) {
|
|
|
|
return this.transitionTo(this.routeName, { queryParams: { namespace: '' } });
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
return this.transitionToTargetRoute();
|
|
|
|
},
|
|
|
|
|
2018-10-01 18:04:34 +00:00
|
|
|
setupController() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.poll.perform();
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
actions: {
|
|
|
|
error(e) {
|
|
|
|
if (e.httpStatus === 503 && e.errors[0] === 'Vault is sealed') {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|