2020-06-10 15:07:06 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { get, set } from '@ember/object';
|
2019-12-17 19:27:28 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
2021-02-23 08:56:42 +00:00
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
2019-12-17 19:27:28 +00:00
|
|
|
|
|
|
|
const modelName = 'discovery-chain';
|
2020-06-10 15:07:06 +00:00
|
|
|
const ERROR_MESH_DISABLED = 'Connect must be enabled in order to use this endpoint';
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class DiscoveryChainService extends RepositoryService {
|
|
|
|
@service('repository/dc')
|
|
|
|
dcs;
|
|
|
|
|
|
|
|
getModelName() {
|
2019-12-17 19:27:28 +00:00
|
|
|
return modelName;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/discovery-chain/:id')
|
2021-02-23 08:56:42 +00:00
|
|
|
findBySlug(params, configuration = {}) {
|
2021-10-26 18:26:04 +00:00
|
|
|
// peekAll and find is fine here as datacenter count should be relatively
|
|
|
|
// low, and DCs are the top bucket (when talking dc's partitions, nspaces)
|
|
|
|
const datacenter = this.dcs.peekAll().findBy('Name', params.dc);
|
|
|
|
if (typeof datacenter !== 'undefined' && !get(datacenter, 'MeshEnabled')) {
|
2020-06-10 15:07:06 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
return super.findBySlug(...arguments).catch((e) => {
|
2020-06-10 15:07:06 +00:00
|
|
|
const code = get(e, 'errors.firstObject.status');
|
2021-02-19 16:42:16 +00:00
|
|
|
const body = (get(e, 'errors.firstObject.detail') || '').trim();
|
2020-06-10 15:07:06 +00:00
|
|
|
switch (code) {
|
|
|
|
case '500':
|
2021-10-26 18:26:04 +00:00
|
|
|
if (typeof datacenter !== 'undefined' && body.endsWith(ERROR_MESH_DISABLED)) {
|
2020-06-10 15:07:06 +00:00
|
|
|
set(datacenter, 'MeshEnabled', false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
2020-07-01 09:21:15 +00:00
|
|
|
throw e;
|
2020-06-10 15:07:06 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|