Skip to content

Add support for VPC connectors in functions.runWith #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions spec/function-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
','
)}`
);
});
});
11 changes: 11 additions & 0 deletions src/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export interface TriggerAnnotated {
regions?: string[];
schedule?: Schedule;
timeout?: string;
vpcConnector?: string;
vpcConnectorEgressSettings?: string;
};
}

Expand Down Expand Up @@ -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;
}
31 changes: 25 additions & 6 deletions src/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/function-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 {
Expand Down