Skip to content

Annotate function triggers with __endpoint property #999

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 27 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
72c66ef
WIP.
taeold Oct 27, 2021
fb5af88
It's v1alpha1
taeold Oct 27, 2021
3c58ff3
Finish writing test. Cleanup.
taeold Oct 27, 2021
5b369f1
Prettier.
taeold Oct 27, 2021
2db4640
Nits.
taeold Oct 27, 2021
306c269
Prettier
taeold Oct 27, 2021
f5b0e35
Merge branch 'master' into dl-endpoint-prop
taeold Oct 27, 2021
584ff6d
Unroll v1alpha manifest out of dir.
taeold Oct 29, 2021
11a08fc
Merge branch 'dl-endpoint-prop' of https://github.com/firebase/fireba…
taeold Oct 29, 2021
e1d9003
Prefer timeoutSeconds over timeout.
taeold Nov 3, 2021
69a8846
Prefer topic over generic resource.
taeold Nov 3, 2021
8d7cbee
Whoops this shouldn't go.
taeold Nov 3, 2021
eb0a9b0
Refactor vpc option.
taeold Nov 4, 2021
105cc7c
Finalize refactor.
taeold Nov 4, 2021
3de212b
Don't transform SA, consistent fn name.
taeold Nov 5, 2021
c765db5
Unroll trigger options.
taeold Nov 5, 2021
08cde7a
Make callable its own trigger, fix tests.
taeold Nov 5, 2021
fafcbd5
No need for dynamic props.
taeold Nov 5, 2021
7864635
Prettier.
taeold Nov 5, 2021
4ca1d44
Better types.
taeold Nov 6, 2021
3f47fdc
Few more fixes.
taeold Nov 8, 2021
726b4eb
One mo nit.
taeold Nov 8, 2021
5eda9cd
No getters if unncessary.
taeold Nov 8, 2021
cec015f
Prettier.
taeold Nov 8, 2021
10f88da
Merge branch 'master' of https://github.com/firebase/firebase-functio…
taeold Nov 9, 2021
11f18e8
Rely on default converter function.
taeold Nov 13, 2021
fe43588
Simplify logic for copying vpc options.
taeold Nov 13, 2021
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
103 changes: 101 additions & 2 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,131 @@ describe('makeCloudFunction', () => {
legacyEventType: 'providers/provider/eventTypes/event',
};

it('should put a __trigger on the returned CloudFunction', () => {
it('should put a __trigger/__endpoint on the returned CloudFunction', () => {
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
});

expect(cf.__trigger).to.deep.equal({
eventTrigger: {
eventType: 'mock.provider.mock.event',
resource: 'resource',
service: 'service',
},
});

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
eventTrigger: {
eventType: 'mock.provider.mock.event',
eventFilters: {
resource: 'resource',
},
retry: false,
},
});
});

it('should have legacy event type in __trigger if provided', () => {
it('should have legacy event type in __trigger/__endpoint if provided', () => {
const cf = makeCloudFunction(cloudFunctionArgs);

expect(cf.__trigger).to.deep.equal({
eventTrigger: {
eventType: 'providers/provider/eventTypes/event',
resource: 'resource',
service: 'service',
},
});

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
eventTrigger: {
eventType: 'providers/provider/eventTypes/event',
eventFilters: {
resource: 'resource',
},
retry: false,
},
});
});

it('should include converted options in __endpoint', () => {
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
options: {
timeoutSeconds: 10,
regions: ['us-central1'],
memory: '128MB',
serviceAccount: 'foo@google.com',
},
});

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
timeoutSeconds: 10,
region: ['us-central1'],
availableMemoryMb: 128,
serviceAccountEmail: 'foo@google.com',
eventTrigger: {
eventType: 'mock.provider.mock.event',
eventFilters: {
resource: 'resource',
},
retry: false,
},
});
});

it('should set retry given failure policy in __endpoint', () => {
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
options: { failurePolicy: { retry: {} } },
});

expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
eventTrigger: {
eventType: 'mock.provider.mock.event',
eventFilters: {
resource: 'resource',
},
retry: true,
},
});
});

it('should setup a scheduleTrigger in __endpoint given a schedule', () => {
const schedule = {
schedule: 'every 5 minutes',
retryConfig: { retryCount: 3 },
timeZone: 'America/New_York',
};
const cf = makeCloudFunction({
provider: 'mock.provider',
eventType: 'mock.event',
service: 'service',
triggerResource: () => 'resource',
handler: () => null,
options: {
schedule,
},
});
expect(cf.__endpoint).to.deep.equal({
platform: 'gcfv1',
scheduleTrigger: schedule,
});
});

