Skip to content

Commit 9332ea0

Browse files
authored
allow Expressions in the top-level configuration of v1 functions (#1249)
1 parent 54b5c6c commit 9332ea0

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/v1/cloud-functions.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { DeploymentOptions } from "./function-configuration";
2626
export { Request, Response };
2727
import { convertIfPresent, copyIfPresent } from "../common/encoding";
2828
import { ManifestEndpoint, ManifestRequiredAPI } from "../runtime/manifest";
29+
import { Expression } from "../params";
2930

3031
export { Change } from "../common/change";
3132

@@ -475,17 +476,26 @@ export function optionsToEndpoint(options: DeploymentOptions): ManifestEndpoint
475476
endpoint.vpc = { connector: options.vpcConnector };
476477
convertIfPresent(endpoint.vpc, options, "egressSettings", "vpcConnectorEgressSettings");
477478
}
478-
convertIfPresent(endpoint, options, "availableMemoryMb", "memory", (mem) => {
479-
const memoryLookup = {
480-
"128MB": 128,
481-
"256MB": 256,
482-
"512MB": 512,
483-
"1GB": 1024,
484-
"2GB": 2048,
485-
"4GB": 4096,
486-
"8GB": 8192,
487-
};
488-
return memoryLookup[mem];
489-
});
479+
convertIfPresent(
480+
endpoint,
481+
options,
482+
"availableMemoryMb",
483+
"memory",
484+
(mem: string | Expression<number>) => {
485+
if (typeof mem === "string") {
486+
const memoryLookup = {
487+
"128MB": 128,
488+
"256MB": 256,
489+
"512MB": 512,
490+
"1GB": 1024,
491+
"2GB": 2048,
492+
"4GB": 4096,
493+
"8GB": 8192,
494+
};
495+
return memoryLookup[mem];
496+
}
497+
return mem;
498+
}
499+
);
490500
return endpoint;
491501
}

src/v1/function-builder.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ import * as testLab from "./providers/testLab";
5050
* @throws { Error } Memory and TimeoutSeconds values must be valid.
5151
*/
5252
function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
53-
if (runtimeOptions.memory && !VALID_MEMORY_OPTIONS.includes(runtimeOptions.memory)) {
53+
if (
54+
runtimeOptions.memory &&
55+
typeof runtimeOptions.memory === "string" &&
56+
!VALID_MEMORY_OPTIONS.includes(runtimeOptions.memory)
57+
) {
5458
throw new Error(
5559
`The only valid memory allocation values are: ${VALID_MEMORY_OPTIONS.join(", ")}`
5660
);

src/v1/function-configuration.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,23 @@ export interface RuntimeOptions {
174174
/**
175175
* Amount of memory to allocate to the function.
176176
*/
177-
memory?: typeof VALID_MEMORY_OPTIONS[number];
177+
memory?: typeof VALID_MEMORY_OPTIONS[number] | Expression<number>;
178178
/**
179179
* Timeout for the function in seconds, possible values are 0 to 540.
180180
*/
181-
timeoutSeconds?: number;
181+
timeoutSeconds?: number | Expression<number>;
182182

183183
/**
184184
* Min number of actual instances to be running at a given time.
185185
* Instances will be billed for memory allocation and 10% of CPU allocation
186186
* while idle.
187187
*/
188-
minInstances?: number;
188+
minInstances?: number | Expression<number>;
189189

190190
/**
191191
* Max number of actual instances allowed to be running in parallel.
192192
*/
193-
maxInstances?: number;
193+
maxInstances?: number | Expression<number>;
194194

195195
/**
196196
* Connect cloud function to specified VPC connector.

0 commit comments

Comments
 (0)