Repository: aliyun/fun
Pull request #69 (2018-07-28 05:51:42)

fix sls trigger 
fix sls trigger
add sls trigger demo
update specs of sls trigger 


Patch:
diff --git a/docs/specs/2018-04-03-zh-cn.md b/docs/specs/2018-04-03-zh-cn.md
index 5cb9fa4..6d1b3fd 100644
--- a/docs/specs/2018-04-03-zh-cn.md
+++ b/docs/specs/2018-04-03-zh-cn.md
@@ -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 事件源对象
 
@@ -247,7 +248,7 @@ Properties:
     LogConfig:
         Project: testlog
         Logstore: logstore2
-        Enable: true
+    Enable: true
 ```
 
 ### Property 类型
diff --git a/docs/specs/2018-04-03.md b/docs/specs/2018-04-03.md
index a690f16..9da23d5 100644
--- a/docs/specs/2018-04-03.md
+++ b/docs/specs/2018-04-03.md
@@ -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
 
@@ -245,7 +246,7 @@ Properties:
     LogConfig:
         Project: testlog
         Logstore: logstore2
-        Enable: true
+    Enable: true
 ```
 
 ### Property types
diff --git a/examples/sls_demo/template.yml b/examples/sls_demo/template.yml
index d85b0b3..f252849 100644
--- a/examples/sls_demo/template.yml
+++ b/examples/sls_demo/template.yml
@@ -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'
@@ -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:
diff --git a/examples/sls_trigger_demo/index.js b/examples/sls_trigger_demo/index.js
new file mode 100644
index 0000000..675f9e2
--- /dev/null
+++ b/examples/sls_trigger_demo/index.js
@@ -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);
+};
\ No newline at end of file
diff --git a/examples/sls_trigger_demo/template.yml b/examples/sls_trigger_demo/template.yml
new file mode 100644
index 0000000..b0f29cf
--- /dev/null
+++ b/examples/sls_trigger_demo/template.yml
@@ -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}/
+
+
+
diff --git a/examples/sls_trigger_demo/trigger.js b/examples/sls_trigger_demo/trigger.js
new file mode 100644
index 0000000..c1992cf
--- /dev/null
+++ b/examples/sls_trigger_demo/trigger.js
@@ -0,0 +1,7 @@
+'use strict';
+
+exports.handler = function(event, context, callback) {
+
+  console.log("triggerd by sls trigger...");
+  callback(null, "trigged...");
+};
\ No newline at end of file
diff --git a/lib/deploy/deploy-support.js b/lib/deploy/deploy-support.js
index 7a61dab..0935fe1 100644
--- a/lib/deploy/deploy-support.js
+++ b/lib/deploy/deploy-support.js
@@ -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.`);
diff --git a/lib/validate/schema/log-event.js b/lib/validate/schema/log-event.js
index ab2044e..11909cd 100644
--- a/lib/validate/schema/log-event.js
+++ b/lib/validate/schema/log-event.js
@@ -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']
     }
diff --git a/package-lock.json b/package-lock.json
index 4ca27ac..4bf1115 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "@alicloud/fun",
-  "version": "2.2.0",
+  "version": "2.2.1",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index e7bfccc..5f910ae 100644
--- a/package.json
+++ b/package.json
@@ -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"


Test patch:
diff --git a/test/deploy/deploy-by-tpl.test.js b/test/deploy/deploy-by-tpl.test.js
index 516ee00..c952082 100644
--- a/test/deploy/deploy-by-tpl.test.js
+++ b/test/deploy/deploy-by-tpl.test.js
@@ -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');