async function parseAndEnrichPayload()

in src/poller/poller-core/index.js [397:501]


async function parseAndEnrichPayload(payload) {
  /** @type {AutoscalerMemorystoreCluster[]} */
  const clusters = await configValidator.parseAndAssertValidConfig(payload);
  const clustersFound = [];

  for (let clusterIdx = 0; clusterIdx < clusters.length; clusterIdx++) {
    // Reference before the modifications are made to the metrics structure
    const customMetrics =
      /** @type {MemorystoreClusterMetric[]} */
      (clusters[clusterIdx].metrics);

    // Merge in the defaults
    clusters[clusterIdx] = {...baseDefaults, ...clusters[clusterIdx]};

    if (clusters[clusterIdx].minSize < CLUSTER_SIZE_MIN) {
      throw new Error(
        `INVALID CONFIG: minSize (${clusters[clusterIdx].minSize}) is below the ` +
          `minimum cluster size of ${CLUSTER_SIZE_MIN}.`,
      );
    }

    if (clusters[clusterIdx].minSize > clusters[clusterIdx].maxSize) {
      throw new Error(
        `INVALID CONFIG: minSize (${clusters[clusterIdx].minSize}) is larger than ` +
          `maxSize (${clusters[clusterIdx].maxSize}).`,
      );
    }

    if (clusters[clusterIdx].units === undefined) {
      clusters[clusterIdx].units = AutoscalerUnits.SHARDS;
      logger.debug({
        message: `  Defaulted units to ${clusters[clusterIdx].units}`,
        projectId: clusters[clusterIdx].projectId,
        regionId: clusters[clusterIdx].regionId,
        clusterId: clusters[clusterIdx].clusterId,
      });
    }

    if (clusters[clusterIdx].units.toUpperCase() == AutoscalerUnits.SHARDS) {
      clusters[clusterIdx].units = clusters[clusterIdx].units.toUpperCase();
      clusters[clusterIdx] = {...shardDefaults, ...clusters[clusterIdx]};
    } else {
      throw new Error(
        `INVALID CONFIG: ${clusters[clusterIdx].units} is invalid. Valid: ${AutoscalerUnits.SHARDS}`,
      );
    }

    // Assemble the metrics
    clusters[clusterIdx].metrics = buildMetrics(clusters[clusterIdx]);

    if (customMetrics != null) {
      for (let customIdx = 0; customIdx < customMetrics.length; customIdx++) {
        const metricIdx = clusters[clusterIdx].metrics.findIndex(
          (x) => x.name === customMetrics[customIdx].name,
        );
        if (metricIdx != -1) {
          throw new Error(
            `INVALID CONFIG: custom metric ${customMetrics[customIdx].name} shadows default metric name`,
          );
        } else {
          /** @type {MemorystoreClusterMetric} */
          const metric = {...metricDefaults, ...customMetrics[customIdx]};
          const cluster = clusters[clusterIdx];
          if (
            validateCustomMetric(
              metric,
              cluster.projectId,
              cluster.regionId,
              cluster.clusterId,
            )
          ) {
            metric.filter = createBaseFilter(cluster) + metric.filter;
            cluster.metrics.push(metric);
            logger.debug({
              message:
                ` Added custom metric ${metric.name}` +
                ` with filter ${metric.filter}`,
              projectId: cluster.projectId,
              regionId: cluster.regionId,
              clusterId: cluster.clusterId,
            });
          }
        }
      }
    }

    try {
      clusters[clusterIdx] = {
        ...clusters[clusterIdx],
        ...(await getMemorystoreClusterMetadata(clusters[clusterIdx])),
      };
      clustersFound.push(clusters[clusterIdx]);
    } catch (err) {
      logger.error({
        message: `Unable to retrieve metadata for ${clusters[clusterIdx].projectId}/${clusters[clusterIdx].regionId}/${clusters[clusterIdx].clusterId}: ${err}`,
        projectId: clusters[clusterIdx].projectId,
        regionId: clusters[clusterIdx].regionId,
        clusterId: clusters[clusterIdx].clusterId,
        err: err,
      });
    }
  }

  return clustersFound;
}