Skip to content

Commit 6b15ff7

Browse files
authored
Add support for VPC connectors in functions.runWith (#752)
1 parent bc9d8ca commit 6b15ff7

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

spec/function-builder.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,43 @@ describe('FunctionBuilder', () => {
199199
} as any);
200200
}).to.throw(Error, 'at least one region');
201201
});
202+
203+
it('should allow a vpcConnector to be set', () => {
204+
const fn = functions
205+
.runWith({
206+
vpcConnector: 'test-connector',
207+
})
208+
.auth.user()
209+
.onCreate((user) => user);
210+
211+
expect(fn.__trigger.vpcConnector).to.equal('test-connector');
212+
});
213+
214+
it('should allow a vpcConnectorEgressSettings to be set', () => {
215+
const fn = functions
216+
.runWith({
217+
vpcConnector: 'test-connector',
218+
vpcConnectorEgressSettings: 'PRIVATE_RANGES_ONLY',
219+
})
220+
.auth.user()
221+
.onCreate((user) => user);
222+
223+
expect(fn.__trigger.vpcConnectorEgressSettings).to.equal(
224+
'PRIVATE_RANGES_ONLY'
225+
);
226+
});
227+
228+
it('should throw an error if user chooses an invalid vpcConnectorEgressSettings', () => {
229+
expect(() => {
230+
return functions.runWith({
231+
vpcConnector: 'test-connector',
232+
vpcConnectorEgressSettings: 'INCORRECT_OPTION',
233+
} as any);
234+
}).to.throw(
235+
Error,
236+
`The only valid vpcConnectorEgressSettings values are: ${functions.VPC_EGRESS_SETTINGS_OPTIONS.join(
237+
','
238+
)}`
239+
);
240+
});
202241
});

src/cloud-functions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ export interface TriggerAnnotated {
270270
regions?: string[];
271271
schedule?: Schedule;
272272
timeout?: string;
273+
vpcConnector?: string;
274+
vpcConnectorEgressSettings?: string;
273275
};
274276
}
275277

@@ -514,5 +516,14 @@ export function optionsToTrigger(options: DeploymentOptions) {
514516
if (options.maxInstances) {
515517
trigger.maxInstances = options.maxInstances;
516518
}
519+
520+
if (options.vpcConnector) {
521+
trigger.vpcConnector = options.vpcConnector;
522+
}
523+
524+
if (options.vpcConnectorEgressSettings) {
525+
trigger.vpcConnectorEgressSettings = options.vpcConnectorEgressSettings;
526+
}
527+
517528
return trigger;
518529
}

src/function-builder.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
RuntimeOptions,
3131
SUPPORTED_REGIONS,
3232
VALID_MEMORY_OPTIONS,
33+
VPC_EGRESS_SETTINGS_OPTIONS,
3334
} from './function-configuration';
3435
import * as analytics from './providers/analytics';
3536
import * as auth from './providers/auth';
@@ -66,6 +67,21 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
6667
`TimeoutSeconds must be between 0 and ${MAX_TIMEOUT_SECONDS}`
6768
);
6869
}
70+
71+
if (
72+
runtimeOptions.vpcConnectorEgressSettings &&
73+
!_.includes(
74+
VPC_EGRESS_SETTINGS_OPTIONS,
75+
runtimeOptions.vpcConnectorEgressSettings
76+
)
77+
) {
78+
throw new Error(
79+
`The only valid vpcConnectorEgressSettings values are: ${VPC_EGRESS_SETTINGS_OPTIONS.join(
80+
','
81+
)}`
82+
);
83+
}
84+
6985
if (runtimeOptions.failurePolicy !== undefined) {
7086
if (
7187
_.isBoolean(runtimeOptions.failurePolicy) === false &&
@@ -116,13 +132,16 @@ export function region(
116132

117133
/**
118134
* Configure runtime options for the function.
119-
* @param runtimeOptions Object with three optional fields:
120-
* 1. failurePolicy: failure policy of the function, with boolean `true` being
135+
* @param runtimeOptions Object with optional fields:
136+
* 1. memory: amount of memory to allocate to the function, possible values
137+
* are: '128MB', '256MB', '512MB', '1GB', and '2GB'.
138+
* 2. timeoutSeconds: timeout for the function in seconds, possible values are
139+
* 0 to 540.
140+
* 3. failurePolicy: failure policy of the function, with boolean `true` being
121141
* equivalent to providing an empty retry object.
122-
* 2. memory: amount of memory to allocate to the function, with possible
123-
* values being '128MB', '256MB', '512MB', '1GB', and '2GB'.
124-
* 3. timeoutSeconds: timeout for the function in seconds, with possible
125-
* values being 0 to 540.
142+
* 4. vpcConnector: id of a VPC connector in same project and region
143+
* 5. vpcConnectorEgressSettings: when a vpcConnector is set, control which
144+
* egress traffic is sent through the vpcConnector.
126145
*
127146
* Value must not be null.
128147
*/

src/function-configuration.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export const VALID_MEMORY_OPTIONS = [
4444
'2GB',
4545
] as const;
4646

47+
/**
48+
* List of available options for VpcConnectorEgressSettings.
49+
*/
50+
export const VPC_EGRESS_SETTINGS_OPTIONS = [
51+
'VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED',
52+
'PRIVATE_RANGES_ONLY',
53+
'ALL_TRAFFIC',
54+
] as const;
55+
4756
/**
4857
* Scheduler retry options. Applies only to scheduled functions.
4958
*/
@@ -91,6 +100,16 @@ export interface RuntimeOptions {
91100
* Max number of actual instances allowed to be running in parallel
92101
*/
93102
maxInstances?: number;
103+
104+
/**
105+
* Connect cloud function to specified VPC connector
106+
*/
107+
vpcConnector?: string;
108+
109+
/**
110+
* Egress settings for VPC connector
111+
*/
112+
vpcConnectorEgressSettings?: typeof VPC_EGRESS_SETTINGS_OPTIONS[number];
94113
}
95114

96115
export interface DeploymentOptions extends RuntimeOptions {

0 commit comments

Comments
 (0)