Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anantab committed Jul 5, 2018
0 parents commit 6b96263
Show file tree
Hide file tree
Showing 8 changed files with 5,995 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": [
"eslint:recommended"
],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017
},
"rules": {
"indent": [
"error",
4,
{
"SwitchCase": 1
}
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double",
"avoid-escape"
],
"semi": [
"error",
"always"
]
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
branches:
only:
- master
language: node_js
node_js:
- "6"
- "8"
- "9"
script:
- npm test
- npm run eslint
114 changes: 114 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Serverless Plugin IfElse
While you can use serverless variables to define different values for your atrributes based on either stage or other properties, it sometimes is not as straightforward.

For example, If you have a serverless project with 3 functions and you want to deploy all 3 functions in one region but only 2 of them in other region, then there is no easier way to exlude the third function based on region you are deploying.

Another use case that inspired me to write this plugin was, I wanted to use ```iamRoleStatements``` for all my Lambda functions in staging but use a pre-define ```role``` in production. You cannot have both attributes in serverless.yml file as serverless ignores ```iamRoleStatements``` if there is ```role``` attribute.

This plugin allows you to write if else conditions in ```serverless.yml``` file to remove or change the values of attributes in the yml file. It works with both ```package``` and ```deploy``` commands. It also works with ```serverless-offline``` plugin.


## Example
serverless.yml
```
service: serverlessIfElseExample
custom:
currentStage: ${opt:stage, self:provider.stage}
serverlessIfElse:
- If: '"${self:custom.currentStage}" == "dev"'
Exclude:
- provider.role
- provider.environment.ENV1
- functions.func3
Set:
provider.timeout: 90
provider.profile: dev
ElseExclude:
- provider.environment.ENV2
ElseSet:
provider.timeout: 120
- ExcludeIf:
functions.func1: '"${self:provider.region}" == "ap-southeast-2"'
functions.func2: '"${opt:region}" == "us-east-1"'
- If: '"${self:provider.region}" == "us-east-1"'
Exclude:
- functions.func1
Set:
provider.timeout: 300
plugins:
- serverless-plugin-ifelse
provider:
name: aws
runtime: nodejs8.10
stage: dev
region : ap-southeast-2
timeout: 60
environment:
ENV1: env-val-1
ENV2: env-val-2
profile: default
iamRoleStatements:
- Effect: Allow
Action:
- s3:*
Resource: "*"
role: arn:aws:iam::xxxxxxxxxxxx:role/testRole
functions:
func1:
name: Function 1
handler: lambda.func1
events:
- http:
path: "path1",
method: "post",
func2:
name: Function 2
handler: lambda.func2
events:
- http:
path: "path2",
method: "post",
func3:
name: Function 3
handler: lambda.func2
events:
- http:
path: "path3",
method: "post"
```

Put your conditions in custom variable ```serverlessIfElse```.

## - If
Write your if condition inside single quote ```''```. Inside your condition, all your variables that resolve in string or the string themselves should be inside double quote. The plugin will otherwise encounter undefined variable error and the condition will not work. The condition can be anything, like == , !=, <, > or even javascript regular expressions.

```- If: '"${self:provider.stage}" == "dev"'```

### Exclude
If condition in If is true, all attibutes in Exclude will be ignored before serverless package or deploy your stack and hence serverless will not see those attributes.

### Set
If condition in If is true, the value of the attribute will be updated with new value.

### ElseExclude
If condition in If is false,the attibutes will be ignored.

### ElseSet
If condition in If is false, the value of the attribute will be updated with new value.

## - ExcludeIf
Use ExcludeIf, if you want to write conditions per attribute. If condition is true, only that attribute will be ignored.

You can write as many conditions as you like and exclude or set attributes any level deep in the yml file.

## Note
This plugin will ignore or update value of attributes based on your conditions and does not evaluate if it causes any side effects. You are responsbile to make sure ignoring or setting new values will work as you expected and will not cause serverless to throw error.

The plugin will not remove or update any first level attributes in serverless.yml file like ```service``` or ```provider``` or ```functions```.
197 changes: 197 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
const serverlessPluginIfElse = require("../index");
let serverless = {};

describe("Test Serverless IfElse Plugin With Condition Set 1", () => {
beforeAll(() => {
const condition = getConditions("condition1");
serverless = getServerless();
serverless.service.custom.serverlessIfElse = condition;
const serverlessIfElse = new serverlessPluginIfElse(serverless);
serverlessIfElse.applyConditions();
});

it("It Should Remove Serverless Properties in Exlude when If condition Matches", () => {
expect(serverless.service.functions.func1).toBeUndefined();
expect(serverless.service.functions.role).toBeUndefined();
});

it("It Should Set Serverless Properties in Set When If condition Matches", () => {
expect(serverless.service.provider.profile).toBe("dev");
});

it("It Should Not Remove Serverless Properties in ElseExclude condition when If condition Matches", () => {
expect(serverless.service.functions.func2).toBeDefined();
expect(serverless.service.functions.func2.name).toBe("Function 2");
expect(serverless.service.provider.iamRoleStatements).toBeDefined();
});

it("It Should Not Set Serverless Properties in ElseSet when If condition Matches", () => {
expect(serverless.service.provider.timeout).toBe(300);
});

it("It Should Not Remove Serverless Properties in ExcludeIf when condition Does not Match", () => {
expect(serverless.service.functions.func3).toBeDefined();
expect(serverless.service.functions.func3.name).toBe("Function 3");
});
});


describe("Test Serverless IfElse Plugin With Condition Set 2", () => {
beforeAll(() => {
const condition = getConditions("condition2");
serverless = getServerless();
serverless.service.custom.serverlessIfElse = condition;
const serverlessIfElse = new serverlessPluginIfElse(serverless);
serverlessIfElse.applyConditions();
});

it("It Should Not Remove Serverless Properties in Exlude when If condition Does Not Match", () => {
expect(serverless.service.functions.func1).toBeDefined();
expect(serverless.service.functions.func1.name).toBe("Function 1");
});

it("It Should Not Set Serverless Properties in Set When If condition Does Not Match", () => {
expect(serverless.service.provider.profile).toBe("default");
});

it("It Should Remove Serverless Properties in ElseExclude when If condition Does Not Match", () => {
expect(serverless.service.functions.func2).toBeUndefined();
expect(serverless.service.provider.iamRoleStatements).toBeUndefined();
});

it("It Should Set Serverless Properties in ElseSet when If condition Does Not Match", () => {
expect(serverless.service.provider.timeout).toBe(90);
});

it("It Should Remove Serverless Properties in ExcludeIf when condition Matches", () => {
expect(serverless.service.functions.func3).toBeUndefined();
});
});

const getServerless = function () {
return {
service: {
service: "Serverless Condition",
custom: {
serverlessExclude: []
},
provider: {
name: "aws",
runtime: "nodejs6.10",
timeout: 300,
stage: "dev",
profile: "default",
role: "arn:aws:iam::xxxxxxxxxxxx:role/Test",
iamRoleStatements: [{
Effect: "Allow",
Action: ["s3:*"],
Resource: "*"
}]
},
functions: {
func1:
{
name: "Function 1",
handler: "func1.handler",
events: [{
http: {
path: "path1",
method: "post",
private: true,
cors: true
}
}
],
},
func2:
{
name: "Function 2",
handler: "func2.handler",
events: [{
http: {
path: "path2",
method: "post",
private: true,
cors: true
}
}
],
},
func3:
{
name: "Function 3",
handler: "func3.handler",
events: [{
http: {
path: "path3",
method: "post",
private: true,
cors: true
}
}
],
}
}
},
cli: {
log: jest.fn()
}
};
};

const getConditions = function (condition) {
const conditions = {
condition1: [
{
If: '"true"=="true"',
Exclude: [
"functions.func1",
"provider.role"
],
Set: {
"provider.profile": "dev",
},
ElseExclude: [
"functions.func2",
"provider.iamRoleStatements"
],
ElseSet: {
"provider.stage": "production",
"provider.timeout": 90
}
},
{
ExcludeIf:
{
"functions.func3": '"true" == "false"',
}
}
],
condition2: [
{
If: '"true"=="false"',
Exclude: [
"functions.func1",
],
Set: {
"provider.profile": "production",
"provider.timeout": 60
},
ElseExclude: [
"functions.func2",
"provider.iamRoleStatements"
],
ElseSet: {
"provider.timeout": 90
}
},
{
ExcludeIf:
{
"functions.func3": '"true" == "true"',
}
}
]
};
return conditions[condition];
};
Loading

0 comments on commit 6b96263

Please sign in to comment.