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

Commit

Permalink
fix: add fc service properties definition
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Apr 13, 2020
1 parent ef732e0 commit 3cbdfe6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
17 changes: 9 additions & 8 deletions packages/serverless-spec-builder/src/fc/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ import {
FCFunctionStructure,
FCSpec,
HTTPEventType,
FCProviderStructure,
} from './interface';
import { SpecBuilder } from '../builder';
import {
ProviderStructure,
HTTPEvent,
ScheduleEvent,
LogEvent,
OSEvent,
} from '../interface';
import { HTTPEvent, ScheduleEvent, LogEvent, OSEvent } from '../interface';
import { uppercaseObjectKey } from '../utils';

export class FCSpecBuilder extends SpecBuilder {
toJSON() {
const providerData: ProviderStructure = this.getProvider();
const providerData: FCProviderStructure = this.getProvider();
const serviceData = this.getService();
const functionsData: FCFunctionsStructure = this.getFunctions();
const serviceName = serviceData.name;
Expand All @@ -31,6 +27,11 @@ export class FCSpecBuilder extends SpecBuilder {
Properties: {
Description: serviceData.description,
Role: providerData.role,
InternetAccess: providerData.internetAccess,
VpcConfig: uppercaseObjectKey(providerData.vpcConfig),
Policies: uppercaseObjectKey(providerData.policies),
LogConfig: uppercaseObjectKey(providerData.logConfig),
NasConfig: uppercaseObjectKey(providerData.nasConfig),
},
},
},
Expand Down
35 changes: 34 additions & 1 deletion packages/serverless-spec-builder/src/fc/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { FunctionsStructure, FunctionStructure } from '../interface';
import {
FunctionsStructure,
FunctionStructure,
ProviderStructure,
SpecStructure,
} from '../interface';

export interface FCSpec {
ROSTemplateFormatVersion: string;
Expand All @@ -14,6 +19,34 @@ export type FCCustomDomainType = 'Aliyun::Serverless::CustomDomain';
export type FCLogType = 'Aliyun::Serverless::Log';
export type FCLogStoreType = 'Aliyun::Serverless::Log::Logstore';

export interface FCProviderStructure extends ProviderStructure {
vpcConfig?: {
vpcId: string;
vSwitchIds: string[];
securityGroupId: string;
};
internetAccess?: boolean;
policies?: string | string[];
logConfig?: {
project: string;
logstore: string;
};
nasConfig?:
| 'auto'
| {
userId: number;
groupId: number;
mountPoints: Array<{
serverAddr: string;
mountDir: string;
}>;
};
}

export interface FCSpecStructure extends SpecStructure {
provider?: FCProviderStructure;
}

export interface FCServiceProperties {
Role?: string;
Policies?: string | string[];
Expand Down
10 changes: 9 additions & 1 deletion packages/serverless-spec-builder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface Ilayer {
export function formatLayers(...multiLayers: Ilayer[]) {
const layerTypeList = { npm: {} };
multiLayers.forEach((layer: Ilayer) => {
Object.keys(layer || {}).forEach(layerName => {
Object.keys(layer || {}).forEach((layerName) => {
const [type, path] = layer[layerName].path.split(':');
if (!layerTypeList[type]) {
return;
Expand Down Expand Up @@ -34,3 +34,11 @@ export function getLayers(...layersList: any) {
layers,
};
}

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);
}
17 changes: 17 additions & 0 deletions packages/serverless-spec-builder/test/fc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ describe('/test/fc.test.ts', () => {
assert.deepStrictEqual(funResult['Events'], {});
});

it('test transform service properties', () => {
const result = generateFunctionsSpec(
path.join(__dirname, './fixtures/fc/f-service-properties.yml')
);
const funResult = result['Resources']['serverless-hello-world']['index'];
assert(funResult['Type'] === 'Aliyun::Serverless::Function');
assert(funResult['Properties']['Initializer'] === 'index.initializer');
assert(funResult['Properties']['Handler'] === 'index.handler');
assert(funResult['Properties']['Runtime'] === 'nodejs10');
assert.deepStrictEqual(funResult['Events'], {});
const properties = result['Resources']['serverless-hello-world']['Properties'];
assert(properties['VpcConfig']);
assert(properties['Policies']);
assert(properties['LogConfig']);
assert(properties['NasConfig']);
});

it('test http events', () => {
const result = generateFunctionsSpec(
path.join(__dirname, './fixtures/fc/f-event-http.yml')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
service:
name: serverless-hello-world

provider:
name: aliyun
policies:
- AliyunECSNetworkInterfaceManagementAccess
vpcConfig:
vpcId: 'vpc-j6cfu2g6tslzekh8grfmk'
vSwitchIds: [ 'vsw-j6chkgsg9naj6gx49espd' ]
securityGroupId: 'sg-j6ceitqs6ljyssm1apom'
logConfig:
project: localtestlog
logstore: localteststore
nasConfig:
userId: 10003
groupId: 10003
mountPoints:
- serverAddr: '012194b28f-xxxxx.cn-hangzhou.nas.aliyuncs.com:/'
mountDir: '/mnt/test'

functions:
index:
handler: index.handler

package:
artifact: code.zip
23 changes: 23 additions & 0 deletions packages/serverless-spec-builder/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { uppercaseObjectKey } from '../src/utils';
import { deepStrictEqual } from 'assert';

describe('/test/utils.test.ts', () => {
it('test unppercase', () => {
const json = {
a: 1,
bbbb: 2,
ddd: [
'ccc',
{
fff: ['eee'],
},
],
};
const result = uppercaseObjectKey(json);
deepStrictEqual(result, {
A: 1,
Bbbb: 2,
Ddd: ['ccc', { Fff: ['eee'] }],
});
});
});

0 comments on commit 3cbdfe6

Please sign in to comment.