2020-09-30 11:33:01 +00:00
|
|
|
import { set, get } from '@ember/object';
|
2018-10-26 16:36:15 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
|
|
|
import { PRIMARY_KEY } from 'consul-ui/models/intention';
|
2021-02-23 08:56:42 +00:00
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
2020-10-26 09:30:07 +00:00
|
|
|
|
2018-10-26 16:36:15 +00:00
|
|
|
const modelName = 'intention';
|
2020-10-26 09:30:07 +00:00
|
|
|
export default class IntentionRepository extends RepositoryService {
|
|
|
|
managedByCRDs = false;
|
|
|
|
|
|
|
|
getModelName() {
|
2018-10-26 16:36:15 +00:00
|
|
|
return modelName;
|
2020-10-26 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getPrimaryKey() {
|
2018-10-26 16:36:15 +00:00
|
|
|
return PRIMARY_KEY;
|
2020-10-26 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
create(obj) {
|
2020-07-09 09:08:47 +00:00
|
|
|
delete obj.Namespace;
|
2020-10-26 09:30:07 +00:00
|
|
|
return super.create({
|
2020-09-18 10:14:06 +00:00
|
|
|
Action: 'allow',
|
|
|
|
...obj,
|
|
|
|
});
|
2020-10-26 09:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isManagedByCRDs() {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (!this.managedByCRDs) {
|
|
|
|
this.managedByCRDs = this.store
|
|
|
|
.peekAll(this.getModelName())
|
|
|
|
.toArray()
|
|
|
|
.some(item => item.IsManagedByCRD);
|
2020-10-26 09:30:07 +00:00
|
|
|
}
|
|
|
|
return this.managedByCRDs;
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:42:16 +00:00
|
|
|
// legacy intentions are strange that in order to read/write you need access
|
|
|
|
// to either/or the destination or source
|
2021-02-23 08:56:42 +00:00
|
|
|
async authorizeBySlug(cb, access, params) {
|
|
|
|
const [, source, , destination] = params.id.split(':');
|
2021-02-19 16:42:16 +00:00
|
|
|
const ability = this.permissions.abilityFor(this.getModelName());
|
2021-02-23 08:56:42 +00:00
|
|
|
params.resources = ability
|
2021-02-19 16:42:16 +00:00
|
|
|
.generateForSegment(source)
|
|
|
|
.concat(ability.generateForSegment(destination));
|
2021-02-23 08:56:42 +00:00
|
|
|
return this.authorizeByPermissions(cb, access, params);
|
2021-02-19 16:42:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:30:07 +00:00
|
|
|
async persist(obj) {
|
|
|
|
const res = await super.persist(...arguments);
|
|
|
|
// if Action is set it means we are an l4 type intention
|
|
|
|
// we don't delete these at a UI level incase the user
|
|
|
|
// would like to switch backwards and forwards between
|
|
|
|
// allow/deny/l7 in the forms, but once its been saved
|
|
|
|
// to the backend we then delete them
|
|
|
|
if (get(res, 'Action.length')) {
|
|
|
|
set(res, 'Permissions', []);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:56:42 +00:00
|
|
|
@dataSource('/:ns/:dc/intentions')
|
|
|
|
async findAllByDatacenter() {
|
|
|
|
return super.findAllByDatacenter(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
@dataSource('/:ns/:dc/intention/:id')
|
|
|
|
async findBySlug() {
|
|
|
|
return super.findBySlug(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
@dataSource('/:ns/:dc/intentions/for-service/:id')
|
|
|
|
async findByService(params, configuration = {}) {
|
2020-04-16 14:15:45 +00:00
|
|
|
const query = {
|
2021-02-23 08:56:42 +00:00
|
|
|
dc: params.dc,
|
|
|
|
nspace: params.nspace,
|
|
|
|
filter: `SourceName == "${params.id}" or DestinationName == "${params.id}" or SourceName == "*" or DestinationName == "*"`,
|
2020-04-16 14:15:45 +00:00
|
|
|
};
|
|
|
|
if (typeof configuration.cursor !== 'undefined') {
|
|
|
|
query.index = configuration.cursor;
|
2020-07-17 13:42:45 +00:00
|
|
|
query.uri = configuration.uri;
|
2020-04-16 14:15:45 +00:00
|
|
|
}
|
2020-10-26 09:30:07 +00:00
|
|
|
return this.store.query(this.getModelName(), query);
|
|
|
|
}
|
|
|
|
}
|