diff --git a/spec/function-builder.spec.ts b/spec/function-builder.spec.ts index 1f9122061..eec531fdf 100644 --- a/spec/function-builder.spec.ts +++ b/spec/function-builder.spec.ts @@ -199,4 +199,43 @@ describe('FunctionBuilder', () => { } as any); }).to.throw(Error, 'at least one region'); }); + + it('should allow a vpcConnector to be set', () => { + const fn = functions + .runWith({ + vpcConnector: 'test-connector', + }) + .auth.user() + .onCreate((user) => user); + + expect(fn.__trigger.vpcConnector).to.equal('test-connector'); + }); + + it('should allow a vpcConnectorEgressSettings to be set', () => { + const fn = functions + .runWith({ + vpcConnector: 'test-connector', + vpcConnectorEgressSettings: 'PRIVATE_RANGES_ONLY', + }) + .auth.user() + .onCreate((user) => user); + + expect(fn.__trigger.vpcConnectorEgressSettings).to.equal( + 'PRIVATE_RANGES_ONLY' + ); + }); + + it('should throw an error if user chooses an invalid vpcConnectorEgressSettings', () => { + expect(() => { + return functions.runWith({ + vpcConnector: 'test-connector', + vpcConnectorEgressSettings: 'INCORRECT_OPTION', + } as any); + }).to.throw( + Error, + `The only valid vpcConnectorEgressSettings values are: ${functions.VPC_EGRESS_SETTINGS_OPTIONS.join( + ',' + )}` + ); + }); }); diff --git a/src/cloud-functions.ts b/src/cloud-functions.ts index 387953bdc..109a1ad05 100644 --- a/src/cloud-functions.ts +++ b/src/cloud-functions.ts @@ -270,6 +270,8 @@ export interface TriggerAnnotated { regions?: string[]; schedule?: Schedule; timeout?: string; + vpcConnector?: string; + vpcConnectorEgressSettings?: string; }; } @@ -514,5 +516,14 @@ export function optionsToTrigger(options: DeploymentOptions) { if (options.maxInstances) { trigger.maxInstances = options.maxInstances; } + + if (options.vpcConnector) { + trigger.vpcConnector = options.vpcConnector; + } + + if (options.vpcConnectorEgressSettings) { + trigger.vpcConnectorEgressSettings = options.vpcConnectorEgressSettings; + } + return trigger; } diff --git a/src/function-builder.ts b/src/function-builder.ts index 6050927ea..919f9f09b 100644 --- a/src/function-builder.ts +++ b/src/function-builder.ts @@ -30,6 +30,7 @@ import { RuntimeOptions, SUPPORTED_REGIONS, VALID_MEMORY_OPTIONS, + VPC_EGRESS_SETTINGS_OPTIONS, } from './function-configuration'; import * as analytics from './providers/analytics'; import * as auth from './providers/auth'; @@ -66,6 +67,21 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean { `TimeoutSeconds must be between 0 and ${MAX_TIMEOUT_SECONDS}` ); } + + if ( + runtimeOptions.vpcConnectorEgressSettings && + !_.includes( + VPC_EGRESS_SETTINGS_OPTIONS, + runtimeOptions.vpcConnectorEgressSettings + ) + ) { + throw new Error( + `The only valid vpcConnectorEgressSettings values are: ${VPC_EGRESS_SETTINGS_OPTIONS.join( + ',' + )}` + ); + } + if (runtimeOptions.failurePolicy !== undefined) { if ( _.isBoolean(runtimeOptions.failurePolicy) === false && @@ -116,13 +132,16 @@ export function region( /** * Configure runtime options for the function. - * @param runtimeOptions Object with three optional fields: - * 1. failurePolicy: failure policy of the function, with boolean `true` being + * @param runtimeOptions Object with optional fields: + * 1. memory: amount of memory to allocate to the function, possible values + * are: '128MB', '256MB', '512MB', '1GB', and '2GB'. + * 2. timeoutSeconds: timeout for the function in seconds, possible values are + * 0 to 540. + * 3. failurePolicy: failure policy of the function, with boolean `true` being * equivalent to providing an empty retry object. - * 2. memory: amount of memory to allocate to the function, with possible - * values being '128MB', '256MB', '512MB', '1GB', and '2GB'. - * 3. timeoutSeconds: timeout for the function in seconds, with possible - * values being 0 to 540. + * 4. vpcConnector: id of a VPC connector in same project and region + * 5. vpcConnectorEgressSettings: when a vpcConnector is set, control which + * egress traffic is sent through the vpcConnector. * * Value must not be null. */ diff --git a/src/function-configuration.ts b/src/function-configuration.ts index 31c708f3f..558895148 100644 --- a/src/function-configuration.ts +++ b/src/function-configuration.ts @@ -44,6 +44,15 @@ export const VALID_MEMORY_OPTIONS = [ '2GB', ] as const; +/** + * List of available options for VpcConnectorEgressSettings. + */ +export const VPC_EGRESS_SETTINGS_OPTIONS = [ + 'VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED', + 'PRIVATE_RANGES_ONLY', + 'ALL_TRAFFIC', +] as const; + /** * Scheduler retry options. Applies only to scheduled functions. */ @@ -91,6 +100,16 @@ export interface RuntimeOptions { * Max number of actual instances allowed to be running in parallel */ maxInstances?: number; + + /** + * Connect cloud function to specified VPC connector + */ + vpcConnector?: string; + + /** + * Egress settings for VPC connector + */ + vpcConnectorEgressSettings?: typeof VPC_EGRESS_SETTINGS_OPTIONS[number]; } export interface DeploymentOptions extends RuntimeOptions {