2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-04 16:53:37 -04:00
|
|
|
import factory from '../generated/factories/host-catalog';
|
2023-01-09 08:48:49 -08:00
|
|
|
import { trait } from 'miragejs';
|
2021-08-19 16:06:47 -07:00
|
|
|
import permissions from '../helpers/permissions';
|
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';
|
2022-01-26 13:03:21 -05:00
|
|
|
|
|
|
|
|
const types = ['static', 'plugin'];
|
|
|
|
|
// Represents known plugin types, except "foobar" which models the possibility
|
|
|
|
|
// of receiving an _unknown_ type, which the UI must handle gracefully.
|
|
|
|
|
const pluginTypes = ['aws', 'azure', 'foobar'];
|
|
|
|
|
|
|
|
|
|
let pluginTypeCounter = 1;
|
2020-06-04 16:53:37 -04:00
|
|
|
|
2020-08-19 16:12:53 -04:00
|
|
|
export default factory.extend({
|
2022-06-01 14:02:18 -04:00
|
|
|
id: () => generateId('hc_'),
|
2022-02-02 17:04:09 -05:00
|
|
|
|
|
|
|
|
// Cycle through available types
|
|
|
|
|
type: (i) => types[i % types.length],
|
|
|
|
|
|
2021-08-19 16:06:47 -07:00
|
|
|
authorized_actions: () =>
|
|
|
|
|
permissions.authorizedActionsFor('host-catalog') || [
|
|
|
|
|
'no-op',
|
|
|
|
|
'read',
|
|
|
|
|
'update',
|
|
|
|
|
'delete',
|
|
|
|
|
],
|
2022-02-02 17:04:09 -05:00
|
|
|
authorized_collection_actions: function () {
|
|
|
|
|
const isStatic = this.type === 'static';
|
2021-09-02 15:28:40 -07:00
|
|
|
return {
|
2022-02-02 17:04:09 -05:00
|
|
|
// only static catalogs allow host create at this time
|
|
|
|
|
hosts: isStatic ? ['create', 'list'] : ['list'],
|
2021-09-02 13:01:58 -07:00
|
|
|
'host-sets': ['create', 'list'],
|
2021-09-02 15:28:40 -07:00
|
|
|
};
|
|
|
|
|
},
|
2022-01-26 13:03:21 -05:00
|
|
|
|
|
|
|
|
plugin: function (i) {
|
|
|
|
|
if (this.type === 'plugin') {
|
|
|
|
|
return {
|
|
|
|
|
id: `plugin-id-${i}`,
|
|
|
|
|
name: pluginTypes[pluginTypeCounter++ % pluginTypes.length],
|
2022-08-01 11:25:07 -07:00
|
|
|
description: faker.random.words(),
|
2022-01-26 13:03:21 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2022-01-21 14:45:21 -08:00
|
|
|
attributes() {
|
2022-01-24 10:01:17 -08:00
|
|
|
switch (this.plugin?.name) {
|
|
|
|
|
case 'aws':
|
2022-02-08 14:44:15 -08:00
|
|
|
return {
|
2022-08-01 11:25:07 -07:00
|
|
|
disable_credential_rotation: faker.datatype.boolean(),
|
|
|
|
|
region: faker.address.state(),
|
2022-02-08 14:44:15 -08:00
|
|
|
};
|
|
|
|
|
case 'azure':
|
2022-01-24 10:01:17 -08:00
|
|
|
return {
|
2022-08-01 11:25:07 -07:00
|
|
|
disable_credential_rotation: faker.datatype.boolean(),
|
|
|
|
|
tenant_id: faker.datatype.hexadecimal(6),
|
|
|
|
|
client_id: faker.datatype.hexadecimal(6),
|
|
|
|
|
subscription_id: faker.datatype.hexadecimal(8),
|
2022-01-24 10:01:17 -08:00
|
|
|
};
|
2022-02-08 14:44:15 -08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
secrets() {
|
|
|
|
|
switch (this.plugin?.name) {
|
|
|
|
|
case 'aws':
|
2022-01-24 10:01:17 -08:00
|
|
|
return {
|
2022-08-01 11:25:07 -07:00
|
|
|
access_key_id: faker.datatype.hexadecimal(8),
|
|
|
|
|
secret_access_key: faker.datatype.string(12),
|
2022-01-24 10:01:17 -08:00
|
|
|
};
|
2022-02-08 14:44:15 -08:00
|
|
|
case 'azure':
|
|
|
|
|
return {
|
2022-08-01 11:25:07 -07:00
|
|
|
secret_id: faker.datatype.hexadecimal(6),
|
|
|
|
|
secret_value: faker.datatype.string(12),
|
2022-02-08 14:44:15 -08:00
|
|
|
};
|
2022-01-21 14:45:21 -08:00
|
|
|
}
|
|
|
|
|
},
|
2020-08-19 16:12:53 -04:00
|
|
|
withChildren: trait({
|
|
|
|
|
afterCreate(hostCatalog, server) {
|
2021-12-15 13:15:31 -05:00
|
|
|
const { scope, type } = hostCatalog;
|
|
|
|
|
const hosts = server.createList('host', 10, { scope, hostCatalog, type });
|
|
|
|
|
server.createList('host-set', 3, { scope, hostCatalog, hosts, type });
|
2021-03-04 15:47:38 -05:00
|
|
|
},
|
|
|
|
|
}),
|
2020-08-19 16:12:53 -04:00
|
|
|
});
|