2020-09-30 11:33:01 +00:00
|
|
|
import { set, get } from '@ember/object';
|
2021-09-15 18:50:11 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
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 {
|
2021-09-15 18:50:11 +00:00
|
|
|
@service('env') env;
|
2020-10-26 09:30:07 +00:00
|
|
|
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-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/intentions')
|
|
|
|
async findAll() {
|
|
|
|
return super.findAll(...arguments);
|
2021-02-23 08:56:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/intention/:id')
|
|
|
|
async findBySlug(params) {
|
|
|
|
let item;
|
|
|
|
if (params.id === '') {
|
|
|
|
const defaultNspace = this.env.var('CONSUL_NSPACES_ENABLED') ? '*' : 'default';
|
2021-12-13 15:42:10 +00:00
|
|
|
const defaultPartition = 'default';
|
2021-09-15 18:50:11 +00:00
|
|
|
item = await this.create({
|
|
|
|
SourceNS: params.nspace || defaultNspace,
|
|
|
|
DestinationNS: params.nspace || defaultNspace,
|
2021-09-24 16:31:58 +00:00
|
|
|
SourcePartition: params.partition || defaultPartition,
|
|
|
|
DestinationPartition: params.partition || defaultPartition,
|
2021-09-15 18:50:11 +00:00
|
|
|
Datacenter: params.dc,
|
|
|
|
Partition: params.partition,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
item = super.findBySlug(...arguments);
|
|
|
|
}
|
|
|
|
return item;
|
2021-02-23 08:56:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/intentions/for-service/:id')
|
2021-02-23 08:56:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|