Skip to content
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

feat: distinct entryPoint and function name #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion info/lib/displayServiceInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ module.exports = {
const funcEventConfig = serviceFunc.events[0][eventType];

let funcResource = funcEventConfig.resource || null;
let funcName = serviceFunc.handler;

if (serviceFunc.prependStage) {
funcName = `${this.options.stage}-${funcName}`;
}

if (serviceFunc.prependService) {
funcName = `${this.serverless.service.service}-${funcName}`;
}

if (eventType === 'http') {
const region = this.options.region;
const project = this.serverless.service.provider.project;
const baseUrl = `https://${region}-${project}.cloudfunctions.net`;
const path = serviceFunc.handler; // NOTE this might change
const path = funcName; // NOTE this might change
funcResource = `${baseUrl}/${path}`;
}

Expand Down Expand Up @@ -99,5 +108,6 @@ const getFunctionNameInService = (funcName, service, stage) => {
funcNameInService = funcNameInService.replace(service, '');
funcNameInService = funcNameInService.replace(stage, '');
funcNameInService = funcNameInService.slice(2, funcNameInService.length);

return funcNameInService;
};
24 changes: 24 additions & 0 deletions info/lib/displayServiceInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ describe('DisplayServiceInfo', () => {
},
],
},
func3: {
handler: 'handler',
prependStage: true,
events: [
{ http: 'foo3' },
],
},
func4: {
handler: 'handler',
prependService: true,
events: [
{ http: 'foo4' },
],
},
};
serverless.service.provider = {
project: 'my-project',
Expand Down Expand Up @@ -105,6 +119,8 @@ describe('DisplayServiceInfo', () => {
{ type: 'resource.which.should.be.filterered', name: 'someResource' },
{ type: 'cloudfunctions.v1beta2.function', name: 'my-service-dev-func1' },
{ type: 'cloudfunctions.v1beta2.function', name: 'my-service-dev-func2' },
{ type: 'cloudfunctions.v1beta2.function', name: 'my-service-dev-func3' },
{ type: 'cloudfunctions.v1beta2.function', name: 'my-service-dev-func4' },
],
};

Expand All @@ -123,6 +139,14 @@ describe('DisplayServiceInfo', () => {
name: 'func2',
resource: 'projects/*/topics/my-test-topic',
},
{
name: 'func3',
resource: 'https://us-central1-my-project.cloudfunctions.net/dev-handler',
},
{
name: 'func4',
resource: 'https://us-central1-my-project.cloudfunctions.net/my-service-handler',
},
],
},
};
Expand Down
22 changes: 19 additions & 3 deletions invoke/lib/invokeFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ module.exports = {
invoke() {
const project = this.serverless.service.provider.project;
const region = this.options.region;
const stage = this.options.stage ? this.options.stage : 'dev';
let func = this.options.function;
const data = this.options.data || '';

func = getGoogleCloudFunctionName(this.serverless.service.functions, func);
func = getGoogleCloudFunctionName(
this.serverless.service.functions,
func,
stage,
this.serverless.service.service,
);

const params = {
name: `projects/${project}/locations/${region}/functions/${func}`,
Expand Down Expand Up @@ -55,7 +61,7 @@ module.exports = {
};

// retrieve the functions name (Google uses our handler property as the function name)
const getGoogleCloudFunctionName = (serviceFunctions, func) => {
const getGoogleCloudFunctionName = (serviceFunctions, func, stage, service) => {
if (!serviceFunctions[func]) {
const errorMessage = [
`Function "${func}" not found. `,
Expand All @@ -64,5 +70,15 @@ const getGoogleCloudFunctionName = (serviceFunctions, func) => {
throw new Error(errorMessage);
}

return serviceFunctions[func].handler;
let funcName = serviceFunctions[func].handler;

if (serviceFunctions[func].prependStage) {
funcName = `${stage}-${funcName}`;
}

if (serviceFunctions[func].prependService) {
funcName = `${service}-${funcName}`;
}

return funcName;
};
70 changes: 70 additions & 0 deletions invoke/lib/invokeFunction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ describe('InvokeFunction', () => {
func1: {
handler: 'foo',
},
func2: {
handler: 'foo2',
prependService: true,
},
func3: {
handler: 'foo3',
prependStage: true,
},
func4: {
handler: 'foo4',
prependStage: true,
prependService: true,
},
};
serverless.setProvider('google', new GoogleProvider(serverless));
const options = {
Expand Down Expand Up @@ -86,6 +99,63 @@ describe('InvokeFunction', () => {
});
});

it('should invoke the provided function with prependService', () => {
googleInvoke.options.function = 'func2';

return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/my-service-foo2',
resource: {
data: '',
},
})).toEqual(true);
});
});

it('should invoke the provided function with prependStage', () => {
googleInvoke.options.function = 'func3';

return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/dev-foo3',
resource: {
data: '',
},
})).toEqual(true);
});
});

it('should invoke the provided function with all prepend', () => {
googleInvoke.options.function = 'func4';

return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/my-service-dev-foo4',
resource: {
data: '',
},
})).toEqual(true);
});
});

it('should invoke the provided function with the data option', () => {
googleInvoke.options.function = 'func1';
googleInvoke.options.data = '{ "some": "json" }';
Expand Down
23 changes: 18 additions & 5 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ module.exports = {

validateHandlerProperty(funcObject, functionName);
validateEventsProperty(funcObject, functionName);

const funcTemplate = getFunctionTemplate(
funcObject,
this.options.region,
this.options.stage,
this.serverless.service.service,
`gs://${
this.serverless.service.provider.deploymentBucketName
}/${this.serverless.service.package.artifactFilePath}`);
this.serverless.service.provider.deploymentBucketName
}/${this.serverless.service.package.artifactFilePath}`,
);

funcTemplate.properties.availableMemoryMb = _.get(funcObject, 'memorySize')
|| _.get(this, 'serverless.service.provider.memorySize')
Expand Down Expand Up @@ -109,15 +111,26 @@ const validateEventsProperty = (funcObject, functionName) => {
}
};

const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint-disable-line
const getFunctionTemplate = (funcObject, region, stage, service, sourceArchiveUrl) => { //eslint-disable-line
let funcName = funcObject.handler;

if (funcObject.prependStage) {
funcName = `${stage}-${funcName}`;
}

if (funcObject.prependService) {
funcName = `${service}-${funcName}`;
}

return {
type: 'cloudfunctions.v1beta2.function',
name: funcObject.name,
properties: {
location: region,
availableMemoryMb: 256,
timeout: '60s',
function: funcObject.handler,
entryPoint: funcObject.handler,
function: funcName,
sourceArchiveUrl,
},
};
Expand Down
Loading