Repository: aliyun/fun
Pull request #67 (2018-07-27 13:14:42)

fix ots parttern regex and add apigateway ConstantParameters support 


Patch:
diff --git a/lib/deploy/deploy-by-tpl.js b/lib/deploy/deploy-by-tpl.js
index 89480f6..b1fd394 100644
--- a/lib/deploy/deploy-by-tpl.js
+++ b/lib/deploy/deploy-by-tpl.js
@@ -354,6 +354,8 @@ async function deployApigateway(name, { apiDefinition, template, tplPath }) {
             openIdConnectConfig = methodDefinition['x-aliyun-apigateway-openid-connect-config'];
           }
 
+          const constantParameters = methodDefinition['x-aliyun-apigateway-constant-parameters'];
+
           await makeApi(apiGroup, {
             stageName: apiDefinition.Properties.StageName,
             requestPath: k,
@@ -369,8 +371,9 @@ async function deployApigateway(name, { apiDefinition, template, tplPath }) {
               config: openIdConnectConfig,
             },
             visibility: methodDefinition['x-aliyun-apigateway-visibility'],
-            requestConfig: requestConfig,
-            resultConfig: resultConfig,
+            requestConfig,
+            resultConfig,
+            constantParameters, 
             description: methodDefinition['x-aliyun-apigateway-description']
           });
         }
diff --git a/lib/deploy/deploy-support.js b/lib/deploy/deploy-support.js
index 6968b7b..7a61dab 100644
--- a/lib/deploy/deploy-support.js
+++ b/lib/deploy/deploy-support.js
@@ -585,6 +585,7 @@ async function makeApi(group, {
   visibility = 'Private',
   requestConfig = {},
   resultConfig = {},
+  constantParameters,
   description
 }) {
   await promiseRetry(async (retry, times) => {
@@ -656,7 +657,11 @@ async function makeApi(group, {
         ResultSample: resultConfig.resultSample || 'result sample',
         FailResultSample: resultConfig.failResultSample || 'failed samples'
       };
-    
+
+      if (constantParameters) {
+        params.ConstantParameters = JSON.stringify(constantParameters);
+      }
+
       if (auth.type === 'APPOPENID' || auth.type === 'OPENID') {
         var openidConf = auth.config || {};
         params.OpenIdConnectConfig = JSON.stringify({
@@ -700,8 +705,14 @@ async function makeApi(group, {
         }
       });
     } catch (ex) {
-      console.log(red(`\tretry ${times} times`));
-      retry(ex);
+      if (ex.code === 'DuplicateRequestParamaters') {
+        console.error(red(ex.message));
+        process.exit(-1);
+      } else {
+        console.log(red(ex.message));
+        console.log(red(`\tretry ${times} times`));
+        retry(ex);
+      }scrollTo;
     }
   }, retryOptions);
 }
@@ -765,8 +776,13 @@ async function makeOtsInstance(instanceName, clusterType, description) {
         method: 'POST'
       });
     } catch (ex) {
-      console.log(red(`retry ${times} times`));
-      retry(ex);
+      if (ex.code === 'InvalidParameter' || ex.code === 'QuotaExhausted') {
+        console.error(red(ex.message));
+        process.exit(-1);
+      } else {
+        console.error(red(`retry ${times} times`));
+        retry(ex);
+      }
     }
   }, retryOptions);
 }
@@ -796,7 +812,7 @@ async function makeOtsTable({
 
       const tables = await client.listTable().catch(err => {
         if (err.errno === 'ENOTFOUND' && err.syscall === 'getaddrinfo') {
-          console.error(`Instance '${err.hostname.split('.')[0]}' is not found.`);
+          console.error(red(`Instance '${err.hostname.split('.')[0]}' is not found.`));
         }
         throw err;
       });
diff --git a/lib/validate/schema/ots-resource.js b/lib/validate/schema/ots-resource.js
index 791e015..dea52bd 100644
--- a/lib/validate/schema/ots-resource.js
+++ b/lib/validate/schema/ots-resource.js
@@ -24,7 +24,7 @@ const otsResourceSchema = {
     },
   },
   'patternProperties': {
-    '^(?!Type|Properties)[a-zA-Z][a-zA-Z0-9_]{0,127}$': {
+    '^(?!Type|Properties)[a-zA-Z_][a-zA-Z0-9_]{0,127}$': {
       '$ref': '/Resources/TableStore/Table'
     }
   },


Test patch:
diff --git a/test/deploy/deploy-by-tpl.test.js b/test/deploy/deploy-by-tpl.test.js
index daeb8bd..516ee00 100644
--- a/test/deploy/deploy-by-tpl.test.js
+++ b/test/deploy/deploy-by-tpl.test.js
@@ -169,6 +169,7 @@ describe('deploy', () => {
       stageName: 'RELEASE',
       visibility: 'PRIVATE',
       serviceTimeout: 3000,
+      constantParameters: undefined,
       resultConfig: { failResultSample: undefined, resultSample: undefined, resultType: undefined },      
       requestConfig: {  }
     });
@@ -251,6 +252,7 @@ describe('deploy', () => {
       visibility: undefined,
       serviceTimeout: 3000,
       requestConfig: {},
+      constantParameters: undefined,
       resultConfig: { failResultSample: undefined, resultSample: undefined, resultType: undefined },    });
   });
   it('deploy segment', async () => {
@@ -294,6 +296,7 @@ describe('deploy', () => {
       stageName: 'RELEASE',
       visibility: undefined,
       resultConfig: { failResultSample: undefined, resultSample: undefined, resultType: undefined },
+      constantParameters: undefined,
       serviceTimeout: 3000,
       requestConfig: { requestMode: 'PASSTHROUGH', requestProtocol: 'http' }
     });
@@ -376,6 +379,7 @@ describe('deploy', () => {
       roleArn: `acs:ram::123:role/aliyunfcgeneratedrole-fc`,
       serviceName: 'wechat',
       stageName: 'RELEASE',
+      constantParameters: undefined,
       visibility: undefined,
       requestConfig: { bodyFormat: 'STREAM', requestMode: 'MAPPING', requestProtocol: 'HTTP' },
       resultConfig: { failResultSample: undefined, resultSample: undefined, resultType: undefined },      
@@ -411,6 +415,7 @@ describe('deploy', () => {
       serviceName: 'wechat',
       stageName: 'RELEASE',
       visibility: undefined,
+      constantParameters: undefined,
       requestConfig: { bodyFormat: 'STREAM', requestMode: 'MAPPING', requestProtocol: 'HTTP' },
       resultConfig: { failResultSample: undefined, resultSample: undefined, resultType: undefined },      serviceTimeout: 3000
     });