2020-08-26 14:24:30 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
2020-12-07 09:14:30 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-12-14 15:28:35 +00:00
|
|
|
import { set } from '@ember/object';
|
2021-02-19 16:42:16 +00:00
|
|
|
import { ACCESS_READ } from 'consul-ui/abilities/base';
|
2021-02-23 08:56:42 +00:00
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
2020-12-07 09:14:30 +00:00
|
|
|
|
2020-08-26 14:24:30 +00:00
|
|
|
const modelName = 'service-instance';
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class ServiceInstanceService extends RepositoryService {
|
2020-12-07 09:14:30 +00:00
|
|
|
@service('repository/proxy') proxyRepo;
|
2020-11-09 09:25:35 +00:00
|
|
|
getModelName() {
|
2020-08-26 14:24:30 +00:00
|
|
|
return modelName;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:38:04 +00:00
|
|
|
shouldReconcile(item, params) {
|
|
|
|
return super.shouldReconcile(...arguments) && item.Service.Service === params.id;
|
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/service-instances/for-service/:id')
|
2021-02-23 08:56:42 +00:00
|
|
|
async findByService(params, configuration = {}) {
|
2020-08-26 14:24:30 +00:00
|
|
|
if (typeof configuration.cursor !== 'undefined') {
|
2021-02-23 08:56:42 +00:00
|
|
|
params.index = configuration.cursor;
|
|
|
|
params.uri = configuration.uri;
|
2020-08-26 14:24:30 +00:00
|
|
|
}
|
2021-10-07 11:38:04 +00:00
|
|
|
const instances = await this.authorizeBySlug(
|
|
|
|
async () => this.query(params),
|
2021-02-19 16:42:16 +00:00
|
|
|
ACCESS_READ,
|
2021-02-23 08:56:42 +00:00
|
|
|
params
|
2021-02-19 16:42:16 +00:00
|
|
|
);
|
2021-10-07 11:38:04 +00:00
|
|
|
return instances;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/service-instance/:serviceId/:node/:id')
|
2021-02-23 08:56:42 +00:00
|
|
|
async findBySlug(params, configuration = {}) {
|
2020-08-26 14:24:30 +00:00
|
|
|
if (typeof configuration.cursor !== 'undefined') {
|
2021-02-23 08:56:42 +00:00
|
|
|
params.index = configuration.cursor;
|
|
|
|
params.uri = configuration.uri;
|
2020-08-26 14:24:30 +00:00
|
|
|
}
|
2021-02-19 16:42:16 +00:00
|
|
|
return this.authorizeBySlug(
|
2021-02-23 08:56:42 +00:00
|
|
|
async () => this.store.queryRecord(this.getModelName(), params),
|
2021-02-19 16:42:16 +00:00
|
|
|
ACCESS_READ,
|
2021-02-23 08:56:42 +00:00
|
|
|
params
|
2021-02-19 16:42:16 +00:00
|
|
|
);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
2020-12-07 09:14:30 +00:00
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/proxy-service-instance/:serviceId/:node/:id')
|
2021-02-23 08:56:42 +00:00
|
|
|
async findProxyBySlug(params, configuration = {}) {
|
2020-12-07 09:14:30 +00:00
|
|
|
const instance = await this.findBySlug(...arguments);
|
|
|
|
let proxy = this.store.peekRecord('proxy', instance.uid);
|
2021-01-12 14:53:21 +00:00
|
|
|
// Currently, we call the proxy endpoint before this endpoint
|
|
|
|
// therefore proxy is never undefined. If we ever call this endpoint
|
|
|
|
// first we'll need to do something like the following
|
2020-12-07 09:14:30 +00:00
|
|
|
// if(typeof proxy === 'undefined') {
|
|
|
|
// await proxyRepo.create({})
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Copy over all the things to the ProxyServiceInstance
|
2021-01-29 15:51:23 +00:00
|
|
|
['Service', 'Node', 'meta'].forEach(prop => {
|
2020-12-07 09:14:30 +00:00
|
|
|
set(proxy, prop, instance[prop]);
|
|
|
|
});
|
|
|
|
['Checks'].forEach(prop => {
|
2021-01-29 15:51:23 +00:00
|
|
|
// completely wipe out any previous values so we don't accumulate things
|
|
|
|
// eternally
|
|
|
|
proxy.set(prop, []);
|
2020-12-07 09:14:30 +00:00
|
|
|
instance[prop].forEach(item => {
|
|
|
|
if (typeof item !== 'undefined') {
|
|
|
|
proxy[prop].addFragment(item.copy());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// delete the ServiceInstance record as we now have a ProxyServiceInstance
|
|
|
|
instance.unloadRecord();
|
|
|
|
return proxy;
|
|
|
|
}
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|