Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into colerogers.firestor…
Browse files Browse the repository at this point in the history
…e-optional-data-field
  • Loading branch information
colerogers committed Mar 27, 2023
2 parents 5733efc + 1f2aeb5 commit f0ba6af
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1 +1,2 @@
- Add firestore 2nd gen triggers (#1358).
- Allow parametrized string type in ServiceAccount fields in Functions and trigger configs (#1309)
30 changes: 30 additions & 0 deletions spec/fixtures/sources/commonjs-parametrized-fields/index.js
@@ -0,0 +1,30 @@
const functions = require("../../../../src/v1/index");
const functionsv2 = require("../../../../src/v2/index");
const params = require("../../../../src/params");
params.clearParams();

const stringParam = params.defineString("STRING_PARAM");
const intParam = params.defineInt("INT_PARAM");
const boolParam = params.defineBoolean("BOOLEAN_PARAM");

exports.v1http = functions.runWith({
minInstances: intParam,
maxInstances: intParam,
memory: intParam,
timeoutSeconds: intParam,
serviceAccount: stringParam,
omit: boolParam
}).https.onRequest((req, resp) => {
resp.status(200).send("Hello world!");
});

exports.v2http = functionsv2.https.onRequest({
minInstances: intParam,
maxInstances: intParam,
memory: intParam,
timeoutSeconds: intParam,
serviceAccount: stringParam,
omit: boolParam
}, (req, resp) => {
resp.status(200).send("Hello world!");
});
@@ -0,0 +1,3 @@
{
"name": "commonjs-parametrized-fields"
}
40 changes: 40 additions & 0 deletions spec/runtime/loader.spec.ts
Expand Up @@ -7,6 +7,7 @@ import { ManifestEndpoint, ManifestRequiredAPI, ManifestStack } from "../../src/
import { clearParams } from "../../src/params";
import { MINIMAL_V1_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../fixtures";
import { MINIMAL_SCHEDULE_TRIGGER, MINIMIAL_TASK_QUEUE_TRIGGER } from "../v1/providers/fixtures";
import { BooleanParam, IntParam, StringParam } from "../../src/params/types";

describe("extractStack", () => {
const httpFn = functions.https.onRequest(() => undefined);
Expand Down Expand Up @@ -376,6 +377,45 @@ describe("loadStack", () => {
],
},
},
{
name: "can use parameterized fields",
modulePath: "./spec/fixtures/sources/commonjs-parametrized-fields",
expected: {
...expected,
params: [
{ name: "STRING_PARAM", type: "string" },
{ name: "INT_PARAM", type: "int" },
{ name: "BOOLEAN_PARAM", type: "boolean" },
],
endpoints: {
v1http: {
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
entryPoint: "v1http",
minInstances: new IntParam("INT_PARAM"),
maxInstances: new IntParam("INT_PARAM"),
availableMemoryMb: new IntParam("INT_PARAM"),
timeoutSeconds: new IntParam("INT_PARAM"),
serviceAccountEmail: new StringParam("STRING_PARAM"),
omit: new BooleanParam("BOOLEAN_PARAM"),
httpsTrigger: {},
},
v2http: {
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
entryPoint: "v2http",
minInstances: new IntParam("INT_PARAM"),
maxInstances: new IntParam("INT_PARAM"),
availableMemoryMb: new IntParam("INT_PARAM"),
timeoutSeconds: new IntParam("INT_PARAM"),
serviceAccountEmail: new StringParam("STRING_PARAM"),
omit: new BooleanParam("BOOLEAN_PARAM"),
labels: {},
httpsTrigger: {},
},
},
},
},
];

for (const tc of testcases) {
Expand Down
8 changes: 7 additions & 1 deletion src/common/encoding.ts
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { Expression } from "../params";

// Copied from firebase-tools/src/gcp/proto

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

export function serviceAccountFromShorthand(serviceAccount: string): string | null {
export function serviceAccountFromShorthand(
serviceAccount: string | Expression<string>
): string | Expression<string> | null {
if (serviceAccount === "default") {
return null;
} else if (serviceAccount instanceof Expression) {
return serviceAccount;
} else if (serviceAccount.endsWith("@")) {
if (!process.env.GCLOUD_PROJECT) {
throw new Error(
Expand Down
13 changes: 8 additions & 5 deletions src/v1/function-builder.ts
Expand Up @@ -23,7 +23,7 @@
import * as express from "express";

import { ResetValue } from "../common/options";
import { SecretParam } from "../params/types";
import { Expression, SecretParam } from "../params/types";
import { EventContext } from "./cloud-functions";
import {
DeploymentOptions,
Expand Down Expand Up @@ -88,12 +88,15 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
const serviceAccount = runtimeOptions.serviceAccount;
if (
serviceAccount &&
serviceAccount !== "default" &&
!(serviceAccount instanceof ResetValue) &&
!serviceAccount.includes("@")
!(
serviceAccount === "default" ||
serviceAccount instanceof ResetValue ||
serviceAccount instanceof Expression ||
serviceAccount.includes("@")
)
) {
throw new Error(
`serviceAccount must be set to 'default', a service account email, or '{serviceAccountName}@'`
`serviceAccount must be set to 'default', a string expression, a service account email, or '{serviceAccountName}@'`
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/v1/function-configuration.ts
Expand Up @@ -215,7 +215,7 @@ export interface RuntimeOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: "default" | string | ResetValue;
serviceAccount?: "default" | string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
4 changes: 2 additions & 2 deletions src/v2/options.ts
Expand Up @@ -172,7 +172,7 @@ export interface GlobalOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

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

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

/** The name of the channel where the function receives events. */
channel?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/alerts/alerts.ts
Expand Up @@ -163,7 +163,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
* Specific service account for the function to run as.
* A value of null restores the default service account.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/alerts/appDistribution.ts
Expand Up @@ -175,7 +175,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/alerts/crashlytics.ts
Expand Up @@ -255,7 +255,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/database.ts
Expand Up @@ -176,7 +176,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/eventarc.ts
Expand Up @@ -140,7 +140,7 @@ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/https.ts
Expand Up @@ -133,7 +133,7 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region"> {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/identity.ts
Expand Up @@ -142,7 +142,7 @@ export interface BlockingOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/pubsub.ts
Expand Up @@ -233,7 +233,7 @@ export interface PubSubOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/storage.ts
Expand Up @@ -281,7 +281,7 @@ export interface StorageOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down
2 changes: 1 addition & 1 deletion src/v2/providers/tasks.ts
Expand Up @@ -138,7 +138,7 @@ export interface TaskQueueOptions extends options.EventHandlerOptions {
/**
* Specific service account for the function to run as.
*/
serviceAccount?: string | ResetValue;
serviceAccount?: string | Expression<string> | ResetValue;

/**
* Ingress settings which control where this function can be called from.
Expand Down

0 comments on commit f0ba6af

Please sign in to comment.