Files
threshold-ui/addons/api/mirage/factories/target.js
T

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-08-21 11:53:53 -04:00
import factory from '../generated/factories/target';
import { trait } from 'ember-cli-mirage';
import { random, datatype } from 'faker';
2021-08-16 10:18:46 -07:00
import permissions from '../helpers/permissions';
2020-08-21 11:53:53 -04:00
2020-08-25 15:45:51 -04:00
const randomBoolean = (chance = 0.5) => Math.random() < chance;
const hostSetChance = 0.3;
export default factory.extend({
2021-08-16 10:18:46 -07:00
authorized_actions: () =>
permissions.authorizedActionsFor('target') || [
'no-op',
'read',
'update',
'delete',
2021-09-16 14:49:48 -04:00
'authorize-session',
'add-host-sources',
'remove-host-sources',
'add-credential-sources',
'remove-credential-sources',
2021-08-16 10:18:46 -07:00
],
2020-10-02 14:50:35 -04:00
/**
* -1 means "unlimited" and we want to generate these on occasion.
2020-10-02 14:50:35 -04:00
*/
session_connection_limit: () => random.arrayElement([-1, datatype.number()]),
2020-10-02 14:50:35 -04:00
/**
* Generates attributes fields by type.
*/
afterCreate(target) {
if (target.type === 'tcp') {
target.update({
attributes: {
default_port: datatype.number(),
2021-03-04 15:47:38 -05:00
},
});
}
},
2020-08-25 15:45:51 -04:00
/**
* Randomly selects existing host sets and credential libraries to assign to target.
2020-08-25 15:45:51 -04:00
*/
withAssociations: trait({
afterCreate(target, server) {
let randomlySelectedHostSets, randomlySelectedCredentialLibraries;
2020-08-25 15:45:51 -04:00
randomlySelectedHostSets = server.schema.hostSets
2020-09-10 16:57:11 -04:00
// BLERG: fun fact, for no reason at all, the element passed
// into a where function is not a full model instance, as you might
// expect at this level of abstraction, but appears to be a serialized
// representation of the model instance. It's very confusing since
// the result set of `where` _is a collection of model instances_.
2021-03-04 15:47:38 -05:00
.where((hostSet) => hostSet.scopeId === target.scope.id)
.models.filter(() => randomBoolean(hostSetChance));
randomlySelectedCredentialLibraries = server.schema.credentialLibraries
.where(
(credentialLibrary) => credentialLibrary.scopeId === target.scope.id
)
.models.filter(() => randomBoolean());
target.update({
hostSets: randomlySelectedHostSets,
credentialLibraries: randomlySelectedCredentialLibraries,
});
2021-03-04 15:47:38 -05:00
},
}),
});