it('should construct the right context for event', () => {
Expand Down
32 changes: 27 additions & 5 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import { expect } from 'chai';
import * as express from 'express';
import * as _ from 'lodash';

import * as functions from '../../../src/index';
import * as https from '../../../src/providers/https';
import {
Expand Down Expand Up @@ -94,11 +94,15 @@ function runHandler(

describe('CloudHttpsBuilder', () => {
describe('#onRequest', () => {
it('should return a Trigger with appropriate values', () => {
it('should return a trigger/endpoint with appropriate values', () => {
const result = https.onRequest((req, resp) => {
resp.send(200);
});
expect(result.__trigger).to.deep.equal({ httpsTrigger: {} });
expect(result.__endpoint).to.deep.equal({
platform: 'gcfv1',
httpsTrigger: {},
});
});

it('should allow both region and runtime options to be set', () => {
Expand All @@ -115,37 +119,51 @@ describe('CloudHttpsBuilder', () => {
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal('90s');
expect(fn.__trigger.httpsTrigger.invoker).to.deep.equal(['private']);

expect(fn.__endpoint.region).to.deep.equal(['us-east1']);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__endpoint.httpsTrigger.invoker).to.deep.equal(['private']);
});
});
});

describe('handler namespace', () => {
describe('#onRequest', () => {
it('should return an empty trigger', () => {
it('should return an empty trigger and endpoint', () => {
const result = functions.handler.https.onRequest((req, res) => {
res.send(200);
});
expect(result.__trigger).to.deep.equal({});
expect(result.__endpoint).to.deep.equal({});
});
});

describe('#onCall', () => {
it('should return an empty trigger', () => {
it('should return an empty trigger and endpoint', () => {
const result = functions.handler.https.onCall(() => null);
expect(result.__trigger).to.deep.equal({});
expect(result.__endpoint).to.deep.equal({});
});
});
});

describe('#onCall', () => {
it('should return a Trigger with appropriate values', () => {
it('should return a trigger/endpoint with appropriate values', () => {
const result = https.onCall((data) => {
return 'response';
});

expect(result.__trigger).to.deep.equal({
httpsTrigger: {},
labels: { 'deployment-callable': 'true' },
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv1',
callableTrigger: {},
labels: {},
});
});

it('should allow both region and runtime options to be set', () => {
Expand All @@ -160,6 +178,10 @@ describe('#onCall', () => {
expect(fn.__trigger.regions).to.deep.equal(['us-east1']);
expect(fn.__trigger.availableMemoryMb).to.deep.equal(256);
expect(fn.__trigger.timeout).to.deep.equal('90s');

expect(fn.__endpoint.region).to.deep.equal(['us-east1']);
expect(fn.__endpoint.availableMemoryMb).to.deep.equal(256);
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
});

it('has a .run method', () => {
Expand Down
19 changes: 19 additions & 0 deletions spec/v2/providers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,22 @@ export const FULL_TRIGGER = {
hello: 'world',
},
};

export const FULL_ENDPOINT = {
platform: 'gcfv2',
region: ['us-west1'],
availableMemoryMb: 512,
timeoutSeconds: 60,
minInstances: 1,
maxInstances: 3,
concurrency: 20,
vpc: {
connector: 'aConnector',
egressSettings: 'ALL_TRAFFIC',
},
serviceAccountEmail: 'root@',
ingressSettings: 'ALLOW_ALL',
labels: {
hello: 'world',
},
};
62 changes: 57 additions & 5 deletions spec/v2/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
expectedResponseHeaders,
MockRequest,
} from '../../fixtures/mockrequest';
import { FULL_OPTIONS, FULL_TRIGGER } from './helpers';
import { FULL_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from './helpers';

/**
* RunHandlerResult contains the data from an express.Response.
Expand Down Expand Up @@ -82,10 +82,11 @@ describe('onRequest', () => {
delete process.env.GCLOUD_PROJECT;
});

it('should return a minimal trigger with appropriate values', () => {
it('should return a minimal trigger/endpoint with appropriate values', () => {
const result = https.onRequest((req, res) => {
res.send(200);
});

expect(result.__trigger).to.deep.equal({
apiVersion: 2,
platform: 'gcfv2',
Expand All @@ -94,9 +95,15 @@ describe('onRequest', () => {
},
labels: {},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
httpsTrigger: {},
labels: {},
});
});

it('should create a complex trigger with appropriate values', () => {
it('should create a complex trigger/endpoint with appropriate values', () => {
const result = https.onRequest(
{
...FULL_OPTIONS,
Expand All @@ -107,6 +114,7 @@ describe('onRequest', () => {
res.send(200);
}
);

expect(result.__trigger).to.deep.equal({
...FULL_TRIGGER,
httpsTrigger: {
Expand All @@ -115,6 +123,14 @@ describe('onRequest', () => {
},
regions: ['us-west1', 'us-central1'],
});

expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
httpsTrigger: {
invoker: ['service-account1@', 'service-account2@'],
},
region: ['us-west1', 'us-central1'],
});
});

it('should merge options and globalOptions', () => {
Expand Down Expand Up @@ -148,6 +164,17 @@ describe('onRequest', () => {
regions: ['us-west1', 'us-central1'],
labels: {},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
httpsTrigger: {
invoker: ['private'],
},
concurrency: 20,
minInstances: 3,
region: ['us-west1', 'us-central1'],
labels: {},
});
});

it('should be an express handler', async () => {
Expand Down Expand Up @@ -209,8 +236,9 @@ describe('onCall', () => {
delete process.env.GCLOUD_PROJECT;
});

it('should return a minimal trigger with appropriate values', () => {
it('should return a minimal trigger/endpoint with appropriate values', () => {
const result = https.onCall((request) => 42);

expect(result.__trigger).to.deep.equal({
apiVersion: 2,
platform: 'gcfv2',
Expand All @@ -221,10 +249,17 @@ describe('onCall', () => {
'deployment-callable': 'true',
},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
callableTrigger: {},
});
});

it('should create a complex trigger with appropriate values', () => {
it('should create a complex trigger/endpoint with appropriate values', () => {
const result = https.onCall(FULL_OPTIONS, (request) => 42);

expect(result.__trigger).to.deep.equal({
...FULL_TRIGGER,
httpsTrigger: {
Expand All @@ -235,6 +270,14 @@ describe('onCall', () => {
'deployment-callable': 'true',
},
});

expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
callableTrigger: {},
labels: {
...FULL_ENDPOINT.labels,
},
});
});

it('should merge options and globalOptions', () => {
Expand Down Expand Up @@ -265,6 +308,15 @@ describe('onCall', () => {
'deployment-callable': 'true',
},
});

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
callableTrigger: {},
concurrency: 20,
minInstances: 3,
region: ['us-west1', 'us-central1'],
labels: {},
});
});

it('has a .run method', () => {
Expand Down
Loading