open-nomad/ui/app/services/system.js
Buck Doyle 8b5b2116ec
Fix job detail crash when recommendations off (#9269)
Without this, visiting any job detail page on Nomad OSS would crash with
an error like this:

Error: Ember Data Request GET
/v1/recommendations?job=ping%F0%9F%A5%B3&namespace=default returned a
404 Payload (text/xml)

The problem was twofold.

1. The recommendation ability didn’t include anything about checking
whether the feature was present. This adds a request to
/v1/operator/license on application load to determine which features are
present and store them in the system service. The ability now looks for
'Dynamic Application Sizing' in that feature list.

2. Second, I didn’t check permissions at all in the job-fetching or job
detail templates.
2020-11-06 08:21:38 -06:00

170 lines
4.6 KiB
JavaScript

import Service, { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import PromiseObject from '../utils/classes/promise-object';
import PromiseArray from '../utils/classes/promise-array';
import { namespace } from '../adapters/application';
import jsonWithDefault from '../utils/json-with-default';
import classic from 'ember-classic-decorator';
import { task } from 'ember-concurrency';
@classic
export default class SystemService extends Service {
@service token;
@service store;
@computed('activeRegion')
get leader() {
const token = this.token;
return PromiseObject.create({
promise: token
.authorizedRequest(`/${namespace}/status/leader`)
.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;
}),
});
}
@computed
get agent() {
const token = this.token;
return PromiseObject.create({
promise: token
.authorizedRawRequest(`/${namespace}/agent/self`)
.then(jsonWithDefault({}))
.then(agent => {
agent.version = agent.member?.Tags?.build || 'Unknown';
return agent;
}),
});
}
@computed
get defaultRegion() {
const token = this.token;
return PromiseObject.create({
promise: token
.authorizedRawRequest(`/${namespace}/agent/members`)
.then(jsonWithDefault({}))
.then(json => {
return { region: json.ServerRegion };
}),
});
}
@computed
get regions() {
const token = this.token;
return PromiseArray.create({
promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])),
});
}
@computed('regions.[]')
get activeRegion() {
const regions = this.regions;
const region = window.localStorage.nomadActiveRegion;
if (regions.includes(region)) {
return region;
}
return null;
}
set activeRegion(value) {
if (value == null) {
window.localStorage.removeItem('nomadActiveRegion');
return;
} 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;
}
}
@computed('regions.[]')
get shouldShowRegions() {
return this.get('regions.length') > 1;
}
@computed('activeRegion', 'defaultRegion.region', 'shouldShowRegions')
get shouldIncludeRegion() {
return this.shouldShowRegions && this.activeRegion !== this.get('defaultRegion.region');
}
@computed('activeRegion')
get namespaces() {
return PromiseArray.create({
promise: this.store.findAll('namespace').then(namespaces => namespaces.compact()),
});
}
@computed('namespaces.[]')
get shouldShowNamespaces() {
const namespaces = this.namespaces.toArray();
return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
}
@computed('namespaces.[]')
get activeNamespace() {
const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
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
window.localStorage.removeItem('nomadActiveNamespace');
return this.namespaces.findBy('id', 'default');
}
set activeNamespace(value) {
if (value == null) {
window.localStorage.removeItem('nomadActiveNamespace');
return;
} else if (typeof value === 'string') {
window.localStorage.nomadActiveNamespace = value;
return this.namespaces.findBy('id', value);
} else {
window.localStorage.nomadActiveNamespace = value.get('name');
return value;
}
}
reset() {
this.set('activeNamespace', null);
this.notifyPropertyChange('namespaces');
}
@task(function*() {
const emptyLicense = { License: { Features: [] } };
try {
return yield this.token
.authorizedRawRequest(`/${namespace}/operator/license`)
.then(jsonWithDefault(emptyLicense));
} catch (e) {
return emptyLicense;
}
})
fetchLicense;
@alias('fetchLicense.lastSuccessful.value') license;
@computed('license.License.Features.[]')
get features() {
return this.get('license.License.Features') || [];
}
}