Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

feat: Func plugin to add/remove functions within function app #151

Merged
merged 27 commits into from
May 31, 2019

Conversation

tbarlow12
Copy link
Contributor

@tbarlow12 tbarlow12 commented May 28, 2019

  • Adds CLI commands sls func add -n {functionName} and sls func remove -n {functionName}
  • Adds 1 plugin with 3 hooks: (func, func:add, func:remove)
  • Generates code directory and updates serverless.yml appropriately
  • For now, only supports http functions. Working on supporting additional triggers
  • Still working on tests, documentation and edge cases

@tbarlow12 tbarlow12 changed the title WIP - feat: Func plugin to add/remove plugins within function app feat: Func plugin to add/remove plugins within function app May 29, 2019
@tbarlow12 tbarlow12 requested review from wbreza, mydiemho and PIC123 May 29, 2019 17:36
src/index.ts Outdated
import { AzureLoginPlugin } from './plugins/login/loginPlugin';
import { AzureApimServicePlugin } from './plugins/apim/apimServicePlugin';
import { AzureApimFunctionPlugin } from './plugins/apim/apimFunctionPlugin';
import Serverless from "serverless";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use double quotes

Copy link
Contributor

@wbreza wbreza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some feedback. Let's discuss.

}

public static getFunctionHandler(name: string) {
return `"use strict";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this inline script into a separate template file. We can then use JS string interpolation (similar to vott resources) to include any dynamic string content.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I've spent some time trying to make this work. My problem has been what kind of file to put the template in. Here are the options I've tried:

Template file options:

  1. TypeScript file
    • Gets transpiled to JavaScript in build - doesn't keep template structure
  2. JavaScript file
    • Can't be included in build
    • Tried setting 'allowJs' to be true, but that conflicts with 'declarations' being true
  3. Text file
    • Could make it work, but it would mean an extra step post-build of copying the text file over. Not sure that's warranted just to avoid the inline script

Other options?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, the ideal solution would be to be able to include a js file of the handler since that's technically what we're packaging, but if that isn't really an option, then inline seems reasonable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a work item to re-review this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AB#18775]

src/plugins/func/remove/azureFuncRemove.ts Outdated Show resolved Hide resolved
src/plugins/func/azureFunc.ts Show resolved Hide resolved
src/plugins/func/funcUtils.ts Outdated Show resolved Hide resolved
src/plugins/func/funcUtils.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@mydiemho mydiemho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure where it should go and if it's out of scope of this pr, but we need some tests to enforce the right file structure for function zip folder.

README.md Show resolved Hide resolved
expect(sls.cli.log).toBeCalledWith("Need to provide a name of function to remove")
});

it("returns with pre-existing function", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy/pasta: should be returns with non-existing function

src/plugins/func/azureFunc.ts Show resolved Hide resolved

private createFunctionDir(name: string) {
this.serverless.cli.log("Creating function dir");
fs.mkdirSync(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we worry about error here? in cases of permission issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll put a try catch here


private static defaultFunctionSlsObject(name: string) {
return {
handler: `${name}/index.handler`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want ${name}/index.handler, this will generate incorrect binding and function won't work.

just index.handler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the structure that serverless.yml needs to find the handler since it will live inside the directory {functionName and inside the file index.js, with the function name handler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but the plugin will generate incorrect scriptFilePath since function.json now resides at the same level as index.json


public static createTestFunctionMetadata(name: string) {
return {
"handler": `${name}/index.handler`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above no ${name}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above :p

src/plugins/func/azureFunc.ts Show resolved Hide resolved

private static defaultFunctionSlsObject(name: string) {
return {
handler: `${name}/index.handler`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but the plugin will generate incorrect scriptFilePath since function.json now resides at the same level as index.json


public static createTestFunctionMetadata(name: string) {
return {
"handler": `${name}/index.handler`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above :p

Copy link
Contributor

@mydiemho mydiemho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! only have one question about a use of string interpolation.

README.md Show resolved Hide resolved

private static defaultFunctionSlsObject(name: string) {
return {
handler: `index.handler`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can change back to double quote since we're no longer interpolating a variable

const template = sls.utils.readFileSync(path);
const names = params.keys();
const vals = params.values();
return new Function(...names, `return \`${template}\`;`)(...vals);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: can

`return \`${template}\`;`

be replace with

`return ${template};`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a string interpolation that won't happen here. We actually have the same function (kind of) in VoTT. This is expecting a file with templated variables. I'm actually not using this right now... I was using it when I had a javascript template file. But I might keep it in just in case we go back to that

@tbarlow12 tbarlow12 changed the title feat: Func plugin to add/remove plugins within function app feat: Func plugin to add/remove functions within function app May 31, 2019
Copy link
Contributor

@PIC123 PIC123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a small question but looks good!

}

public static getFunctionHandler(name: string) {
return `"use strict";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, the ideal solution would be to be able to include a js file of the handler since that's technically what we're packaging, but if that isn't really an option, then inline seems reasonable.


it("returns with missing name", async () => {
const sls = MockFactory.createTestServerless();
const options = MockFactory.createTestServerlessOptions();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Would it make sense to add a default name to the options and then remove/override as needed? Just thinking if we'll need it for other function tests if we add more functionality, like the option to include the extension bundles

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. Let's add that in another PR

Copy link
Contributor

@wbreza wbreza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing earlier feedback.

}

public static getFunctionHandler(name: string) {
return `"use strict";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a work item to re-review this later.

@tbarlow12 tbarlow12 merged commit 3639b0f into dev May 31, 2019
@tbarlow12 tbarlow12 deleted the tabarlow/func-plugin branch June 7, 2019 17:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants