Repository: aliyun/fun
Pull request #205 (2019-04-29 07:30:43)

Sunfeiyu/cdn trigger event handle
#189  1. cdn handle 处理函数
          2. 修改 yml中描述触发器的属性  由小写变大写
          3. 增加文章 cdn 触发器



Patch:
diff --git a/docs/specs/2018-04-03-zh-cn.md b/docs/specs/2018-04-03-zh-cn.md
index 68830fd..89e7804 100644
--- a/docs/specs/2018-04-03-zh-cn.md
+++ b/docs/specs/2018-04-03-zh-cn.md
@@ -433,10 +433,10 @@ tablestore-trigger-test: # trigger name
 
 属性名称 | 类型 | 描述
 ---|:---:|---
-events| `array` | **必填。** 为 OSS 端触发函数执行的事件，比如参数为 ["oss:ObjectCreated:PutObject", "oss:ObjectCreated:PutSymlink"] 等...
-bucketName| `stirng` | **必填。** 为 OSS 中对应的 bucket 名称。可参照[OSS 示例](https://help.aliyun.com/document_detail/74762.html)
-filter   | `object` | **必填。** 为 OSS 对象过滤参数，满足过滤条件的 OSS 对象才可以触发函数，包含一个配置属性 key，表示过滤器支持过滤的对象键 (key)。 
-key | [OSS Key 对象](#OSS-Key-配置对象) | **必填。** 过滤器支持过滤的对象键
+Events| `array` | **必填。** 为 OSS 端触发函数执行的事件，比如参数为 ["oss:ObjectCreated:PutObject", "oss:ObjectCreated:PutSymlink"] 等...
+BucketName| `stirng` | **必填。** 为 OSS 中对应的 bucket 名称。可参照[OSS 示例](https://help.aliyun.com/document_detail/74762.html)
+Filter   | `object` | **必填。** 为 OSS 对象过滤参数，满足过滤条件的 OSS 对象才可以触发函数，包含一个配置属性 key，表示过滤器支持过滤的对象键 (key)。 
+Key | [OSS Key 对象](#OSS-Key-配置对象) | **必填。** 过滤器支持过滤的对象键
 
 ##### 示例：OSS 事件源对象
 
@@ -444,14 +444,14 @@ key | [OSS Key 对象](#OSS-Key-配置对象) | **必填。** 过滤器支持过
 oss-trigger-test: # trigger name
     Type: OSS # trigger type
      Properties:
-      bucketName: ossBucketName # oss bucket name
-      events:
+      BucketName: ossBucketName # oss bucket name
+      Events:
         - oss:ObjectCreated:*
         - oss:ObjectRemoved:DeleteObject
-      filter: 
-        key:
-          prefix: source/
-          suffix: .png
+      Filter: 
+        Key:
+          Prefix: source/
+          Suffix: .png
 ```
 
 #### CDN
@@ -462,10 +462,10 @@ oss-trigger-test: # trigger name
 
 属性名称 | 类型 | 描述
 ---|:---:|---
-eventName| `stirng` | **必填。** 为 CDN 端触发函数执行的事件，一经创建不能更改
-eventVersion| `stirng` | **必填。** 为 CDN 端触发函数执行事件的版本，一经创建不能更改
-notes| `stirng` | **必填。** 备注信息
-filter   | [CDN Filter 对象](#CDN-Filter-配置对象) | **必填。** 过滤器（至少需要一个过滤器）
+EventName| `stirng` | **必填。** 为 CDN 端触发函数执行的事件，一经创建不能更改
+EventVersion| `stirng` | **必填。** 为 CDN 端触发函数执行事件的版本，一经创建不能更改
+Notes| `stirng` | **必填。** 备注信息
+Filter   | [CDN Filter 对象](#CDN-Filter-配置对象) | **必填。** 过滤器（至少需要一个过滤器）
 
 ##### 示例：CDN 事件源对象
 
@@ -473,11 +473,11 @@ filter   | [CDN Filter 对象](#CDN-Filter-配置对象) | **必填。** 过滤
 cdn-trigger-name: # trigger name
     Type: CDN # trigger type
     Properties:
-      eventName: LogFileCreated
-      eventVersion: '1.0.0'
-      notes: cdn events trigger test
-      filter: 
-        domain: ['www.taobao.com','www.tmall.com']
+      EventName: LogFileCreated
+      EventVersion: '1.0.0'
+      Notes: cdn events trigger test
+      Filter: 
+        Domain: ['www.taobao.com','www.tmall.com']
 ```
 
 ### Property 类型
@@ -630,25 +630,25 @@ PathConfig 用于设置自定义域名中路径与函数的映射关系。
 
 #### OSS Key 配置对象
 
-key 配置对象的属性包括： `prefix` 和 `suffix` 属性。它们所代表的含义分别为：匹配前缀和匹配后缀。
+Key 配置对象的属性包括： `Prefix` 和 `Suffix` 属性。它们所代表的含义分别为：匹配前缀和匹配后缀。
 
 示例：
 
 ```
-key:
-  prefix: source/
-  suffix: .png
+Key:
+  Prefix: source/
+  Suffix: .png
 
 ```
 
 #### CDN Filter 配置对象
 
-filter 配置对象为 CDN 过滤参数，满足过滤条件的才可以触发函数。filter 对象可配置的属性目前只包括：`domain`, domain 是个字符串数组且必填，代表过滤参数值的集合。
+Filter 配置对象为 CDN 过滤参数，满足过滤条件的才可以触发函数。Filter 对象可配置的属性目前只包括：`Domain`, Domain 是个字符串数组且必填，代表过滤参数值的集合。
 
 示例：
 
 ```
-filter: 
-  domain: ['www.taobao.com','www.tmall.com']
+Filter: 
+  Domain: ['www.taobao.com','www.tmall.com']
 
 ```
\ No newline at end of file
diff --git a/docs/specs/2018-04-03.md b/docs/specs/2018-04-03.md
index 7cde60b..bd29742 100644
--- a/docs/specs/2018-04-03.md
+++ b/docs/specs/2018-04-03.md
@@ -406,10 +406,10 @@ Describe the object of type [OSS Trigger](https://help.aliyun.com/document_detai
 
 Property Name | Type | Description
 ---|:---:|---
-events | `array` | **Required.** Indicates the type of an OSS event.
-bucketName | `string` | **Required.** the corresponding bucket name in OSS. can refer to [OSS example](https://www.alibabacloud.com/help/doc-detail/31885.html).
-filter | `object` | **Required.** Filters OSS events. Only OSS objects that meet the filtering condition can trigger a function. This parameter has a key attribute:
-key | [`OSS Key configuration object`](#OSS-Key-configuration-object) | **Required** Indicates the key supported by the filter.
+Events | `array` | **Required.** Indicates the type of an OSS event.
+BucketName | `string` | **Required.** the corresponding bucket name in OSS. can refer to [OSS example](https://www.alibabacloud.com/help/doc-detail/31885.html).
+Filter | `object` | **Required.** Filters OSS events. Only OSS objects that meet the filtering condition can trigger a function. This parameter has a key attribute:
+Key | [`OSS Key configuration object`](#OSS-Key-configuration-object) | **Required** Indicates the key supported by the filter.
 
 ##### Example: OSS event source object
 
@@ -417,14 +417,14 @@ key | [`OSS Key configuration object`](#OSS-Key-configuration-object) | **Requir
 oss-trigger-test: # trigger name
     Type: OSS # trigger type
      Properties:
-      bucketName: ossBucketName # oss bucket name     
-      events:
+      BucketName: ossBucketName # oss bucket name     
+      Events:
         - oss:ObjectCreated:*
         - oss:ObjectRemoved:DeleteObject
-      filter: 
-        key:
-          prefix: source/
-          suffix: .png
+      Filter: 
+        Key:
+          Prefix: source/
+          Suffix: .png
 ```
 
 #### CDN
@@ -435,10 +435,10 @@ Describe the object of type [CDN Trigger](https://www.alibabacloud.com/help/doc-
 
 Property Name | Type | Description
 ---|:---:|---
-eventName | `string` | **Required.** is CDN event which invokes the function execution, can not be changed after creation.
-eventVersion | `string` | **Required.** is CDN event version which invokes the function execution, can not be changed after creation.
-notes | `string` | **Required.** Descriptions.
-filter   | [`CDN Filter configuration object`](#CDN-Filter-configuration-object) | **Required** Filters (Need to have at least one filter).
+EventName | `string` | **Required.** is CDN event which invokes the function execution, can not be changed after creation.
+EventVersion | `string` | **Required.** is CDN event version which invokes the function execution, can not be changed after creation.
+Notes | `string` | **Required.** Descriptions.
+Filter   | [`CDN Filter configuration object`](#CDN-Filter-configuration-object) | **Required** Filters (Need to have at least one filter).
 
 ##### Example: CDN event source object
 
@@ -446,11 +446,11 @@ filter   | [`CDN Filter configuration object`](#CDN-Filter-configuration-object)
 cdn-trigger-name: # trigger name
     Type: CDN # trigger type
     Properties:
-      eventName: LogFileCreated
-      eventVersion: '1.0.0'
-      notes: cdn events trigger test
-      filter: 
-        domain: ['www.taobao.com','www.tmall.com']
+      EventName: LogFileCreated
+      EventVersion: '1.0.0'
+      Notes: cdn events trigger test
+      Filter: 
+        Domain: ['www.taobao.com','www.tmall.com']
 ```
 
 ### Property types
@@ -604,26 +604,26 @@ Example：
 
 #### OSS Key configuration object
 
-The properties that the key configuration object contains include the `prefix` and `suffix` attributes. 
+The properties that the key configuration object contains include the `Prefix` and `Suffix` attributes. 
 They mean the following: matching prefix and matching suffix.
 
 Example: 
 
 ```
-key:
-  prefix: source/
-  suffix: .png
+Key:
+  Prefix: source/
+  Suffix: .png
   
 ```
 
 #### CDN Filter configuration object
 
-The configurable properties of the filter object currently only include: `domain`, which is an array of strings, which is required to represent a collection of filter parameter values.
+The configurable properties of the Filter object currently only include: `Domain`, which is an array of strings, which is required to represent a collection of Filter parameter values.
 
 Example: 
 
 ```
-filter: 
-  domain: ['www.taobao.com','www.tmall.com']
+Filter: 
+  Domain: ['www.taobao.com','www.tmall.com']
   
 ```
\ No newline at end of file
diff --git a/examples/cdn-trigger/index.js b/examples/cdn-trigger/index.js
new file mode 100644
index 0000000..8f1fce5
--- /dev/null
+++ b/examples/cdn-trigger/index.js
@@ -0,0 +1,12 @@
+exports.handler = function(event, context, callback) {
+    var eventObj = JSON.parse(event.toString());
+    console.log("event: " + event);
+    console.log('context: ', JSON.stringify(context));
+    console.log('eventName: '+ eventObj.events[0].eventName);
+    console.log('eventVersion: '+ eventObj.events[0].eventVersion);
+    console.log('eventSource: '+ eventObj.events[0].eventSource);
+    console.log('region: '+ eventObj.events[0].region);
+    console.log('eventParameter: '+ JSON.stringify(eventObj.events[0].eventParameter,null,4))
+    // cdn trigger 事件格式请参考 https://help.aliyun.com/document_detail/73333.html
+    callback(null, 'hello cdn_trigger');
+};
\ No newline at end of file
diff --git a/examples/cdn-trigger/index.py b/examples/cdn-trigger/index.py
deleted file mode 100644
index c5b2142..0000000
--- a/examples/cdn-trigger/index.py
+++ /dev/null
@@ -1,7 +0,0 @@
-# -*- coding: utf-8 -*-
-import logging
-
-def handler(event, context):
-  logger = logging.getLogger()
-  logger.info('cdn trigger event = %s', event)
-  return 'hello world'
\ No newline at end of file
diff --git a/examples/cdn-trigger/template.yml b/examples/cdn-trigger/template.yml
index 38a18ed..9c02ae0 100644
--- a/examples/cdn-trigger/template.yml
+++ b/examples/cdn-trigger/template.yml
@@ -10,15 +10,15 @@ Resources:
       Type: 'Aliyun::Serverless::Function'
       Properties:
         Handler: index.handler
-        Runtime: python2.7
+        Runtime: nodejs8
         CodeUri: './'
       Events:
         cdn-trigger-name: # trigger name
           Type: CDN # trigger type
           Properties:
-            eventName: CachedObjectsRefreshed
-            eventVersion: '1.0.0'
-            notes: cdn events trigger test
-            filter: 
-              domain: ['cdn-trigger.sunfeiyu.top']
+            EventName: CachedObjectsRefreshed
+            EventVersion: '1.0.0'
+            Notes: cdn events trigger test
+            Filter: 
+              Domain: ['cdn-trigger.sunfeiyu.top']
                
\ No newline at end of file
diff --git a/examples/oss-trigger/template.yml b/examples/oss-trigger/template.yml
index c3850dd..7295b5e 100644
--- a/examples/oss-trigger/template.yml
+++ b/examples/oss-trigger/template.yml
@@ -16,12 +16,12 @@ Resources:
         oss-trigger-name:
           Type: OSS # trigger type
           Properties:
-            bucketName: coco-superme # oss bucket name
-            events:
+            BucketName: coco-superme # oss bucket name
+            Events:
               - oss:ObjectCreated:*
               - oss:ObjectRemoved:DeleteObject        
-            filter: 
-              key:
-                prefix: source/
-                suffix: .png
+            Filter: 
+              Key:
+                Prefix: source/
+                Suffix: .png
                
\ No newline at end of file
diff --git a/lib/deploy/deploy-support.js b/lib/deploy/deploy-support.js
index 666e666..35913b3 100644
--- a/lib/deploy/deploy-support.js
+++ b/lib/deploy/deploy-support.js
@@ -490,7 +490,7 @@ async function getSourceArn(triggerType, triggerProperties) {
   } else if (triggerType === 'TableStore') {
     return `acs:ots:${profile.defaultRegion}:${profile.accountId}:instance/${triggerProperties.InstanceName}/table/${triggerProperties.TableName}`;
   } else if (triggerType === 'OSS') {
-    return `acs:oss:${profile.defaultRegion}:${profile.accountId}:${triggerProperties.bucketName}`;
+    return `acs:oss:${profile.defaultRegion}:${profile.accountId}:${triggerProperties.BucketName || triggerProperties.bucketName}`;
   } else if (triggerType === 'CDN') {
     return `acs:cdn:*:${profile.accountId}`;
   }
@@ -557,15 +557,15 @@ function getTriggerConfig(triggerType, triggerProperties) {
     return {};
   } else if (triggerType === 'OSS') {
     return {
-      events : triggerProperties.events,
-      filter : triggerProperties.filter
+      events : triggerProperties.Events || triggerProperties.events,
+      filter : triggerProperties.Filter || triggerProperties.filter
     };
   } else if (triggerType === 'CDN') {
     return {
-      eventName : triggerProperties.eventName,
-      eventVersion : triggerProperties.eventVersion,
-      notes : triggerProperties.notes,
-      filter : triggerProperties.filter
+      eventName : triggerProperties.EventName,
+      eventVersion : triggerProperties.EventVersion,
+      notes : triggerProperties.Notes,
+      filter : triggerProperties.Filter
     };
   }
   console.error(`trigger type is ${triggerType} not supported.`);
diff --git a/lib/validate/schema/cdn-event.js b/lib/validate/schema/cdn-event.js
index ef3dacd..55bbcd9 100644
--- a/lib/validate/schema/cdn-event.js
+++ b/lib/validate/schema/cdn-event.js
@@ -11,30 +11,30 @@ const cdnEventSchema = {
     'Properties': {
       'type': 'object',
       'properties': {
-        'eventName': {
+        'EventName': {
           'type': 'string'
         },
-        'eventVersion': {
+        'EventVersion': {
           'type': 'string'
         },
-        'notes': {
+        'Notes': {
           'type': 'string'
         },
-        'filter': {
+        'Filter': {
           'type': 'object',
           'properties': {
-            'domain': {
+            'Domain': {
               'type': 'array',
               'items': {
                 'type': 'string',
               }
             }
           },
-          'required': ['domain'],
+          'required': ['Domain'],
           'additionalProperties': false
         }
       },
-      'required': ['eventName','eventVersion','notes','filter'],
+      'required': ['EventName','EventVersion','Notes','Filter'],
       'additionalProperties': false
     },
   },
diff --git a/lib/validate/schema/oss-event.js b/lib/validate/schema/oss-event.js
index 87850e9..53b9c40 100644
--- a/lib/validate/schema/oss-event.js
+++ b/lib/validate/schema/oss-event.js
@@ -39,9 +39,38 @@ const ossEventSchema = {
           },
           'required': ['key'],
           'additionalProperties': false
+        },
+        'Events': {
+          'type': 'array',
+          'items': {
+            'type': 'string',
+          }
+        },
+        'BucketName': {
+          'type': 'string'
+        },
+        'Filter': {
+          'type': 'object',
+          'properties': {
+            'Key': {
+              'type': 'object',
+              'properties': {
+                'Prefix': {
+                  'type': 'string'
+                },
+                'Suffix': {
+                  'type': 'string'
+                },
+              },
+              'required': ['Prefix','Suffix'],
+              'additionalProperties': false
+            }
+          },
+          'required': ['Key'],
+          'additionalProperties': false
         }
       },
-      'required': ['events','bucketName','filter'],
+      'patternRequired': ['[Ee]vents','[Bb]ucketName','[Ff]ilter'],//为了兼容 #192 这个 pr 定义的 oss trigger 首字母小写的 schama
       'additionalProperties': false
     },
   },


Test patch:
diff --git a/test/deploy/deploy-by-tpl.test.js b/test/deploy/deploy-by-tpl.test.js
index 0bb5c58..bb0e8c8 100644
--- a/test/deploy/deploy-by-tpl.test.js
+++ b/test/deploy/deploy-by-tpl.test.js
@@ -486,9 +486,9 @@ describe('deploy', () => {
       triggerName: 'oss-trigger-name',
       triggerType: 'OSS',
       triggerProperties: {
-        bucketName: 'coco-superme',
-        events: ['oss:ObjectCreated:*', 'oss:ObjectRemoved:DeleteObject'],
-        filter: { key: { prefix: 'source/', suffix: '.png' }}
+        BucketName: 'coco-superme',
+        Events: ['oss:ObjectCreated:*', 'oss:ObjectRemoved:DeleteObject'],
+        Filter: { Key: { Prefix: 'source/', Suffix: '.png' }}
       },
     });
     assert.calledWith(deploySupport.getTriggerNameList, {
@@ -517,7 +517,7 @@ describe('deploy', () => {
         description: undefined,
         functionName: 'cdn-test-function',
         memorySize: undefined,
-        runtime: 'python2.7',
+        runtime: 'nodejs8',
         serviceName: 'cdn-test-service',
         timeout: undefined,
         initializationTimeout: undefined,
@@ -529,11 +529,11 @@ describe('deploy', () => {
       triggerName: 'cdn-trigger-name',
       triggerType: 'CDN',
       triggerProperties: {
-        'eventName': 'CachedObjectsRefreshed',
-        'eventVersion': '1.0.0',
-        'notes': 'cdn events trigger test',
-        'filter': {
-          'domain': [
+        'EventName': 'CachedObjectsRefreshed',
+        'EventVersion': '1.0.0',
+        'Notes': 'cdn events trigger test',
+        'Filter': {
+          'Domain': [
             'cdn-trigger.sunfeiyu.top'
           ]
         }},