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

130 lines
3.7 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';
2018-11-02 05:07:58 +00:00
import jsonWithDefault from '../utils/json-with-default';
2017-09-19 14:47:10 +00:00
export default Service.extend({
token: service(),
store: service(),
leader: computed('activeRegion', function() {
2019-03-26 07:46:44 +00:00
const token = this.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;
}),
});
}),
defaultRegion: computed(function() {
2019-03-26 07:46:44 +00:00
const token = this.token;
return PromiseObject.create({
promise: token
.authorizedRawRequest(`/${namespace}/agent/members`)
.then(jsonWithDefault({}))
.then(json => {
return { region: json.ServerRegion };
}),
});
}),
regions: computed(function() {
2019-03-26 07:46:44 +00:00
const token = this.token;
return PromiseArray.create({
promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])),
});
}),
activeRegion: computed('regions.[]', {
get() {
2019-03-26 07:46:44 +00:00
const regions = this.regions;
const region = window.localStorage.nomadActiveRegion;
if (regions.includes(region)) {
return region;
}
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;
}
},
}),
shouldShowRegions: computed('regions.[]', function() {
return this.get('regions.length') > 1;
}),
shouldIncludeRegion: computed(
'activeRegion',
'defaultRegion.region',
'shouldShowRegions',
function() {
2019-03-26 07:46:44 +00:00
return this.shouldShowRegions &&
this.activeRegion !== this.get('defaultRegion.region');
}
),
namespaces: computed('activeRegion', function() {
return PromiseArray.create({
2019-03-26 07:46:44 +00:00
promise: this.store
.findAll('namespace')
.then(namespaces => namespaces.compact()),
});
}),
shouldShowNamespaces: computed('namespaces.[]', function() {
2019-03-26 07:46:44 +00:00
const namespaces = this.namespaces.toArray();
return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
}),
activeNamespace: computed('namespaces.[]', {
get() {
const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
2019-03-26 07:46:44 +00:00
const namespace = this.namespaces.findBy('id', namespaceId);
if (namespace) {
return namespace;
}
// If the namespace in localStorage is no longer in the cluster, it needs to
// be cleared from localStorage
2018-10-17 19:55:00 +00:00
window.localStorage.removeItem('nomadActiveNamespace');
2019-03-26 07:46:44 +00:00
return this.namespaces.findBy('id', 'default');
},
set(key, value) {
if (value == null) {
window.localStorage.removeItem('nomadActiveNamespace');
} else if (typeof value === 'string') {
window.localStorage.nomadActiveNamespace = value;
2019-03-26 07:46:44 +00:00
return this.namespaces.findBy('id', value);
} else {
window.localStorage.nomadActiveNamespace = value.get('name');
return value;
}
},
}),
reset() {
this.set('activeNamespace', null);
},
2017-09-19 14:47:10 +00:00
});