Skip to content

Commit 1f2aeb5

Browse files
authored
Make ServiceAccount parametrizable (#1309)
Make ServiceAccount parametrizable
1 parent 3efab1d commit 1f2aeb5

File tree

18 files changed

+102
-19
lines changed

18 files changed

+102
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
- Add firestore 2nd gen triggers (#1358).
2+
- Allow parametrized string type in ServiceAccount fields in Functions and trigger configs (#1309)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const functions = require("../../../../src/v1/index");
2+
const functionsv2 = require("../../../../src/v2/index");
3+
const params = require("../../../../src/params");
4+
params.clearParams();
5+
6+
const stringParam = params.defineString("STRING_PARAM");
7+
const intParam = params.defineInt("INT_PARAM");
8+
const boolParam = params.defineBoolean("BOOLEAN_PARAM");
9+
10+
exports.v1http = functions.runWith({
11+
minInstances: intParam,
12+
maxInstances: intParam,
13+
memory: intParam,
14+
timeoutSeconds: intParam,
15+
serviceAccount: stringParam,
16+
omit: boolParam
17+
}).https.onRequest((req, resp) => {
18+
resp.status(200).send("Hello world!");
19+
});
20+
21+
exports.v2http = functionsv2.https.onRequest({
22+
minInstances: intParam,
23+
maxInstances: intParam,
24+
memory: intParam,
25+
timeoutSeconds: intParam,
26+
serviceAccount: stringParam,
27+
omit: boolParam
28+
}, (req, resp) => {
29+
resp.status(200).send("Hello world!");
30+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "commonjs-parametrized-fields"
3+
}

spec/runtime/loader.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ManifestEndpoint, ManifestRequiredAPI, ManifestStack } from "../../src/
77
import { clearParams } from "../../src/params";
88
import { MINIMAL_V1_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../fixtures";
99
import { MINIMAL_SCHEDULE_TRIGGER, MINIMIAL_TASK_QUEUE_TRIGGER } from "../v1/providers/fixtures";
10+
import { BooleanParam, IntParam, StringParam } from "../../src/params/types";
1011

1112
describe("extractStack", () => {
1213
const httpFn = functions.https.onRequest(() => undefined);
@@ -376,6 +377,45 @@ describe("loadStack", () => {
376377
],
377378
},
378379
},
380+
{
381+
name: "can use parameterized fields",
382+
modulePath: "./spec/fixtures/sources/commonjs-parametrized-fields",
383+
expected: {
384+
...expected,
385+
params: [
386+
{ name: "STRING_PARAM", type: "string" },
387+
{ name: "INT_PARAM", type: "int" },
388+
{ name: "BOOLEAN_PARAM", type: "boolean" },
389+
],
390+
endpoints: {
391+
v1http: {
392+
...MINIMAL_V1_ENDPOINT,
393+
platform: "gcfv1",
394+
entryPoint: "v1http",
395+
minInstances: new IntParam("INT_PARAM"),
396+
maxInstances: new IntParam("INT_PARAM"),
397+
availableMemoryMb: new IntParam("INT_PARAM"),
398+
timeoutSeconds: new IntParam("INT_PARAM"),
399+
serviceAccountEmail: new StringParam("STRING_PARAM"),
400+
omit: new BooleanParam("BOOLEAN_PARAM"),
401+
httpsTrigger: {},
402+
},
403+
v2http: {
404+
...MINIMAL_V2_ENDPOINT,
405+
platform: "gcfv2",
406+
entryPoint: "v2http",
407+
minInstances: new IntParam("INT_PARAM"),
408+
maxInstances: new IntParam("INT_PARAM"),
409+
availableMemoryMb: new IntParam("INT_PARAM"),
410+
timeoutSeconds: new IntParam("INT_PARAM"),
411+
serviceAccountEmail: new StringParam("STRING_PARAM"),
412+
omit: new BooleanParam("BOOLEAN_PARAM"),
413+
labels: {},
414+
httpsTrigger: {},
415+
},
416+
},
417+
},
418+
},
379419
];
380420

381421
for (const tc of testcases) {

src/common/encoding.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
import { Expression } from "../params";
24+
2325
// Copied from firebase-tools/src/gcp/proto
2426

2527
/**
@@ -72,9 +74,13 @@ export function convertIfPresent<Src, Dest>(
7274
dest[destField] = converter(src[srcField]);
7375
}
7476

75-
export function serviceAccountFromShorthand(serviceAccount: string): string | null {
77+
export function serviceAccountFromShorthand(
78+
serviceAccount: string | Expression<string>
79+
): string | Expression<string> | null {
7680
if (serviceAccount === "default") {
7781
return null;
82+
} else if (serviceAccount instanceof Expression) {
83+
return serviceAccount;
7884
} else if (serviceAccount.endsWith("@")) {
7985
if (!process.env.GCLOUD_PROJECT) {
8086
throw new Error(

src/v1/function-builder.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import * as express from "express";
2424

2525
import { ResetValue } from "../common/options";
26-
import { SecretParam } from "../params/types";
26+
import { Expression, SecretParam } from "../params/types";
2727
import { EventContext } from "./cloud-functions";
2828
import {
2929
DeploymentOptions,
@@ -88,12 +88,15 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
8888
const serviceAccount = runtimeOptions.serviceAccount;
8989
if (
9090
serviceAccount &&
91-
serviceAccount !== "default" &&
92-
!(serviceAccount instanceof ResetValue) &&
93-
!serviceAccount.includes("@")
91+
!(
92+
serviceAccount === "default" ||
93+
serviceAccount instanceof ResetValue ||
94+
serviceAccount instanceof Expression ||
95+
serviceAccount.includes("@")
96+
)
9497
) {
9598
throw new Error(
96-
`serviceAccount must be set to 'default', a service account email, or '{serviceAccountName}@'`
99+
`serviceAccount must be set to 'default', a string expression, a service account email, or '{serviceAccountName}@'`
97100
);
98101
}
99102

src/v1/function-configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export interface RuntimeOptions {
215215
/**
216216
* Specific service account for the function to run as.
217217
*/
218-
serviceAccount?: "default" | string | ResetValue;
218+
serviceAccount?: "default" | string | Expression<string> | ResetValue;
219219

220220
/**
221221
* Ingress settings which control where this function can be called from.

src/v2/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export interface GlobalOptions {
172172
/**
173173
* Specific service account for the function to run as.
174174
*/
175-
serviceAccount?: string | ResetValue;
175+
serviceAccount?: string | Expression<string> | ResetValue;
176176

177177
/**
178178
* Ingress settings which control where this function can be called from.
@@ -255,7 +255,7 @@ export interface EventHandlerOptions extends Omit<GlobalOptions, "enforceAppChec
255255
region?: string;
256256

257257
/** The service account that EventArc should use to invoke this function. Requires the P4SA to have ActAs permission on this service account. */
258-
serviceAccount?: string | ResetValue;
258+
serviceAccount?: string | Expression<string> | ResetValue;
259259

260260
/** The name of the channel where the function receives events. */
261261
channel?: string;

src/v2/providers/alerts/alerts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
163163
* Specific service account for the function to run as.
164164
* A value of null restores the default service account.
165165
*/
166-
serviceAccount?: string | ResetValue;
166+
serviceAccount?: string | Expression<string> | ResetValue;
167167

168168
/**
169169
* Ingress settings which control where this function can be called from.

src/v2/providers/alerts/appDistribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
175175
/**
176176
* Specific service account for the function to run as.
177177
*/
178-
serviceAccount?: string | ResetValue;
178+
serviceAccount?: string | Expression<string> | ResetValue;
179179

180180
/**
181181
* Ingress settings which control where this function can be called from.

0 commit comments

Comments
 (0)