2017-12-15 21:39:18 +00:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
2018-08-09 18:03:37 +00:00
|
|
|
import { copy } from '@ember/object/internals';
|
2017-09-19 14:47:10 +00:00
|
|
|
import PromiseObject from '../utils/classes/promise-object';
|
2018-08-02 22:56:11 +00:00
|
|
|
import PromiseArray from '../utils/classes/promise-array';
|
2017-09-19 14:47:10 +00:00
|
|
|
import { namespace } from '../adapters/application';
|
|
|
|
|
2018-08-09 18:03:37 +00:00
|
|
|
// When the request isn't ok (e.g., forbidden) handle gracefully
|
|
|
|
const jsonWithDefault = defaultResponse => res =>
|
|
|
|
res.ok ? res.json() : copy(defaultResponse, true);
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
export default Service.extend({
|
2017-12-15 21:39:18 +00:00
|
|
|
token: service(),
|
|
|
|
store: service(),
|
2017-10-07 00:14:08 +00:00
|
|
|
|
2018-08-04 01:17:12 +00:00
|
|
|
leader: computed('activeRegion', function() {
|
2017-10-07 00:14:08 +00:00
|
|
|
const token = this.get('token');
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
return PromiseObject.create({
|
2017-10-07 00:14:08 +00:00
|
|
|
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;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}),
|
2017-10-10 01:50:49 +00:00
|
|
|
|
2018-08-09 02:34:56 +00:00
|
|
|
defaultRegion: computed(function() {
|
|
|
|
const token = this.get('token');
|
|
|
|
return PromiseObject.create({
|
|
|
|
promise: token
|
|
|
|
.authorizedRawRequest(`/${namespace}/agent/members`)
|
2018-08-09 18:03:37 +00:00
|
|
|
.then(jsonWithDefault({}))
|
2018-08-09 02:34:56 +00:00
|
|
|
.then(json => {
|
|
|
|
return { region: json.ServerRegion };
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2018-08-02 22:56:11 +00:00
|
|
|
regions: computed(function() {
|
|
|
|
const token = this.get('token');
|
|
|
|
|
|
|
|
return PromiseArray.create({
|
2018-08-09 18:03:37 +00:00
|
|
|
promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])),
|
2018-08-02 22:56:11 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
|
|
|
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
|
2018-08-07 01:33:57 +00:00
|
|
|
// this.set('activeRegion', null);
|
2018-08-02 22:56:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
2018-08-04 01:17:12 +00:00
|
|
|
shouldShowRegions: computed('regions.[]', function() {
|
|
|
|
return this.get('regions.length') > 1;
|
|
|
|
}),
|
|
|
|
|
2018-08-09 02:34:56 +00:00
|
|
|
shouldIncludeRegion: computed(
|
|
|
|
'activeRegion',
|
|
|
|
'defaultRegion.region',
|
|
|
|
'shouldShowRegions',
|
|
|
|
function() {
|
|
|
|
return (
|
2018-08-09 02:39:32 +00:00
|
|
|
this.get('shouldShowRegions') &&
|
|
|
|
this.get('activeRegion') !== this.get('defaultRegion.region')
|
2018-08-09 02:34:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
),
|
|
|
|
|
2018-08-04 01:17:12 +00:00
|
|
|
namespaces: computed('activeRegion', function() {
|
|
|
|
return PromiseArray.create({
|
|
|
|
promise: this.get('store')
|
|
|
|
.findAll('namespace')
|
|
|
|
.then(namespaces => namespaces.compact()),
|
|
|
|
});
|
2017-10-10 01:50:49 +00:00
|
|
|
}),
|
|
|
|
|
2017-10-23 17:26:48 +00:00
|
|
|
shouldShowNamespaces: computed('namespaces.[]', function() {
|
|
|
|
const namespaces = this.get('namespaces').toArray();
|
|
|
|
return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
|
|
|
|
}),
|
|
|
|
|
2017-10-10 01:50:49 +00:00
|
|
|
activeNamespace: computed('namespaces.[]', {
|
|
|
|
get() {
|
|
|
|
const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
|
2018-03-28 21:55:07 +00:00
|
|
|
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');
|
2017-10-10 01:50:49 +00:00
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
if (value == null) {
|
|
|
|
window.localStorage.removeItem('nomadActiveNamespace');
|
2017-10-10 17:54:23 +00:00
|
|
|
} else if (typeof value === 'string') {
|
2017-10-10 01:50:49 +00:00
|
|
|
window.localStorage.nomadActiveNamespace = value;
|
2017-10-10 17:54:23 +00:00
|
|
|
return this.get('namespaces').findBy('id', value);
|
2018-03-28 21:55:07 +00:00
|
|
|
} else {
|
|
|
|
window.localStorage.nomadActiveNamespace = value.get('name');
|
|
|
|
return value;
|
2017-10-10 01:50:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
2018-08-04 01:17:12 +00:00
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('activeNamespace', null);
|
|
|
|
},
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|