googleapis#compute_v1 TypeScript Examples

The following examples show how to use googleapis#compute_v1. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: gcpMachineTypes.ts    From cloud-pricing-api with Apache License 2.0 6 votes vote down vote up
async function calculateAmountFromTotal(
  product: Product,
  machineType: compute_v1.Schema$MachineType,
  purchaseOption: string
): Promise<{ amount: Decimal; effectiveDateStart: string } | null> {
  const machineTypeName = machineType.name || '';
  const prefix = machineTypeName.split('-')[0];

  const desc = machineTypeDescriptionLookups[prefix].total;

  let descRegex = new RegExp(`^${desc}`);
  if (purchaseOption === 'preemptible') {
    descRegex = new RegExp(`^Preemptible ${desc}`);
  }

  const matchedProduct = await findComputeProducts(product.region, descRegex);

  if (!matchedProduct) {
    config.logger.warn(
      `Could not find product for machine type ${machineTypeName} and purchase option ${purchaseOption}`
    );
    return null;
  }

  const matchedPrice =
    matchedProduct.prices.find((p) => p.endUsageAmount == null) ||
    matchedProduct.prices[0];
  const amount = new Decimal(matchedPrice.USD || 0);
  const effectiveDateStart =
    matchedPrice.effectiveDateStart || new Date().toString();

  return { amount, effectiveDateStart };
}
Example #2
Source File: gcpMachineTypes.ts    From cloud-pricing-api with Apache License 2.0 5 votes vote down vote up
async function createPrice(
  product: Product,
  machineType: compute_v1.Schema$MachineType,
  purchaseOption: string
): Promise<Price | null> {
  const machineTypeName = machineType.name || '';
  const prefix = machineTypeName.split('-')[0];

  let result: { amount: Decimal; effectiveDateStart: string } | null = null;

  if (!machineTypeDescriptionLookups[prefix]) {
    config.logger.warn(
      `Could not find description lookup for machine type ${machineTypeName}`
    );
    return null;
  }

  if (machineTypeDescriptionLookups[prefix].total) {
    result = await calculateAmountFromTotal(
      product,
      machineType,
      purchaseOption
    );
  } else {
    result = await calculateAmountFromCpuAndMem(
      product,
      machineType,
      purchaseOption
    );
  }
  if (!result) {
    return null;
  }
  const { amount, effectiveDateStart } = result;

  const price = {
    priceHash: '',
    purchaseOption,
    unit: 'Hours',
    USD: amount.toString(),
    effectiveDateStart: effectiveDateStart.toString(),
  };
  price.priceHash = generatePriceHash(product, price);
  return price;
}
Example #3
Source File: gcpMachineTypes.ts    From cloud-pricing-api with Apache License 2.0 5 votes vote down vote up
async function calculateAmountFromCpuAndMem(
  product: Product,
  machineType: compute_v1.Schema$MachineType,
  purchaseOption: string
): Promise<{ amount: Decimal; effectiveDateStart: string } | null> {
  const machineTypeName = machineType.name || '';
  const prefix = machineTypeName.split('-')[0];

  const cpuDesc = machineTypeDescriptionLookups[prefix].cpu;
  const memDesc = machineTypeDescriptionLookups[prefix].memory;

  let cpuDescRegex = new RegExp(`^${cpuDesc}`);
  let memDescRegex = new RegExp(`^${memDesc}`);
  if (purchaseOption === 'preemptible') {
    cpuDescRegex = new RegExp(`^Preemptible ${cpuDesc}`);
    memDescRegex = new RegExp(`^Preemptible ${memDesc}`);
  }

  const cpuProduct = await findComputeProducts(product.region, cpuDescRegex);
  const memProduct = await findComputeProducts(product.region, memDescRegex);

  if (!cpuProduct) {
    config.logger.warn(
      `Could not find CPU product for machine type ${machineTypeName} and purchase option ${purchaseOption}`
    );
    return null;
  }
  if (!memProduct) {
    config.logger.warn(
      `Could not find memory product for machine type ${machineTypeName} and purchase option ${purchaseOption}`
    );
    return null;
  }

  const overrides = machineTypeOverrides[machineTypeName];
  let cpu = new Decimal(machineType.guestCpus || 0);
  let mem = new Decimal(machineType.memoryMb || 0)
    .div(new Decimal(1024))
    .toDP(2);
  if (overrides?.cpu) {
    cpu = new Decimal(overrides.cpu);
  }
  if (overrides?.memory) {
    mem = new Decimal(overrides.memory);
  }

  const cpuPrice =
    cpuProduct.prices.find((p) => p.endUsageAmount == null) ||
    cpuProduct.prices[0];
  const memPrice =
    memProduct.prices.find((p) => p.endUsageAmount == null) ||
    memProduct.prices[0];

  const amount = cpu
    .mul(new Decimal(cpuPrice.USD || 0))
    .add(mem.mul(new Decimal(memPrice.USD || 0)));

  const cpuEffectiveDateStart = cpuPrice.effectiveDateStart;
  const memEffectiveDateStart = memPrice.effectiveDateStart;
  const effectiveDateStart =
    _.min([cpuEffectiveDateStart, memEffectiveDateStart]) ||
    new Date().toString();

  return { amount, effectiveDateStart };
}