2020-10-01 08:33:22 +00:00
|
|
|
import Route from 'consul-ui/routing/route';
|
2021-09-15 18:50:11 +00:00
|
|
|
import { action } from '@ember/object';
|
2022-03-09 08:29:27 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2022-09-08 10:43:39 +00:00
|
|
|
import { runInDebug } from '@ember/debug';
|
2018-12-04 17:01:59 +00:00
|
|
|
|
|
|
|
import WithBlockingActions from 'consul-ui/mixins/with-blocking-actions';
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
export default class ApplicationRoute extends Route.extend(WithBlockingActions) {
|
2022-03-09 08:29:27 +00:00
|
|
|
@service('client/http') client;
|
2022-09-08 10:43:39 +00:00
|
|
|
@service('env') env;
|
|
|
|
@service('repository/token') tokenRepo;
|
|
|
|
@service('settings') settings;
|
2022-03-09 08:29:27 +00:00
|
|
|
|
|
|
|
data;
|
|
|
|
|
2022-09-08 10:43:39 +00:00
|
|
|
async model() {
|
2022-09-15 08:43:17 +00:00
|
|
|
if (this.env.var('CONSUL_ACLS_ENABLED')) {
|
2022-09-08 10:43:39 +00:00
|
|
|
const secret = this.env.var('CONSUL_HTTP_TOKEN');
|
|
|
|
const existing = await this.settings.findBySlug('token');
|
2022-09-15 08:43:17 +00:00
|
|
|
if (!existing.AccessorID && secret) {
|
2022-09-08 10:43:39 +00:00
|
|
|
try {
|
|
|
|
const token = await this.tokenRepo.self({
|
|
|
|
secret: secret,
|
2022-09-15 08:43:17 +00:00
|
|
|
dc: this.env.var('CONSUL_DATACENTER_LOCAL'),
|
2022-09-08 10:43:39 +00:00
|
|
|
});
|
|
|
|
await this.settings.persist({
|
|
|
|
token: {
|
|
|
|
AccessorID: token.AccessorID,
|
|
|
|
SecretID: token.SecretID,
|
|
|
|
Namespace: token.Namespace,
|
|
|
|
Partition: token.Partition,
|
2022-09-15 08:43:17 +00:00
|
|
|
},
|
2022-09-08 10:43:39 +00:00
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
runInDebug((_) => console.error(e));
|
2022-09-08 10:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-03-09 08:29:27 +00:00
|
|
|
@action
|
|
|
|
onClientChanged(e) {
|
|
|
|
let data = e.data;
|
2022-09-15 08:43:17 +00:00
|
|
|
if (data === '') {
|
2022-03-09 08:29:27 +00:00
|
|
|
data = { blocking: true };
|
|
|
|
}
|
|
|
|
// this.data is always undefined first time round and its the 'first read'
|
|
|
|
// of the value so we don't need to abort anything
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof this.data === 'undefined') {
|
2022-03-09 08:29:27 +00:00
|
|
|
this.data = Object.assign({}, data);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
if (this.data.blocking === true && data.blocking === false) {
|
2022-03-09 08:29:27 +00:00
|
|
|
this.client.abort();
|
|
|
|
}
|
|
|
|
this.data = Object.assign({}, data);
|
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@action
|
|
|
|
error(e, transition) {
|
|
|
|
// TODO: Normalize all this better
|
|
|
|
let error = {
|
|
|
|
status: e.code || e.statusCode || '',
|
|
|
|
message: e.message || e.detail || 'Error',
|
|
|
|
};
|
|
|
|
if (e.errors && e.errors[0]) {
|
|
|
|
error = e.errors[0];
|
|
|
|
error.message = error.message || error.title || error.detail || 'Error';
|
|
|
|
}
|
|
|
|
if (error.status === '') {
|
|
|
|
error.message = 'Error';
|
|
|
|
}
|
|
|
|
this.controllerFor('application').setProperties({ error: error });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|