Skip to content

Commit

Permalink
fix sls trigger not enabled bug (#69)
Browse files Browse the repository at this point in the history
* fix log trigger not enable bug
* add sls trigger demo
* add sls trigger test
* update related documents
  • Loading branch information
tanhe123 committed Jul 28, 2018
1 parent d46eb20 commit 0bf8802
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 22 deletions.
5 changes: 3 additions & 2 deletions docs/specs/2018-04-03-zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ http-trigger-test: # trigger name
---|:---:|---
SourceConfig | `object` | **必填。** 数据源的 Logstore 名称。触发器会定时从该 Logstore 订阅数据到函数计算。
JobConfig | `object` | **必填。** 包含两个可配置属性,一个是 MaxRetryTime,表示日志服务触发函数执行时,如果遇到错误,所允许的最大尝试次数。另一个是 TriggerInterval,表示日志服务触发函数执行的间隔。
LogConfig | `object` | **必填。** 包含四个可配置属性。第一个是 Project,表示日志服务 Project 名称。第二个是 Logstore,表示触发函数执行时,产生的日志会记录到该 Logstore。第三个是 Enable,表示是否启用该触发器。第四个是可选属性 FunctionParameter,它本身也是一个 object,当事件触发时,会连同它的内容一起发送给函数。
LogConfig | `object` | **必填。** 包含三个可配置属性。第一个是 Project,表示日志服务 Project 名称。第二个是 Logstore,表示触发函数执行时,产生的日志会记录到该 Logstore。第三个是可选属性 FunctionParameter,它本身也是一个 object,当事件触发时,会连同它的内容一起发送给函数。
Enable | `boolean` | 表示是否启用该触发器。

##### 示例:Log 事件源对象

Expand All @@ -247,7 +248,7 @@ Properties:
LogConfig:
Project: testlog
Logstore: logstore2
Enable: true
Enable: true
```

### Property 类型
Expand Down
5 changes: 3 additions & 2 deletions docs/specs/2018-04-03.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ Property Name | Type | Description
SourceConfig | `object` | **Required.** The datasource's Logstore name. Triggers periodically subscribe to data from the Logstore to Function compute.

JobConfig | `object` | **Required.** There are two configurable properties, one of which is MaxRetryTime, which represents the maximum number of attempts allowed if the logging service triggers a function execution. The other is TriggerInterval, which indicates the interval at which the logging service triggers the execution of the function.
LogConfig | `object` | **Required.** There are four configurable properties. The first is Project, which represents the log service Project name. The second is the Logstore, which means that when the trigger function is executed, the resulting log will be logged to the Logstore. The third is Enable, which indicates whether the trigger is enabled. The fourth is an optional property, FunctionParameter, which is itself an object that, when the event fires, is sent to the function along with its contents.
LogConfig | `object` | **Required.** There are three configurable properties. The first is Project, which represents the log service Project name. The second is the Logstore, which means that when the trigger function is executed, the resulting log will be logged to the Logstore. The third is an optional property, FunctionParameter, which is itself an object that, when the event fires, is sent to the function along with its contents.
Enable | `boolean` | Indicates whether the trigger is enabled.

##### Example: Log event source object

Expand All @@ -245,7 +246,7 @@ Properties:
LogConfig:
Project: testlog
Logstore: logstore2
Enable: true
Enable: true
```

### Property types
Expand Down
15 changes: 7 additions & 8 deletions examples/sls_demo/template.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
log-compute1:
log-compute:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'sls test'
Expand All @@ -17,15 +17,14 @@ Resources:
Type: Log
Properties:
SourceConfig:
Logstore: log-com-m
Logstore: log-com-m
JobConfig:
MaxRetryTime: 1
TriggerInterval: 30
MaxRetryTime: 1
TriggerInterval: 30
LogConfig:
Project: log-com-m
Logstore: log-en-m
Enable: true

Project: log-com-m
Logstore: log-en-m
Enable: true
log-com-m:
Type: 'Aliyun::Serverless::Log'
Properties:
Expand Down
15 changes: 15 additions & 0 deletions examples/sls_trigger_demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports.handler = function(event, context, callback) {
var response = {
isBase64Encoded:false,
statusCode: 200,
headers: {
"x-custom-header" : "header value"
},
body: "hellowrold"
};

console.log("response: " + JSON.stringify(response));
callback(null, response);
};
77 changes: 77 additions & 0 deletions examples/sls_trigger_demo/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
helloworld:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'sls test'
LogConfig:
Project: sls-log-test
Logstore: log-trigger
helloworld:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs8
CodeUri: './index.js'

fc:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'sls test'
LogConfig:
Project: sls-log-test
Logstore: log
slstrigger:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: trigger.handler
Runtime: nodejs8
CodeUri: './trigger.js'
Events:
log-stream:
Type: Log
Properties:
SourceConfig:
Logstore: log
JobConfig:
MaxRetryTime: 1
TriggerInterval: 30
LogConfig:
Project: sls-log-test
Logstore: sls-log
Enable: true

sls-log-test:
Type: 'Aliyun::Serverless::Log'
Properties:
Description: 'just python log'
log-trigger:
Type: 'Aliyun::Serverless::Log::Logstore'
Properties:
TTL: 10
ShardCount: 1
sls-log:
Type: 'Aliyun::Serverless::Log::Logstore'
Properties:
TTL: 10
ShardCount: 1
log:
Type: 'Aliyun::Serverless::Log::Logstore'
Properties:
TTL: 10
ShardCount: 1

hello:
Type: 'Aliyun::Serverless::Api'
Properties:
StageName: RELEASE
DefinitionBody:
'/hello':
get:
x-aliyun-apigateway-api-name: pythonhello
x-aliyun-apigateway-fc:
arn: acs:fc:::services/${helloworld.Arn}/functions/${helloworld.Arn}/



7 changes: 7 additions & 0 deletions examples/sls_trigger_demo/trigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

exports.handler = function(event, context, callback) {

console.log("triggerd by sls trigger...");
callback(null, "trigged...");
};
6 changes: 3 additions & 3 deletions lib/deploy/deploy-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ function getTriggerConfig(triggerType, triggerProperties) {
logConfig: {
project: logConfig.Project,
logstore: logConfig.Logstore,
functionParameter: logConfig.FunctionParameter || {},
enable: logConfig.Enable
}
functionParameter: logConfig.FunctionParameter || {}
},
Enable: triggerProperties.Enable || true
};
}
console.error(`trigger type is ${triggerType} not supported.`);
Expand Down
8 changes: 3 additions & 5 deletions lib/validate/schema/log-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ const logEventSchema = {
'Logstore': {
'type': 'string'
},
'Enable': {
'type': 'boolean'
},
'FunctionParameter': {
'type': 'object'
}
},
'required': ['Project', 'Logstore', 'Enable']
}
'required': ['Project', 'Logstore']
},
'Enabled': { 'type': 'boolean' }
},
'required': ['SourceConfig', 'JobConfig', 'LogConfig']
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alicloud/fun",
"version": "2.2.0",
"version": "2.2.1",
"description": "(have)Fun with Serverless",
"engines": {
"node": ">=8.0.0"
Expand Down
36 changes: 36 additions & 0 deletions test/deploy/deploy-by-tpl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,42 @@ describe('deploy', () => {
});
});

it('deploy sls_demo', async () => {
await deploy('sls_demo');

assert.calledWith(deploySupport.makeService, {
description: 'sls test',
internetAccess: null,
logConfig: { },
role: `acs:ram::123:role/aliyunfcgeneratedrole-fc`,
serviceName: 'log-compute',
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction, {
codeUri: './',
handler: 'index.handler',
description: undefined,
functionName: 'log-compute',
memorySize: undefined,
runtime: 'python2.7',
serviceName: 'log-compute',
timeout: undefined,
environmentVariables: undefined
});
assert.calledWith(deploySupport.makeTrigger, {
serviceName: 'log-compute',
functionName: 'log-compute',
triggerName: 'log-stream',
triggerType: 'Log',
triggerProperties: {
Enable: true,
JobConfig: { MaxRetryTime: 1, TriggerInterval: 30 },
LogConfig: { Logstore: 'log-en-m', Project: 'log-com-m' },
SourceConfig: { Logstore: 'log-com-m' }
},
});
});

it('deploy python', async () => {
await deploy('python');

Expand Down

0 comments on commit 0bf8802

Please sign in to comment.