From e42896b16c5540e3ea41716f45a52d6836623bd7 Mon Sep 17 00:00:00 2001 From: Victor Fan Date: Fri, 30 Sep 2022 12:08:23 -0700 Subject: [PATCH 1/2] allow Expressions in the top-level configuration of v1 functions --- src/v1/function-configuration.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/v1/function-configuration.ts b/src/v1/function-configuration.ts index 79d4409c9..2724c8818 100644 --- a/src/v1/function-configuration.ts +++ b/src/v1/function-configuration.ts @@ -174,23 +174,23 @@ export interface RuntimeOptions { /** * Amount of memory to allocate to the function. */ - memory?: typeof VALID_MEMORY_OPTIONS[number]; + memory?: typeof VALID_MEMORY_OPTIONS[number] | Expression; /** * Timeout for the function in seconds, possible values are 0 to 540. */ - timeoutSeconds?: number; + timeoutSeconds?: number | Expression; /** * Min number of actual instances to be running at a given time. * Instances will be billed for memory allocation and 10% of CPU allocation * while idle. */ - minInstances?: number; + minInstances?: number | Expression; /** * Max number of actual instances allowed to be running in parallel. */ - maxInstances?: number; + maxInstances?: number | Expression; /** * Connect cloud function to specified VPC connector. From 8552adb9a29959891b68440eca397161abbe9ee9 Mon Sep 17 00:00:00 2001 From: Victor Fan Date: Fri, 30 Sep 2022 12:50:29 -0700 Subject: [PATCH 2/2] make it actually work --- src/v1/cloud-functions.ts | 34 ++++++++++++++++++++++------------ src/v1/function-builder.ts | 6 +++++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/v1/cloud-functions.ts b/src/v1/cloud-functions.ts index 00eba0040..2f6805843 100644 --- a/src/v1/cloud-functions.ts +++ b/src/v1/cloud-functions.ts @@ -26,6 +26,7 @@ import { DeploymentOptions } from "./function-configuration"; export { Request, Response }; import { convertIfPresent, copyIfPresent } from "../common/encoding"; import { ManifestEndpoint, ManifestRequiredAPI } from "../runtime/manifest"; +import { Expression } from "../params"; export { Change } from "../common/change"; @@ -475,17 +476,26 @@ export function optionsToEndpoint(options: DeploymentOptions): ManifestEndpoint endpoint.vpc = { connector: options.vpcConnector }; convertIfPresent(endpoint.vpc, options, "egressSettings", "vpcConnectorEgressSettings"); } - convertIfPresent(endpoint, options, "availableMemoryMb", "memory", (mem) => { - const memoryLookup = { - "128MB": 128, - "256MB": 256, - "512MB": 512, - "1GB": 1024, - "2GB": 2048, - "4GB": 4096, - "8GB": 8192, - }; - return memoryLookup[mem]; - }); + convertIfPresent( + endpoint, + options, + "availableMemoryMb", + "memory", + (mem: string | Expression) => { + if (typeof mem === "string") { + const memoryLookup = { + "128MB": 128, + "256MB": 256, + "512MB": 512, + "1GB": 1024, + "2GB": 2048, + "4GB": 4096, + "8GB": 8192, + }; + return memoryLookup[mem]; + } + return mem; + } + ); return endpoint; } diff --git a/src/v1/function-builder.ts b/src/v1/function-builder.ts index 41286e3c7..b5cf4dce6 100644 --- a/src/v1/function-builder.ts +++ b/src/v1/function-builder.ts @@ -50,7 +50,11 @@ import * as testLab from "./providers/testLab"; * @throws { Error } Memory and TimeoutSeconds values must be valid. */ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean { - if (runtimeOptions.memory && !VALID_MEMORY_OPTIONS.includes(runtimeOptions.memory)) { + if ( + runtimeOptions.memory && + typeof runtimeOptions.memory === "string" && + !VALID_MEMORY_OPTIONS.includes(runtimeOptions.memory) + ) { throw new Error( `The only valid memory allocation values are: ${VALID_MEMORY_OPTIONS.join(", ")}` );