Skip to content

Variables js support - #2657

Merged
austencollins merged 10 commits into
masterfrom
variables-js-support
Nov 12, 2016
Merged

austencollins merged 10 commits into
masterfrom
variables-js-support

Conversation

@austencollins

@austencollins austencollins commented Nov 8, 2016 •

Copy link
Copy Markdown
Member

What did you implement:

Problem:
Serverless Variables are awesome, but they don't support generating dynamic data.

Use-cases:

  • Generating a random string to use in a serverless.yml value, every time Serverless is run.
  • I keep mock event data to test my functions with in serverless.yml. But, I need to generate random values in those mocks to make them more realistic and robust. With this, I can.
  • Do anything with Serverless Variables in Javascript(!!!)

How did you implement it:

Added a few lines of code (and moved existing code around which makes this change look larger than it is) to Variables.js to allow for dynamic data via javascript.

How can we verify it:

  • In serverless.yml, use this reference:
provider:
  stage: ${file(./confg.js):dynamicData}
  • Make a config.js in your service root, with this code:
module.exports.dynamicData = function() {
  // Synchronous logic is executed here
  return 'dev';
}

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config/commands/resources
  • Change ready for review message below

Is this ready for review?: YES

@eahefnawy

Copy link
Copy Markdown
Contributor

Looks great @ac360 .... simple yet great addition! G2M 😌

Comment thread docs/providers/aws/guide/variables.md Outdated
In the above example, you're referencing the entire `myCustomFile.yml` file in the `custom` property. You need to pass the path relative to your service directory. You can also request specific properties in that file as shown in the `schedule` property. It's completely recursive and you can go as deep as you want.

## Reference Variables in Javascript Files
To add dynamic data into your variables, reference javascript files by putting `${file(./myFile.js):someModule}` syntax in your `serverless.yml`. This functionality is recursive, so you can go as deep in that file as you want. Here's an example:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add or extend the current example to show that you can also reference sub properties, so you can return a hash in the js file and then reference that sub property (same way you did in the test). Other than that great change!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1 Fixed.

@flomotlik

Copy link
Copy Markdown
Contributor

@eahefnawy did you test/validate this on your system as well? Imho small docs change that we can do to show how powerful this is, but this should definitely go out with the next release, don't see anything stopping this being merged after a small docs update.

@pmuens

pmuens commented Nov 8, 2016 •

Copy link
Copy Markdown
Contributor

Damn! This is brilliant! ❤️

Tested it according to the steps in the description. Works as describes. Added just a few comments. After that it's GTM from my side. 👍

Comment thread lib/classes/Variables.js Outdated
` file "${referencedFileRelativePath}" sub properties`,
' Please use ":" or "." to reference sub properties.',
` file "${referencedFileRelativePath}".`,
' Check your javascript is returning the correct data.',

@pmuens pmuens Nov 8, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is an if missing here? --> Check if your JavaScript code is returning the correct data.

Comment thread lib/classes/Variables.js Outdated
const errorMessage = [
'Invalid variable syntax when referencing',
` file "${referencedFileRelativePath}" sub properties`,
' Please use ":" or "." to reference sub properties.',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this error message accurate? Because at the top there's a an if which only checks for :.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed

@nicka

nicka commented Nov 8, 2016

Copy link
Copy Markdown
Contributor

This is awesome! Could we also have access to serverless.options like stage, provider or custom within the custom functions?

@flomotlik

flomotlik commented Nov 8, 2016 •

Copy link
Copy Markdown
Contributor

@nicka in that case you should be writing a plugin. With the new .serverless_plugins folder thats also essentially just a small js file. This should be super simple and just let you do some simple calculations to get some data.

@nicka

nicka commented Nov 8, 2016 •

Copy link
Copy Markdown
Contributor

@flo This should be super simple and just let you do some simple calculations to get some data.

Not sure if I completely agree on this. Even in the provided example: You need some params/arguments to even reach to a return value of dev. Unless someone is going to add ENV variables or do 3rd party calls, I don't see how you would come to a calculation.

Don't get me wrong! I really like this feature and I see it's potential 👍

@flomotlik

Copy link
Copy Markdown
Contributor

Totally can see your point there, but for this case users really should switch to a simple plugin. This is imho really for very simple things like calculated hash value, expanding a template filename to the actual filename, expanding paths, ... so things that are really simple.

We might extend it in the future to do more, but imho it would overlap a lot with the plugin system here and I don't want to duplicate functionality until we really and fully understand what the need is (which at this point at least I'm not 100% certain I have a complete grasp on how what you're proposing would be used and in which situations)

@austencollins

austencollins commented Nov 8, 2016 •

Copy link
Copy Markdown
Member Author

@nicka @flo I thought about what could be passed in, but wanted to think more about this and get feedback before implementation.

At the moment, I'm not opposed to overlapping with the Plugin system. If the serverless instance was passed in to these Serverless Variables scripts, I think it could be a win for convenience and productivity. Lots of smaller things don't need commands, lifecycleEvents, and the cognitive overhead of plugin development. Further, this is an easy way to build customization that consistently covers all commands/events, which is harder w/ Plugins. With these scripts, you'll always know that serverless.yml contains the same data, no mater what you're doing.

Here are some potential, powerful use-cases:

  • Loading data from a remote source (e.g., KMS).
  • Allowing people to pull/process data from their CF stack and return anything they want.
  • Allowing people to pull/process data from options, env vars, etc.

I think we should merge this PR without passing anything in, and open another discussion issue for what this could become.

@flomotlik

Copy link
Copy Markdown
Contributor

@ac360 agreed. The main concern I have is that we pass something into the variable system and it then starts changing other parts of the config. So imho anything we pass in should be read-only and only help to determine a specific value. And if that isn't easy enough to accomplish through the plugin system then we should make the plugin system simpler.

@nicka

nicka commented Nov 8, 2016

Copy link
Copy Markdown
Contributor

@flo So imho anything we pass in should be read-only and only help to determine a specific value.

Agreed, this feature should not act as a modifier of the original serverless instance, but as a helper to calculate possible outcomes.

@austencollins
austencollins merged commit 3320647 into master Nov 12, 2016
@pmuens
pmuens deleted the variables-js-support branch November 12, 2016 10:37
@eahefnawy eahefnawy added this to the 1.2 milestone Nov 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants