github.com/emate/nomad@v0.8.2-wo-binpacking/ui/mirage/common.js (about) 1 import { faker } from 'ember-cli-mirage'; 2 import { provide } from './utils'; 3 4 // Realistically, resource reservations have a low cardinality 5 const CPU_RESERVATIONS = [250, 500, 1000, 2000, 2500, 4000]; 6 const MEMORY_RESERVATIONS = [256, 512, 1024, 2048, 4096, 8192]; 7 const DISK_RESERVATIONS = [200, 500, 1000, 2000, 5000, 10000, 100000]; 8 const IOPS_RESERVATIONS = [100000, 250000, 500000, 1000000, 10000000, 20000000]; 9 10 // There is also a good chance that certain resource restrictions are unbounded 11 IOPS_RESERVATIONS.push(...Array(1000).fill(0)); 12 DISK_RESERVATIONS.push(...Array(500).fill(0)); 13 14 export const DATACENTERS = provide( 15 15, 16 (n, i) => `${faker.address.countryCode().toLowerCase()}${i}` 17 ); 18 19 export const HOSTS = provide(100, () => { 20 const ip = Math.random() > 0.5 ? faker.internet.ip() : `[${ipv6()}]`; 21 return `${ip}:${faker.random.number({ min: 4000, max: 4999 })}`; 22 }); 23 24 export function generateResources(options = {}) { 25 return { 26 CPU: faker.random.arrayElement(CPU_RESERVATIONS), 27 MemoryMB: faker.random.arrayElement(MEMORY_RESERVATIONS), 28 DiskMB: faker.random.arrayElement(DISK_RESERVATIONS), 29 IOPS: faker.random.arrayElement(IOPS_RESERVATIONS), 30 Networks: generateNetworks(options.networks), 31 }; 32 } 33 34 export function generateNetworks(options = {}) { 35 return Array(faker.random.number({ min: 1, max: 3 })) 36 .fill(null) 37 .map(() => ({ 38 Device: `eth${faker.random.number({ max: 5 })}`, 39 CIDR: '', 40 IP: faker.internet.ip(), 41 MBits: 10, 42 ReservedPorts: Array( 43 faker.random.number({ 44 min: options.minPorts != null ? options.minPorts : 0, 45 max: options.maxPorts != null ? options.maxPorts : 2, 46 }) 47 ) 48 .fill(null) 49 .map(() => ({ 50 Label: faker.hacker.noun(), 51 Value: faker.random.number({ min: 5000, max: 60000 }), 52 })), 53 DynamicPorts: Array( 54 faker.random.number({ 55 min: options.minPorts != null ? options.minPorts : 0, 56 max: options.maxPorts != null ? options.maxPorts : 2, 57 }) 58 ) 59 .fill(null) 60 .map(() => ({ 61 Label: faker.hacker.noun(), 62 Value: faker.random.number({ min: 5000, max: 60000 }), 63 })), 64 })); 65 } 66 67 // Faker v4.0 has a built-in ipv6 function. Once Mirage upgrades, 68 // this code can be removed. 69 function ipv6() { 70 const subnets = []; 71 for (var i = 0; i < 8; i++) { 72 var subnet = []; 73 for (var char = 0; char < 4; char++) { 74 subnet.push(faker.random.number(15).toString(16)); 75 } 76 subnets.push(subnet.join('')); 77 } 78 return subnets.join(':'); 79 }