Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
refactor: add evnironment spec for fc
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Apr 14, 2020
1 parent acaa9d1 commit 284a014
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/serverless-spec-builder/src/fc/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class FCSpecBuilder extends SpecBuilder {
Timeout: funSpec.timeout || providerData.timeout || 30,
InitializationTimeout: funSpec.initTimeout || 3,
MemorySize: funSpec.memorySize || providerData.memorySize || 512,
EnvironmentVariables: {
...providerData.environment,
...funSpec.environment,
},
},
Events: {},
};
Expand Down
12 changes: 7 additions & 5 deletions packages/serverless-spec-builder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ export function getLayers(...layersList: any) {
}

export function uppercaseObjectKey(obj) {
const json = JSON.stringify(obj);
const result = json.replace(/"([^"])([^"]*)":/gim, (...value) => {
return `"${value[1].toUpperCase()}${value[2]}":`;
});
return JSON.parse(result);
if (obj) {
const json = JSON.stringify(obj);
const result = json.replace(/"([^"])([^"]*)":/gim, (...value) => {
return `"${value[1].toUpperCase()}${value[2]}":`;
});
return JSON.parse(result);
}
}
21 changes: 20 additions & 1 deletion packages/serverless-spec-builder/test/fc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('/test/fc.test.ts', () => {
assert(funResult['Properties']['Handler'] === 'index.handler');
assert(funResult['Properties']['Runtime'] === 'nodejs10');
assert.deepStrictEqual(funResult['Events'], {});
const properties = result['Resources']['serverless-hello-world']['Properties'];
const properties =
result['Resources']['serverless-hello-world']['Properties'];
assert(properties['VpcConfig']);
assert(properties['Policies']);
assert(properties['LogConfig']);
Expand Down Expand Up @@ -83,4 +84,22 @@ describe('/test/fc.test.ts', () => {
assert(funResult['Properties']['Runtime'] === 'nodejs10');
assert.deepStrictEqual(funResult['Events'], {});
});

it('test transform environment', () => {
const result = generateFunctionsSpec(
path.join(__dirname, './fixtures/fc/f-environment.yml')
);
const funResult = result['Resources']['serverless-hello-world']['index'];
assert(funResult['Type'] === 'Aliyun::Serverless::Function');
assert(funResult['Properties']['Handler'] === 'index.handler');
assert(funResult['Properties']['Runtime'] === 'nodejs10');
assert(funResult['Properties']['EnvironmentVariables']['GLOBAL_PASS']);
assert(!funResult['Properties']['EnvironmentVariables']['MYSQL_USER']);
assert(!funResult['Properties']['EnvironmentVariables']['MYSQL_PASS']);

const funResult2 = result['Resources']['serverless-hello-world']['index2'];
assert(funResult2['Properties']['EnvironmentVariables']['GLOBAL_PASS']);
assert(funResult2['Properties']['EnvironmentVariables']['MYSQL_USER']);
assert(funResult2['Properties']['EnvironmentVariables']['MYSQL_PASS']);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
service:
name: serverless-hello-world

provider:
name: aliyun
environment:
GLOBAL_PASS: admin

functions:
index:
handler: index.handler
index2:
handler: index2.handler
environment:
MYSQL_USER: root
MYSQL_PASS: pass

package:
artifact: code.zip

0 comments on commit 284a014

Please sign in to comment.