open-nomad/ui/app/services/system.js

98 lines
3.0 KiB
JavaScript
Raw Normal View History

import Service, { inject as service } from '@ember/service';
import { computed } from '@ember/object';
2017-09-19 14:47:10 +00:00
import PromiseObject from '../utils/classes/promise-object';
import PromiseArray from '../utils/classes/promise-array';
2017-09-19 14:47:10 +00:00
import { namespace } from '../adapters/application';
export default Service.extend({
token: service(),
store: service(),
2017-09-19 14:47:10 +00:00
leader: computed(function() {
const token = this.get('token');
2017-09-19 14:47:10 +00:00
return PromiseObject.create({
promise: token
.authorizedRequest(`/${namespace}/status/leader`)
2017-09-19 14:47:10 +00:00
.then(res => res.json())
.then(rpcAddr => ({ rpcAddr }))
.then(leader => {
// Dirty self so leader can be used as a dependent key
this.notifyPropertyChange('leader.rpcAddr');
return leader;
}),
});
}),
regions: computed(function() {
const token = this.get('token');
return PromiseArray.create({
promise: token.authorizedRequest(`/${namespace}/regions`).then(res => res.json()),
});
}),
activeRegion: computed('regions.[]', {
get() {
const regions = this.get('regions');
const region = window.localStorage.nomadActiveRegion;
if (regions.includes(region)) {
return region;
}
// If the region in localStorage is no longer in the cluster, it needs to
// be cleared from localStorage
this.set('activeRegion', null);
return null;
},
set(key, value) {
if (value == null) {
window.localStorage.removeItem('nomadActiveRegion');
} else {
// All localStorage values are strings. Stringify first so
// the return value is consistent with what is persisted.
const strValue = value + '';
window.localStorage.nomadActiveRegion = strValue;
return strValue;
}
},
}),
namespaces: computed(function() {
return this.get('store').findAll('namespace');
}),
shouldShowNamespaces: computed('namespaces.[]', function() {
const namespaces = this.get('namespaces').toArray();
return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
}),
activeNamespace: computed('namespaces.[]', {
get() {
const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
const namespace = this.get('namespaces').findBy('id', namespaceId);
if (namespace) {
return namespace;
}
// If the namespace is localStorage is no longer in the cluster, it needs to
// be cleared from localStorage
this.set('activeNamespace', null);
return this.get('namespaces').findBy('id', 'default');
},
set(key, value) {
if (value == null) {
window.localStorage.removeItem('nomadActiveNamespace');
} else if (typeof value === 'string') {
window.localStorage.nomadActiveNamespace = value;
return this.get('namespaces').findBy('id', value);
} else {
window.localStorage.nomadActiveNamespace = value.get('name');
return value;
}
},
}),
2017-09-19 14:47:10 +00:00
});