Repository: aliyun/fun
Pull request #196 (2019-04-18 07:46:27)

The trigger on the line is different from the local. and fix document…
#160  The trigger on the line is different from the local. and fix document… 
发布完 function 后，需要去查下所有的 trigger，如果存在于本地 trigger 不一致的，那么就提示用户，需要手动删除

tanhe123: 测了下，还有两个问题：
第一个是如果 yml 没有配置 events，那么即使线上有也不会提示 warning。
第二个是 warning 没有添加缩进。

再提个 mr 上来把，这个先 merge 进去。


Patch:
diff --git a/docs/usage/installation-zh.md b/docs/usage/installation-zh.md
index 408d09f..81f7313 100644
--- a/docs/usage/installation-zh.md
+++ b/docs/usage/installation-zh.md
@@ -18,7 +18,7 @@
 
 可以参考官方[教程](https://store.docker.com/editions/community/docker-ce-desktop-windows)。
 
-如果遇到网络问题，可以下载阿里云提供的 [Docker For Windows](http://mirrors.aliyun.com/docker-toolbox/windows/docker-for-windows/stable/)。
+如果遇到网络问题，可以下载阿里云提供的 [Docker For Windows](http://mirrors.aliyun.com/docker-toolbox/windows/docker-for-windows/beta/)。
 
 ### Linux 安装 docker
 
diff --git a/lib/deploy/deploy-by-tpl.js b/lib/deploy/deploy-by-tpl.js
index 76a2157..3e0235f 100644
--- a/lib/deploy/deploy-by-tpl.js
+++ b/lib/deploy/deploy-by-tpl.js
@@ -12,7 +12,7 @@ const getTpl = require('../tpl').getTpl;
 const validate = require('../validate/validate');
 const debug = require('debug')('fun:deploy');
 
-const { green, yellow } = require('colors');
+const { green, yellow, red} = require('colors');
 
 const readFile = util.promisify(fs.readFile);
 const _ = require('lodash');
@@ -30,7 +30,8 @@ let {
   makeSlsProject,
   makeLogstore,
   makeLogstoreIndex,
-  makeCustomDomain
+  makeCustomDomain,
+  getTriggerNameList
 } = require('./deploy-support');
 
 let {
@@ -86,6 +87,16 @@ async function displayTriggerInfo(serviceName, functionName, triggerName, trigge
 
 async function deployTriggers(serviceName, functionName, events) {
   if (events) {
+
+    let localTriggerNames = Object.keys(events);
+    let onLineTriggerNames = await getTriggerNameList({serviceName, functionName});
+
+    onLineTriggerNames.filter(x =>!_.includes(localTriggerNames,x)).forEach(element => {
+
+      console.warn(red(`The trigger ${element} you configured in fc console does not match the local configuration. Fun will not modify this trigger. You can remove this trigger manually through fc console if necessary`));
+
+    });
+    
     for (const [triggerName, triggerDefinition] of Object.entries(events)) {
       console.log(`\t\tWaiting for ${yellow(triggerDefinition.Type)} trigger ${triggerName} to be deployed...`);
 
@@ -114,7 +125,6 @@ async function deployFunction(baseDir, serviceName, functionName, functionRes) {
     codeUri: properties.CodeUri,
     environmentVariables: properties.EnvironmentVariables
   });
-
   await deployTriggers(serviceName, functionName, functionRes.Events);
 }
 
diff --git a/lib/deploy/deploy-support.js b/lib/deploy/deploy-support.js
index 8e129c6..8bafed3 100644
--- a/lib/deploy/deploy-support.js
+++ b/lib/deploy/deploy-support.js
@@ -737,6 +737,21 @@ async function makeInvocationRole(serviceName, functionName, triggerType) {
   return;
 }
 
+async function getTriggerNameList ({
+  serviceName,
+  functionName
+}) {
+  const fc = await getFcClient();
+  var listTriggerResponse = await fc.listTriggers(serviceName,functionName);
+
+  var triggerNameArray = [];
+
+  if(listTriggerResponse && listTriggerResponse.data.triggers) {
+    triggerNameArray = listTriggerResponse.data.triggers.map(p => p.triggerName);
+  }
+  return triggerNameArray;
+}
+
 async function makeTrigger({
   serviceName,
   functionName,
@@ -1162,5 +1177,6 @@ module.exports = {
   makeCustomDomain,
   makeMnsTopic,
   makeInvocationRole,
-  getFunCodeAsBase64
+  getFunCodeAsBase64,
+  getTriggerNameList
 };
\ No newline at end of file


Test patch:
diff --git a/test/deploy/deploy-by-tpl.test.js b/test/deploy/deploy-by-tpl.test.js
index fe6067a..c6e300c 100644
--- a/test/deploy/deploy-by-tpl.test.js
+++ b/test/deploy/deploy-by-tpl.test.js
@@ -14,7 +14,11 @@ describe('deploy service role ',() => {
 
   beforeEach(() => {
     Object.keys(deploySupport).forEach(m => {
-      sandbox.stub(deploySupport, m).resolves({});
+      if (m === 'getTriggerNameList') {
+        sandbox.stub(deploySupport, m).resolves([]);
+      }else {
+        sandbox.stub(deploySupport, m).resolves({});
+      }
     });
 
     Object.keys(ram).forEach(m => {
@@ -85,7 +89,11 @@ describe('deploy', () => {
 
   beforeEach(() => {
     Object.keys(deploySupport).forEach(m => {
-      sandbox.stub(deploySupport, m).resolves({});
+      if (m === 'getTriggerNameList') {
+        sandbox.stub(deploySupport, m).resolves([]);
+      }else {
+        sandbox.stub(deploySupport, m).resolves({});
+      }
     });
 
     Object.keys(ram).forEach(m => {
@@ -443,6 +451,49 @@ describe('deploy', () => {
     });
   });
 
+  it('deploy oss-trigger', async () => {
+    await deploy('oss-trigger');
+
+    assert.calledWith(deploySupport.makeService, {
+      description: 'oss trigger test',
+      internetAccess: null,
+      logConfig: {},
+      role: '',
+      serviceName: 'oss-test-service',
+      vpcConfig: {},
+      nasConfig: {}
+    });
+    assert.calledWith(deploySupport.makeFunction,
+      path.join(process.cwd(), 'examples', 'oss-trigger'), {
+        codeUri: './',
+        handler: 'index.handler',
+        initializer: undefined,
+        description: undefined,
+        functionName: 'oss-test-function',
+        memorySize: undefined,
+        runtime: 'python2.7',
+        serviceName: 'oss-test-service',
+        timeout: undefined,
+        initializationTimeout: undefined,
+        environmentVariables: undefined
+      });
+    assert.calledWith(deploySupport.makeTrigger, {
+      serviceName: 'oss-test-service',
+      functionName: 'oss-test-function',
+      triggerName: 'oss-trigger-name',
+      triggerType: 'OSS',
+      triggerProperties: {
+        bucketName: 'coco-superme',
+        events: ['oss:ObjectCreated:*', 'oss:ObjectRemoved:DeleteObject'],
+        filter: { key: { prefix: 'source/', suffix: '.png' }}
+      },
+    });
+    assert.calledWith(deploySupport.getTriggerNameList, {
+      serviceName: 'oss-test-service',
+      functionName: 'oss-test-function'
+    });
+  });
+
   it('deploy mnsTopic-trigger', async () => {
     await deploy('mnsTopic-trigger');