2021-09-15 18:50:11 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { runInDebug } from '@ember/debug';
|
2019-12-17 18:47:37 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
2020-02-21 16:48:54 +00:00
|
|
|
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/nspace';
|
2021-02-23 08:56:42 +00:00
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
2019-12-17 18:47:37 +00:00
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
const findActiveNspace = function(nspaces, nspace) {
|
|
|
|
let found = nspaces.find(function(item) {
|
|
|
|
return item.Name === nspace.Name;
|
|
|
|
});
|
|
|
|
if (typeof found === 'undefined') {
|
|
|
|
runInDebug(_ =>
|
|
|
|
console.info(`${nspace.Name} not found in [${nspaces.map(item => item.Name).join(', ')}]`)
|
|
|
|
);
|
|
|
|
// if we can't find the nspace that was specified try default
|
|
|
|
found = nspaces.find(function(item) {
|
|
|
|
return item.Name === 'default';
|
|
|
|
});
|
|
|
|
// if there is no default just choose the first
|
|
|
|
if (typeof found === 'undefined') {
|
|
|
|
found = nspaces[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
};
|
2019-12-17 18:47:37 +00:00
|
|
|
const modelName = 'nspace';
|
2021-09-15 18:50:11 +00:00
|
|
|
export default class NspaceEnabledService extends RepositoryService {
|
|
|
|
@service('router') router;
|
|
|
|
@service('container') container;
|
|
|
|
@service('env') env;
|
|
|
|
@service('form') form;
|
|
|
|
|
|
|
|
@service('settings') settings;
|
|
|
|
@service('repository/permission') permissions;
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
getPrimaryKey() {
|
2020-02-21 16:48:54 +00:00
|
|
|
return PRIMARY_KEY;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getSlugKey() {
|
2020-02-21 16:48:54 +00:00
|
|
|
return SLUG_KEY;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getModelName() {
|
2019-12-17 18:47:37 +00:00
|
|
|
return modelName;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/namespaces')
|
|
|
|
async findAll() {
|
|
|
|
if (!this.permissions.can('use nspaces')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return super.findAll(...arguments).catch(() => []);
|
|
|
|
}
|
|
|
|
|
|
|
|
@dataSource('/:partition/:ns/:dc/namespace/:id')
|
|
|
|
async findBySlug(params) {
|
|
|
|
let item;
|
|
|
|
if (params.id === '') {
|
|
|
|
item = await this.create({
|
|
|
|
Datacenter: params.dc,
|
|
|
|
Partition: params.partition,
|
|
|
|
ACLs: {
|
|
|
|
PolicyDefaults: [],
|
|
|
|
RoleDefaults: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
item = await super.findBySlug(...arguments);
|
|
|
|
}
|
|
|
|
return this.form
|
|
|
|
.form(this.getModelName())
|
|
|
|
.setData(item)
|
|
|
|
.getData();
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
remove(item) {
|
2020-10-08 15:00:52 +00:00
|
|
|
// Namespace deletion is more of a soft delete.
|
|
|
|
// Therefore the namespace still exists once we've requested a delete/removal.
|
|
|
|
// This makes 'removing' more of a custom action rather than a standard
|
|
|
|
// ember-data delete.
|
|
|
|
// Here we use the same request for a delete but we bypass ember-data's
|
|
|
|
// destroyRecord/unloadRecord and serialization so we don't get
|
|
|
|
// ember data error messages when the UI tries to update a 'DeletedAt' property
|
|
|
|
// on an object that ember-data is trying to delete
|
|
|
|
const res = this.store.adapterFor('nspace').rpc(
|
|
|
|
(adapter, request, serialized, unserialized) => {
|
|
|
|
return adapter.requestForDeleteRecord(request, serialized, unserialized);
|
|
|
|
},
|
|
|
|
(serializer, respond, serialized, unserialized) => {
|
|
|
|
return item;
|
|
|
|
},
|
|
|
|
item,
|
|
|
|
'nspace'
|
|
|
|
);
|
|
|
|
return res;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
authorize(dc, nspace) {
|
|
|
|
if (!this.env.var('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 [];
|
|
|
|
});
|
2021-02-23 08:56:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
async getActive(paramsNspace = '') {
|
|
|
|
if (this.permissions.can('use nspaces')) {
|
|
|
|
return {
|
|
|
|
Name: 'default',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const nspaces = this.store.peekAll('nspace').toArray();
|
|
|
|
if (paramsNspace.length === 0) {
|
|
|
|
const token = await this.settings.findBySlug('token');
|
|
|
|
paramsNspace = token.Namespace || 'default';
|
2019-12-17 18:47:37 +00:00
|
|
|
}
|
2021-09-15 18:50:11 +00:00
|
|
|
// if there is only 1 namespace then use that, otherwise find the
|
|
|
|
// namespace object that corresponds to the active one
|
|
|
|
return nspaces.length === 1 ? nspaces[0] : findActiveNspace(nspaces, { Name: paramsNspace });
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|