Skip to content

Commit fb1fe3d

Browse files
authored
moving invoker into HttpsTrigger block and adding more tests (#960)
1 parent 1a727bb commit fb1fe3d

File tree

9 files changed

+49
-12
lines changed

9 files changed

+49
-12
lines changed

spec/common/encoding.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { expect } from 'chai';
22
import { convertInvoker } from '../../src/common/encoding';
33

44
describe('convertInvoker', () => {
5+
it('should raise an error on empty array', () => {
6+
expect(() => convertInvoker([])).to.throw;
7+
});
8+
9+
it('should raise an error on empty string', () => {
10+
expect(() => convertInvoker('')).to.throw;
11+
});
12+
13+
it('should raise an error on empty string with service accounts', () => {
14+
expect(() => convertInvoker(['service-account@', ''])).to.throw;
15+
});
16+
517
it('should raise an error on mixing public and service accounts', () => {
618
expect(() => convertInvoker(['public', 'service-account@'])).to.throw;
719
});

spec/v1/providers/https.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,14 @@ describe('CloudHttpsBuilder', () => {
107107
.runWith({
108108
timeoutSeconds: 90,
109109
memory: '256MB',
110+
invoker: 'private',
110111
})
111112
.https.onRequest(() => null);
112113

113114
expect(fn.__trigger.regions).to.deep.equal(['us-east1']);
114115
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
115116
expect(fn.__trigger.timeout).to.deep.equal('90s');
117+
expect(fn.__trigger.httpsTrigger.invoker).to.deep.equal(['private']);
116118
});
117119
});
118120
});

spec/v2/providers/https.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ describe('onRequest', () => {
111111
...FULL_TRIGGER,
112112
httpsTrigger: {
113113
allowInsecure: false,
114+
invoker: ['service-account1@', 'service-account2@'],
114115
},
115116
regions: ['us-west1', 'us-central1'],
116-
invoker: ['service-account1@', 'service-account2@'],
117117
});
118118
});
119119

@@ -141,12 +141,12 @@ describe('onRequest', () => {
141141
platform: 'gcfv2',
142142
httpsTrigger: {
143143
allowInsecure: false,
144+
invoker: ['private'],
144145
},
145146
concurrency: 20,
146147
minInstances: 3,
147148
regions: ['us-west1', 'us-central1'],
148149
labels: {},
149-
invoker: ['private'],
150150
});
151151
});
152152

src/cloud-functions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { warn } from './logger';
3232
export { Request, Response };
3333
import {
3434
convertIfPresent,
35-
convertInvoker,
3635
copyIfPresent,
3736
Duration,
3837
durationFromSeconds,
@@ -270,7 +269,9 @@ export interface TriggerAnnotated {
270269
service: string;
271270
};
272271
failurePolicy?: FailurePolicy;
273-
httpsTrigger?: {};
272+
httpsTrigger?: {
273+
invoker?: string[];
274+
};
274275
labels?: { [key: string]: string };
275276
regions?: string[];
276277
schedule?: Schedule;
@@ -279,7 +280,6 @@ export interface TriggerAnnotated {
279280
vpcConnectorEgressSettings?: string;
280281
serviceAccountEmail?: string;
281282
ingressSettings?: string;
282-
invoker?: string[];
283283
};
284284
}
285285

@@ -542,7 +542,6 @@ export function optionsToTrigger(options: DeploymentOptions) {
542542
'serviceAccount',
543543
serviceAccountFromShorthand
544544
);
545-
convertIfPresent(trigger, options, 'invoker', 'invoker', convertInvoker);
546545

547546
return trigger;
548547
}

src/common/encoding.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,20 @@ export function convertInvoker(invoker: string | string[]): string[] {
7070
invoker = [invoker];
7171
}
7272

73+
if (invoker.length === 0) {
74+
throw new Error('Invalid option for invoker: Must be a non-empty array.');
75+
}
76+
77+
if (invoker.find((inv) => inv.length === 0)) {
78+
throw new Error('Invalid option for invoker: Must be a non-empty string.');
79+
}
80+
7381
if (
7482
invoker.length > 1 &&
7583
invoker.find((inv) => inv === 'public' || inv === 'private')
7684
) {
7785
throw new Error(
78-
"Invalid option for invoker. Cannot have 'public' or 'private' in an array of service accounts"
86+
"Invalid option for invoker: Cannot have 'public' or 'private' in an array of service accounts."
7987
);
8088
}
8189

src/providers/https.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import * as express from 'express';
2424

2525
import { HttpsFunction, optionsToTrigger, Runnable } from '../cloud-functions';
26+
import { convertIfPresent, convertInvoker } from '../common/encoding';
2627
import {
2728
CallableContext,
2829
FunctionsErrorCode,
@@ -68,6 +69,13 @@ export function _onRequestWithOptions(
6869
...optionsToTrigger(options),
6970
httpsTrigger: {},
7071
};
72+
convertIfPresent(
73+
cloudFunction.__trigger.httpsTrigger,
74+
options,
75+
'invoker',
76+
'invoker',
77+
convertInvoker
78+
);
7179
// TODO parse the options
7280
return cloudFunction;
7381
}

src/v2/core.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ export interface TriggerAnnotation {
3232
service: string;
3333
};
3434
failurePolicy?: { retry: boolean };
35-
httpsTrigger?: {};
35+
httpsTrigger?: {
36+
invoker?: string[];
37+
};
3638
labels?: { [key: string]: string };
3739
regions?: string[];
3840
timeout?: string;
3941
vpcConnector?: string;
4042
vpcConnectorEgressSettings?: string;
4143
serviceAccountEmail?: string;
4244
ingressSettings?: string;
43-
invoker?: string[];
4445

4546
// TODO: schedule
4647
}

src/v2/options.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// SOFTWARE.
2222

2323
import {
24-
convertInvoker,
2524
durationFromSeconds,
2625
serviceAccountFromShorthand,
2726
} from '../common/encoding';
@@ -276,7 +275,6 @@ export function optionsToTriggerAnnotations(
276275
return retry ? { retry: true } : null;
277276
}
278277
);
279-
convertIfPresent(annotation, opts, 'invoker', 'invoker', convertInvoker);
280278

281279
return annotation;
282280
}

src/v2/providers/https.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import * as cors from 'cors';
2424
import * as express from 'express';
25+
import { convertIfPresent, convertInvoker } from '../../common/encoding';
2526

2627
import {
2728
CallableRequest,
@@ -104,7 +105,7 @@ export function onRequest(
104105
const specificOpts = options.optionsToTriggerAnnotations(
105106
opts as options.GlobalOptions
106107
);
107-
return {
108+
const trigger: any = {
108109
// TODO(inlined): Remove "apiVersion" once the latest version of the CLI
109110
// has migrated to "platform".
110111
apiVersion: 2,
@@ -119,6 +120,14 @@ export function onRequest(
119120
allowInsecure: false,
120121
},
121122
};
123+
convertIfPresent(
124+
trigger.httpsTrigger,
125+
opts,
126+
'invoker',
127+
'invoker',
128+
convertInvoker
129+
);
130+
return trigger;
122131
},
123132
});
124133
return handler as HttpsFunction;

0 commit comments

Comments
 (0)