mirror of
https://github.com/status-im/consul.git
synced 2025-01-19 10:15:06 +00:00
8b12d0d09d
We use a `<DataSource @src={{url}} />` component throughout our UI for when we want to load data from within our components. The URL specified as the `@src` is used to map/lookup what is used in to retrieve data, for example we mostly use our repository methods wrapped with our Promise backed `EventSource` implementation, but DataSource URLs can also be mapped to EventTarget backed `EventSource`s and native `EventSource`s or `WebSockets` if we ever need to use those (for example these are options for potential streaming support with the Consul backend). The URL to function/method mapping previous to this PR used a very naive humongous `switch` statement which was a temporary 'this is fine for the moment' solution, although we'd always wanted to replace with something more manageable. Here we add `wayfarer` as a dependency - a very small (1kb), very fast, radix trie based router, and use that to perform the URL to function/method mapping. This essentially turns every `DataSource` into a very small SPA - change its URL and the view of data changes. When the data itself changes, either the yielded view of data changes or the `onchange` event is fired with the changed data, making the externally sourced view of data completely reactive. ```javascript // use the new decorator a service somewhere to annotate/decorate // a method with the URL that can be used to access this method @dataSource('/:ns/:dc/services') async findAllByDatacenter(params) { // get the data } // can use with JS in a route somewhere async model() { return this.data.source(uri => uri`/${nspace}/${dc}/services`) } ``` ```hbs {{!-- or just straight in a template using the component --}} <DataSource @src="/default/dc1/services" @onchange="" /> ``` This also uses a new `container` Service to automatically execute/import certain services yet not execute them. This new service also provides a lookup that supports both standard ember DI lookup plus Class based lookup or these specific services. Lastly we also provide another debug function called DataSourceRoutes() which can be called from console which gives you a list of URLs and their mappings.
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
import { set, get } from '@ember/object';
|
|
import RepositoryService from 'consul-ui/services/repository';
|
|
import { PRIMARY_KEY } from 'consul-ui/models/intention';
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
|
|
|
const modelName = 'intention';
|
|
export default class IntentionRepository extends RepositoryService {
|
|
managedByCRDs = false;
|
|
|
|
getModelName() {
|
|
return modelName;
|
|
}
|
|
|
|
getPrimaryKey() {
|
|
return PRIMARY_KEY;
|
|
}
|
|
|
|
create(obj) {
|
|
delete obj.Namespace;
|
|
return super.create({
|
|
Action: 'allow',
|
|
...obj,
|
|
});
|
|
}
|
|
|
|
isManagedByCRDs() {
|
|
if (!this.managedByCRDs) {
|
|
this.managedByCRDs = this.store
|
|
.peekAll(this.getModelName())
|
|
.toArray()
|
|
.some(item => item.IsManagedByCRD);
|
|
}
|
|
return this.managedByCRDs;
|
|
}
|
|
|
|
// legacy intentions are strange that in order to read/write you need access
|
|
// to either/or the destination or source
|
|
async authorizeBySlug(cb, access, params) {
|
|
const [, source, , destination] = params.id.split(':');
|
|
const ability = this.permissions.abilityFor(this.getModelName());
|
|
params.resources = ability
|
|
.generateForSegment(source)
|
|
.concat(ability.generateForSegment(destination));
|
|
return this.authorizeByPermissions(cb, access, params);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
@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 = {}) {
|
|
const query = {
|
|
dc: params.dc,
|
|
nspace: params.nspace,
|
|
filter: `SourceName == "${params.id}" or DestinationName == "${params.id}" or SourceName == "*" or DestinationName == "*"`,
|
|
};
|
|
if (typeof configuration.cursor !== 'undefined') {
|
|
query.index = configuration.cursor;
|
|
query.uri = configuration.uri;
|
|
}
|
|
return this.store.query(this.getModelName(), query);
|
|
}
|
|
}
|