open-consul/ui/packages/consul-ui/app/services/repository/nspace/enabled.js

91 lines
2.5 KiB
JavaScript
Raw Normal View History

import { inject as service } from '@ember/service';
import { get } from '@ember/object';
import { env } from 'consul-ui/env';
import RepositoryService from 'consul-ui/services/repository';
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/nspace';
const modelName = 'nspace';
export default class EnabledService extends RepositoryService {
@service('router')
router;
@service('settings')
settings;
getPrimaryKey() {
return PRIMARY_KEY;
}
getSlugKey() {
return SLUG_KEY;
}
getModelName() {
return modelName;
}
ui: DataSource Decorator (#9746) 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.
2021-02-23 08:56:42 +00:00
findAll(params, configuration = {}) {
const query = {};
if (typeof configuration.cursor !== 'undefined') {
query.index = configuration.cursor;
query.uri = configuration.uri;
}
return this.store.query(this.getModelName(), query);
}
authorize(dc, nspace) {
if (!env('CONSUL_ACLS_ENABLED')) {
return Promise.resolve([
{
Resource: 'operator',
Access: 'write',
Allow: true,
},
]);
}
return this.store.authorize(this.getModelName(), { dc: dc, ns: nspace }).catch(function(e) {
return [];
});
}
getActive() {
let routeParams = {};
// this is only populated before the model hook as fired,
// it is then deleted after the model hook has finished
const infos = get(this, 'router._router.currentState.router.activeTransition.routeInfos');
if (typeof infos !== 'undefined') {
infos.forEach(function(item) {
Object.keys(item.params).forEach(function(prop) {
routeParams[prop] = item.params[prop];
});
});
} else {
// this is only populated after the model hook has finished
//
const current = get(this, 'router.currentRoute');
if (current) {
const nspacedRoute = current.find(function(item, i, arr) {
return item.paramNames.includes('nspace');
});
if (typeof nspacedRoute !== 'undefined') {
routeParams.nspace = nspacedRoute.params.nspace;
}
}
}
return this.settings
.findBySlug('nspace')
.then(function(nspace) {
// If we can't figure out the nspace from the URL use
// the previously saved nspace and if thats not there
// then just use default
return routeParams.nspace || nspace || '~default';
})
.then(nspace => this.settings.persist({ nspace: nspace }))
.then(function(item) {
return {
Name: item.nspace.substr(1),
};
});
}
}