diff --git a/src/gcp/cloudfunctionsv2.spec.ts b/src/gcp/cloudfunctionsv2.spec.ts index f45fbfa7861..ff2c8ecef06 100644 --- a/src/gcp/cloudfunctionsv2.spec.ts +++ b/src/gcp/cloudfunctionsv2.spec.ts @@ -244,6 +244,10 @@ describe("cloudfunctionsv2", () => { const fullGcfFunction: cloudfunctionsv2.InputCloudFunction = { ...CLOUD_FUNCTION_V2, + buildConfig: { + ...CLOUD_FUNCTION_V2.buildConfig, + serviceAccount: "projects/project/serviceAccounts/inlined@google.com", + }, labels: { ...CLOUD_FUNCTION_V2.labels, foo: "bar", @@ -332,6 +336,10 @@ describe("cloudfunctionsv2", () => { const saGcfFunction: cloudfunctionsv2.InputCloudFunction = { ...CLOUD_FUNCTION_V2, + buildConfig: { + ...CLOUD_FUNCTION_V2.buildConfig, + serviceAccount: "projects/project/serviceAccounts/sa@google.com", + }, eventTrigger: { eventType: events.v2.DATABASE_EVENTS[0], eventFilters: [ @@ -405,6 +413,10 @@ describe("cloudfunctionsv2", () => { }), ).to.deep.equal({ ...CLOUD_FUNCTION_V2, + buildConfig: { + ...CLOUD_FUNCTION_V2.buildConfig, + serviceAccount: `projects/${ENDPOINT.project}/serviceAccounts/sa@${ENDPOINT.project}.iam.gserviceaccount.com`, + }, serviceConfig: { ...CLOUD_FUNCTION_V2.serviceConfig, serviceAccountEmail: `sa@${ENDPOINT.project}.iam.gserviceaccount.com`, @@ -421,6 +433,10 @@ describe("cloudfunctionsv2", () => { }), ).to.deep.equal({ ...CLOUD_FUNCTION_V2, + buildConfig: { + ...CLOUD_FUNCTION_V2.buildConfig, + serviceAccount: null, + }, serviceConfig: { ...CLOUD_FUNCTION_V2.serviceConfig, serviceAccountEmail: null, diff --git a/src/gcp/cloudfunctionsv2.ts b/src/gcp/cloudfunctionsv2.ts index aaafac63f78..e564c12bcad 100644 --- a/src/gcp/cloudfunctionsv2.ts +++ b/src/gcp/cloudfunctionsv2.ts @@ -51,6 +51,7 @@ export interface BuildConfig { source: Source; sourceToken?: string; environmentVariables: Record; + serviceAccount?: string | null; // Output only build?: string; @@ -467,16 +468,18 @@ export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunc "ingressSettings", "timeoutSeconds", ); - proto.convertIfPresent( - gcfFunction.serviceConfig, - endpoint, - "serviceAccountEmail", - "serviceAccount", - (from) => - !from - ? null - : proto.formatServiceAccount(from, endpoint.project, true /* removeTypePrefix */), - ); + + if (Object.prototype.hasOwnProperty.call(endpoint, "serviceAccount")) { + const serviceAccount = endpoint.serviceAccount; + if (!serviceAccount) { + gcfFunction.buildConfig.serviceAccount = null; + gcfFunction.serviceConfig.serviceAccountEmail = null; + } else { + const email = proto.formatServiceAccount(serviceAccount, endpoint.project, true); + gcfFunction.buildConfig.serviceAccount = `projects/${endpoint.project}/serviceAccounts/${email}`; + gcfFunction.serviceConfig.serviceAccountEmail = email; + } + } // Memory must be set because the default value of GCF gen 2 is Megabytes and // we use mebibytes const mem = endpoint.availableMemoryMb || backend.DEFAULT_MEMORY;