2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-14 15:16:35 -04:00
|
|
|
import factory from '../generated/factories/host';
|
2021-08-18 15:56:25 -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-02-11 10:16:23 -08:00
|
|
|
|
2020-08-21 18:21:17 -04:00
|
|
|
export default factory.extend({
|
2022-06-01 14:02:18 -04:00
|
|
|
id: () => generateId('h_'),
|
2023-04-25 16:22:12 -05:00
|
|
|
name() {
|
|
|
|
|
if (this.type === 'static') return faker.random.words();
|
|
|
|
|
},
|
2022-01-27 11:36:29 -08:00
|
|
|
type() {
|
|
|
|
|
return this.hostCatalog?.type || factory.attrs.type;
|
|
|
|
|
},
|
2022-02-02 17:04:09 -05:00
|
|
|
authorized_actions: function () {
|
|
|
|
|
const isStatic = this.type === 'static';
|
|
|
|
|
const defaults = ['no-op', 'read'];
|
|
|
|
|
// Only static allows update/delete at this time.
|
|
|
|
|
if (isStatic) defaults.push('update', 'delete');
|
|
|
|
|
return permissions.authorizedActionsFor('host') || defaults;
|
|
|
|
|
},
|
2022-01-26 11:39:01 -08:00
|
|
|
plugin() {
|
|
|
|
|
if (this.type === 'plugin') {
|
|
|
|
|
return this.hostCatalog.plugin;
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-02-15 15:12:20 -08:00
|
|
|
ip_addresses() {
|
|
|
|
|
const addressesAmount = Math.floor(Math.random() * 6) + 1;
|
|
|
|
|
let result = [];
|
|
|
|
|
for (let i = 0; i < addressesAmount; ++i) {
|
2022-08-01 11:25:07 -07:00
|
|
|
result.push(faker.internet.ip());
|
2022-02-15 15:12:20 -08:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
},
|
2023-04-25 16:22:12 -05:00
|
|
|
// Return external fields only for plugins
|
2022-02-15 15:12:20 -08:00
|
|
|
external_id() {
|
2023-04-25 16:22:12 -05:00
|
|
|
if (this.type === 'plugin') {
|
|
|
|
|
return faker.datatype.uuid();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
external_name(i) {
|
|
|
|
|
if (this.type === 'plugin') {
|
|
|
|
|
return `${this.plugin.name} provided name ${i}`;
|
|
|
|
|
}
|
2022-02-15 15:12:20 -08:00
|
|
|
},
|
|
|
|
|
// Only aws plugins have dns_names
|
|
|
|
|
dns_names() {
|
|
|
|
|
if (this.plugin?.name === 'aws') {
|
|
|
|
|
const dnsNamesAmount = Math.floor(Math.random() * 6) + 1;
|
|
|
|
|
let result = [];
|
|
|
|
|
for (let i = 0; i < dnsNamesAmount; i++) {
|
2022-08-01 11:25:07 -07:00
|
|
|
result.push(faker.internet.domainName());
|
2022-02-15 15:12:20 -08:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-02-11 10:16:23 -08:00
|
|
|
},
|
2020-08-21 18:21:17 -04:00
|
|
|
});
|