2018-09-25 16:28:26 +00:00
|
|
|
import { alias, equal } from '@ember/object/computed';
|
|
|
|
import Service, { inject as service } from '@ember/service';
|
2018-08-16 17:48:24 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
2018-08-22 16:13:28 +00:00
|
|
|
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
const ROOT_NAMESPACE = '';
|
|
|
|
export default Service.extend({
|
2018-09-25 16:28:26 +00:00
|
|
|
store: service(),
|
|
|
|
auth: service(),
|
|
|
|
userRootNamespace: alias('auth.authData.userRootNamespace'),
|
2018-08-16 17:48:24 +00:00
|
|
|
//populated by the query param on the cluster route
|
|
|
|
path: null,
|
|
|
|
// list of namespaces available to the current user under the
|
|
|
|
// current namespace
|
|
|
|
accessibleNamespaces: null,
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
inRootNamespace: equal('path', ROOT_NAMESPACE),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
setNamespace(path) {
|
|
|
|
this.set('path', path);
|
|
|
|
},
|
2018-08-22 16:13:28 +00:00
|
|
|
listPath: lazyCapabilities(apiPath`sys/namespaces/`, 'path'),
|
2018-09-25 16:28:26 +00:00
|
|
|
canList: alias('listPath.canList'),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
findNamespacesForUser: task(function*() {
|
|
|
|
// uses the adapter and the raw response here since
|
|
|
|
// models get wiped when switching namespaces and we
|
|
|
|
// want to keep track of these separately
|
|
|
|
let store = this.get('store');
|
|
|
|
let adapter = store.adapterFor('namespace');
|
2018-08-22 16:13:28 +00:00
|
|
|
let userRoot = this.get('auth.authData.userRootNamespace');
|
2018-08-16 17:48:24 +00:00
|
|
|
try {
|
|
|
|
let ns = yield adapter.findAll(store, 'namespace', null, {
|
|
|
|
adapterOptions: {
|
|
|
|
forUser: true,
|
2018-08-22 16:13:28 +00:00
|
|
|
namespace: userRoot,
|
2018-08-16 17:48:24 +00:00
|
|
|
},
|
|
|
|
});
|
2018-08-22 16:13:28 +00:00
|
|
|
this.set(
|
|
|
|
'accessibleNamespaces',
|
|
|
|
ns.data.keys.map(n => {
|
|
|
|
let fullNS = n;
|
|
|
|
// if the user's root isn't '', then we need to construct
|
|
|
|
// the paths so they connect to the user root to the list
|
|
|
|
// otherwise using the current ns to grab the correct leaf
|
|
|
|
// node in the graph doesn't work
|
|
|
|
if (userRoot) {
|
|
|
|
fullNS = `${userRoot}/${n}`;
|
|
|
|
}
|
|
|
|
return fullNS.replace(/\/$/, '');
|
|
|
|
})
|
|
|
|
);
|
2018-08-16 17:48:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
//do nothing here
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
});
|