-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
namespace.js
76 lines (68 loc) · 2.13 KB
/
namespace.js
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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { alias, equal } from '@ember/object/computed';
import Service, { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
import { computed } from '@ember/object';
const ROOT_NAMESPACE = '';
export default Service.extend({
store: service(),
auth: service(),
userRootNamespace: alias('auth.authData.userRootNamespace'),
//populated by the query param on the cluster route
path: '',
// list of namespaces available to the current user under the
// current namespace
accessibleNamespaces: null,
inRootNamespace: equal('path', ROOT_NAMESPACE),
currentNamespace: computed('inRootNamespace', 'path', function () {
if (this.inRootNamespace) return 'root';
const parts = this.path?.split('/');
return parts[parts.length - 1];
}),
setNamespace(path) {
if (!path) {
this.set('path', '');
return;
}
this.set('path', path);
},
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
const store = this.store;
const adapter = store.adapterFor('namespace');
const userRoot = this.auth.authData.userRootNamespace;
try {
const ns = yield adapter.findAll(store, 'namespace', null, {
adapterOptions: {
forUser: true,
namespace: userRoot,
},
});
const keys = ns.data.keys || [];
this.set(
'accessibleNamespaces',
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(/\/$/, '');
})
);
} catch (e) {
//do nothing here
}
}).drop(),
reset() {
this.set('accessibleNamespaces', null);
},
});