2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2021-01-21 16:43:11 -05:00
|
|
|
import factory from '../generated/factories/session';
|
2023-01-09 08:48:49 -08:00
|
|
|
import { trait } from 'miragejs';
|
2022-08-01 11:25:07 -07:00
|
|
|
import { faker } from '@faker-js/faker';
|
2022-06-01 14:02:18 -04:00
|
|
|
import generateId from '../helpers/id';
|
2020-09-18 12:54:31 -04:00
|
|
|
|
2021-03-04 15:47:38 -05:00
|
|
|
const statusStrings = ['pending', 'active', 'canceling', 'terminated'];
|
2020-09-19 09:36:48 -04:00
|
|
|
|
2022-08-01 11:25:07 -07:00
|
|
|
export const pickRandomStatusString = () =>
|
|
|
|
|
faker.helpers.arrayElement(statusStrings);
|
2020-09-19 09:36:48 -04:00
|
|
|
|
2020-09-18 12:54:31 -04:00
|
|
|
export default factory.extend({
|
2022-06-01 14:02:18 -04:00
|
|
|
id: () => generateId('ss_'),
|
|
|
|
|
|
2020-09-19 09:36:48 -04:00
|
|
|
status: pickRandomStatusString,
|
2020-09-18 12:54:31 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sessions generated with this trait will automatically form
|
|
|
|
|
* associations to user, target, host set, and host (if available) within
|
|
|
|
|
* the current scope.
|
|
|
|
|
*/
|
|
|
|
|
withAssociations: trait({
|
|
|
|
|
afterCreate(record, server) {
|
|
|
|
|
const scopeId = record.scopeId;
|
|
|
|
|
if (record.scope.type === 'project') {
|
|
|
|
|
const orgScopeId = record.scope?.scope.id;
|
2021-03-04 15:47:38 -05:00
|
|
|
const user = server.schema.users.where({ scopeId: orgScopeId })
|
|
|
|
|
.models[0];
|
2020-09-18 12:54:31 -04:00
|
|
|
if (user) record.update({ user });
|
|
|
|
|
}
|
|
|
|
|
const target = server.schema.targets.where({ scopeId }).models[0];
|
|
|
|
|
if (target) {
|
|
|
|
|
record.update({ target });
|
|
|
|
|
const hostSet = target.hostSets.models[0];
|
|
|
|
|
if (hostSet) {
|
|
|
|
|
record.update({ hostSet });
|
|
|
|
|
const host = hostSet.hosts.models[0];
|
|
|
|
|
if (host) record.update({ host });
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-04 15:47:38 -05:00
|
|
|
},
|
|
|
|
|
}),
|
2020-09-18 12:54:31 -04:00
|
|
|
});
|