diff --git a/ui/core/mirage/factories/target.js b/ui/core/mirage/factories/target.js index 9746158f2..7c74806a6 100644 --- a/ui/core/mirage/factories/target.js +++ b/ui/core/mirage/factories/target.js @@ -1,13 +1,21 @@ import factory from '../generated/factories/target'; import { trait } from 'ember-cli-mirage'; +const randomBoolean = (chance = 0.5) => Math.random() < chance; +const hostSetChance = 0.3; + export default factory.extend({ - withChildren: trait({ + /** + * Randomly selects existing host sets to assign to target. + */ + withRandomHostSets: trait({ afterCreate(target, server) { - const scope = target.scope; - const hosts = server.createList('host', 10, { scope }); - server.createList('host-set', 3, { scope, target , hosts}); + let randomlySelectedHostSets; + randomlySelectedHostSets = server.schema.hostSets + .where(item => item.scope.id === target.scope.id).models + .filter(() => randomBoolean(hostSetChance)); + target.update({hostSets: randomlySelectedHostSets}); } }) diff --git a/ui/core/mirage/models/target.js b/ui/core/mirage/models/target.js new file mode 100644 index 000000000..d33295663 --- /dev/null +++ b/ui/core/mirage/models/target.js @@ -0,0 +1,7 @@ +import { Model, hasMany } from 'ember-cli-mirage'; + +export default Model.extend({ + + hostSets: hasMany() + +}); diff --git a/ui/core/mirage/scenarios/default.js b/ui/core/mirage/scenarios/default.js index 5c835dadf..2b212f999 100644 --- a/ui/core/mirage/scenarios/default.js +++ b/ui/core/mirage/scenarios/default.js @@ -33,7 +33,7 @@ export default function(server) { server.db.scopes.where({type: 'project'}).forEach(({ id, type }) => { const scope = { id, type }; server.createList('host-catalog', 2, { scope }, 'withChildren'); - server.createList('target', 2, { scope }, 'withChildren'); + server.createList('target', 2, { scope }, 'withRandomHostSets'); }); } diff --git a/ui/core/mirage/serializers/target.js b/ui/core/mirage/serializers/target.js index 3d4bc5f3e..c9c6935a0 100644 --- a/ui/core/mirage/serializers/target.js +++ b/ui/core/mirage/serializers/target.js @@ -1,5 +1,24 @@ import ApplicationSerializer from './application'; +import { underscore } from '@ember/string'; export default ApplicationSerializer.extend({ - modelName: 'target' + modelName: 'target', + + keyForRelationshipIds(relationshipName) { + return `${this._container.inflector.singularize( + underscore(relationshipName) + )}_ids`; + }, + + _hashForModel(model) { + const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); + const { hostSets } = this.schema; + json.host_sets = model.hostSetIds.map(host_set_id => { + const hostSet = hostSets.find(host_set_id); + const host_catalog_id = hostSet?.hostCatalog?.id; + return { host_set_id, host_catalog_id }; + }); + return json; + } + });