openwhisk-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csantan...@apache.org
Subject [incubator-openwhisk] branch master updated: Set user agent header in API GW requests (#2439)
Date Thu, 03 Aug 2017 20:44:35 GMT
This is an automated email from the ASF dual-hosted git repository.

csantanapr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new d27e2e4  Set user agent header in API GW requests (#2439)
d27e2e4 is described below

commit d27e2e4efb9dee456e100dd4b6c17180feb11602
Author: Mark Deuser <mdeuser@us.ibm.com>
AuthorDate: Thu Aug 3 16:44:33 2017 -0400

    Set user agent header in API GW requests (#2439)
    
    * Add OW User-Agent header in API GW requests
    
    * Adhere to user-agent header format
    Append any incoming user-agent header value as a sub user-agent
---
 core/routemgmt/common/apigw-utils.js  | 34 +++++++++++++++++++++++-----------
 core/routemgmt/createApi/createApi.js | 11 ++++++++---
 core/routemgmt/deleteApi/deleteApi.js |  9 +++++++--
 core/routemgmt/getApi/getApi.js       | 12 +++++++++---
 4 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/core/routemgmt/common/apigw-utils.js b/core/routemgmt/common/apigw-utils.js
index 5f60e1c..a234595 100644
--- a/core/routemgmt/common/apigw-utils.js
+++ b/core/routemgmt/common/apigw-utils.js
@@ -21,6 +21,9 @@
 var request = require('request');
 var _ = require('lodash');
 
+const ApimgmtUserAgent = "OpenWhisk-apimgmt/1.0.0";
+var UserAgent = ApimgmtUserAgent;
+
 /**
  * Configures an API route on the API Gateway.  This API will map to an OpenWhisk action
that
  * will be invoked by the API Gateway when the API route is accessed.
@@ -42,15 +45,17 @@ function addApiToGateway(gwInfo, spaceGuid, swaggerApi, apiId) {
     followAllRedirects: true,
     url: gwInfo.gwUrl+'/'+encodeURIComponent(spaceGuid) + '/apis',
     json: swaggerApi,  // Use of json automatically sets header: 'Content-Type': 'application/json'
+    headers: {
+      'User-Agent': UserAgent
+    }
   };
   if (gwInfo.gwAuth) {
     _.set(options, "headers.Authorization", 'Bearer ' + gwInfo.gwAuth);
   }
-  console.log('addApiToGateway: ');
 
   if (apiId) {
     console.log("addApiToGateway: Updating existing API");
-    options.url = gwInfo.gwUrl + '/' + encodeURIComponent(spaceGuid) + '/apis/' + encodeURIComponent(apiId)
+    options.url = gwInfo.gwUrl + '/' + encodeURIComponent(spaceGuid) + '/apis/' + encodeURIComponent(apiId);
     requestFcn = request.put;
   }
 
@@ -64,8 +69,8 @@ function addApiToGateway(gwInfo, spaceGuid, swaggerApi, apiId) {
       var statusCode = response ? response.statusCode : undefined;
       console.log('addApiToGateway: response status:'+ statusCode);
       if (error) console.error('Warning: addRouteToGateway request failed: '+ makeJsonString(error));
+      if (response && response.headers) console.log('addApiToGateway: response headers:
'+makeJsonString(response.headers));
       if (body) console.log('addApiToGateway: response body: '+makeJsonString(body));
-
       if (error) {
         console.error('addApiToGateway: Unable to configure the API Gateway');
         reject('Unable to configure the API Gateway: '+makeJsonString(error));
@@ -103,7 +108,8 @@ function deleteApiFromGateway(gwInfo, spaceGuid, apiId) {
     url: gwInfo.gwUrl+'/'+encodeURIComponent(spaceGuid)+'/apis/'+encodeURIComponent(apiId),
     agentOptions: {rejectUnauthorized: false},
     headers: {
-      'Accept': 'application/json'
+      'Accept': 'application/json',
+      'User-Agent': UserAgent
     }
   };
   if (gwInfo.gwAuth) {
@@ -117,6 +123,7 @@ function deleteApiFromGateway(gwInfo, spaceGuid, apiId) {
       console.log('deleteApiFromGateway: response status:'+ statusCode);
       if (error) console.error('Warning: deleteGatewayApi request failed: '+ makeJsonString(error));
       if (body) console.log('deleteApiFromGateway: response body: '+makeJsonString(body));
+      if (response && response.headers) console.log('deleteApiFromGateway: response
headers: '+makeJsonString(response.headers));
       if (error) {
         console.error('deleteApiFromGateway: Unable to delete the API Gateway');
         reject('Unable to delete the API Gateway: '+makeJsonString(error));
@@ -155,7 +162,8 @@ function getApis(gwInfo, spaceGuid, bpOrApiName) {
     followAllRedirects: true,
     url: gwInfo.gwUrl+'/'+encodeURIComponent(spaceGuid)+'/apis',
     headers: {
-      'Accept': 'application/json'
+      'Accept': 'application/json',
+      'User-Agent': UserAgent
     },
     json: true
   };
@@ -375,7 +383,7 @@ function generateBaseSwaggerApi(basepath, apiname) {
 function addEndpointToSwaggerApi(swaggerApi, endpoint, responsetype) {
   var operation = endpoint.gatewayMethod.toLowerCase();
   var operationId = makeOperationId(operation, endpoint.gatewayPath);
-  var responsetype = responsetype || 'json';
+  responsetype = responsetype || 'json';
   console.log('addEndpointToSwaggerApi: operationid = '+operationId);
   try {
     var auth_base64 = Buffer.from(endpoint.action.authkey,'ascii').toString('base64');
@@ -477,9 +485,8 @@ function removeEndpointFromSwaggerApi(swaggerApi, endpoint) {
   if (!operation) {
       console.log('removeEndpointFromSwaggerApi: No operation; removing entire relpath '+relpath);
       if (swaggerApi.paths[relpath]) {
-          for (var operation in swaggerApi.paths[relpath]) {
-            var operationId = makeOperationId(operation, relpath);
-            deleteActionOperationInvocationDetails(swaggerApi, operationId);
+          for (var op in swaggerApi.paths[relpath]) {
+            deleteActionOperationInvocationDetails(swaggerApi, makeOperationId(op, relpath));
           }
           delete swaggerApi.paths[relpath];
       } else {
@@ -487,14 +494,13 @@ function removeEndpointFromSwaggerApi(swaggerApi, endpoint) {
           return 'path \''+relpath+'\' does not exist in the API';
       }
   } else { // relpath and operation are specified, just delete the specific operation
-      var operationId = makeOperationId(operation, relpath);
       if (swaggerApi.paths[relpath] && swaggerApi.paths[relpath][operation]) {
           delete swaggerApi.paths[relpath][operation];
           if (Object.keys(swaggerApi.paths[relpath]).length === 0) {
             console.log('removeEndpointFromSwaggerApi: after deleting operation '+operation+',
relpath '+relpath+' has no more operations; so deleting entire relpath '+relpath);
             delete swaggerApi.paths[relpath];
           }
-          deleteActionOperationInvocationDetails(swaggerApi, operationId);
+          deleteActionOperationInvocationDetails(swaggerApi, makeOperationId(operation, relpath));
       } else {
           console.log('removeEndpointFromSwaggerApi: relpath '+relpath+' with operation '+operation+'
does not exist in the API');
           return 'path \''+relpath+'\' with operation \''+operation+'\' does not exist in
the API';
@@ -865,6 +871,11 @@ function makeCamelCase(str) {
   return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
 }
 
+function setSubUserAgent(subAgent) {
+  if (subAgent && subAgent.length > 0) {
+    UserAgent = UserAgent + " " + subAgent;
+  }
+}
 
 module.exports.getApis = getApis;
 module.exports.addApiToGateway = addApiToGateway;
@@ -882,3 +893,4 @@ module.exports.updateNamespace = updateNamespace;
 module.exports.makeErrorResponseObject = makeErrorResponseObject;
 module.exports.makeResponseObject = makeResponseObject;
 module.exports.makeJsonString = makeJsonString;
+module.exports.setSubUserAgent = setSubUserAgent;
diff --git a/core/routemgmt/createApi/createApi.js b/core/routemgmt/createApi/createApi.js
index e71b50a..a984b33 100644
--- a/core/routemgmt/createApi/createApi.js
+++ b/core/routemgmt/createApi/createApi.js
@@ -58,7 +58,7 @@ function main(message) {
   //console.log('message: '+JSON.stringify(message));  // ONLY FOR TEMPORARY/LOCAL DEBUG;
DON'T ENABLE PERMANENTLY
   var badArgMsg = validateArgs(message);
   if (badArgMsg) {
-    return Promise.reject(utils2.makeErrorResponseObject(badArgMsg, (message.__ow_method
!= undefined)));
+    return Promise.reject(utils2.makeErrorResponseObject(badArgMsg, (message.__ow_method
!== undefined)));
   }
 
   var gwInfo = {
@@ -73,6 +73,11 @@ function main(message) {
     utils.updateNamespace(message.apidoc, message.__ow_user);
   }
 
+  // Set the User-Agent header value
+  if (message.__ow_headers && message.__ow_headers['user-agent']) {
+    utils2.setSubUserAgent(message.__ow_headers['user-agent']);
+  }
+
   // message.apidoc already validated; creating shortcut to it
   var doc;
   if (typeof message.apidoc === 'object') {
@@ -95,7 +100,7 @@ function main(message) {
   var tenantInstance = message.tenantInstance || 'openwhisk';
 
   // This can be invoked as either a standard web action or as a normal action
-  var calledAsWebAction = message.__ow_method != undefined;
+  var calledAsWebAction = message.__ow_method !== undefined;
 
   // Log parameter values
   console.log('GW URL        : '+message.gwUrl);
@@ -133,7 +138,7 @@ function main(message) {
     .then(function(endpointDocs) {
       console.log('Got '+endpointDocs.length+' APIs');
       if (endpointDocs.length === 0) {
-        console.log('No API found for namespace '+doc.namespace + ' with basePath '+ basepath)
+        console.log('No API found for namespace '+doc.namespace + ' with basePath '+ basepath);
         return Promise.resolve(utils2.generateBaseSwaggerApi(basepath, doc.apiName));
       } else {
         apiDocId = endpointDocs[0].artifact_id;
diff --git a/core/routemgmt/deleteApi/deleteApi.js b/core/routemgmt/deleteApi/deleteApi.js
index 4fd4c5e..421c25b 100644
--- a/core/routemgmt/deleteApi/deleteApi.js
+++ b/core/routemgmt/deleteApi/deleteApi.js
@@ -57,13 +57,18 @@ function main(message) {
     gwInfo.gwAuth = Buffer.from(message.gwUser+':'+message.gwPwd,'ascii').toString('base64');
   }
 
+  // Set the User-Agent header value
+  if (message.__ow_headers && message.__ow_headers['user-agent']) {
+    utils2.setSubUserAgent(message.__ow_headers['user-agent']);
+  }
+
   // Set namespace override if provided
   message.namespace = message.__ow_user || message.namespace;
 
   var tenantInstance = message.tenantInstance || 'openwhisk';
 
   // This can be invoked as either a web action or as a normal action
-  var calledAsWebAction = message.__ow_method != undefined;
+  var calledAsWebAction = message.__ow_method !== undefined;
 
   // Log parameter values
   console.log('GW URL        : '+message.gwUrl);
@@ -98,7 +103,7 @@ function main(message) {
     .then(function(endpointDocs) {
       console.log('Got '+endpointDocs.length+' APIs');
       if (endpointDocs.length === 0) {
-        console.log('No API found for namespace '+message.namespace + ' with basePath '+
message.basepath)
+        console.log('No API found for namespace '+message.namespace + ' with basePath '+
message.basepath);
         return Promise.reject('API \''+message.basepath+'\' does not exist.');
       } else if (endpointDocs.length > 1) {
         console.error('Multiple APIs found for namespace '+message.namespace+' with basepath/apiname
'+message.basepath);
diff --git a/core/routemgmt/getApi/getApi.js b/core/routemgmt/getApi/getApi.js
index 04766ed..198b820 100644
--- a/core/routemgmt/getApi/getApi.js
+++ b/core/routemgmt/getApi/getApi.js
@@ -46,9 +46,10 @@ var utils = require('./utils.js');
 var utils2 = require('./apigw-utils.js');
 
 function main(message) {
+  console.log('message: '+JSON.stringify(message));  // ONLY FOR TEMPORARY/LOCAL DEBUG; DON'T
ENABLE PERMANENTLY
   var badArgMsg = validateArgs(message);
   if (badArgMsg) {
-    return Promise.reject(utils2.makeErrorResponseObject(badArgMsg, (message.__ow_method
!= undefined)));
+    return Promise.reject(utils2.makeErrorResponseObject(badArgMsg, (message.__ow_method
!== undefined)));
   }
 
   message.outputFormat = message.outputFormat || 'swagger';
@@ -61,11 +62,16 @@ function main(message) {
     gwInfo.gwAuth = Buffer.from(message.gwUser+':'+message.gwPwd,'ascii').toString('base64');
   }
 
+  // Set the User-Agent header value
+  if (message.__ow_headers && message.__ow_headers['user-agent']) {
+    utils2.setSubUserAgent(message.__ow_headers['user-agent']);
+  }
+
   // Set namespace override if provided
   message.namespace = message.__ow_user || message.namespace;
 
   // This can be invoked as either web action or as a normal action
-  var calledAsWebAction = message.__ow_method != undefined;
+  var calledAsWebAction = message.__ow_method !== undefined;
 
   // Log parameter values
   console.log('gwUrl         : '+message.gwUrl);
@@ -89,7 +95,7 @@ function main(message) {
     .then(function(endpointDocs) {
       console.log('Got '+endpointDocs.length+' APIs');
       if (endpointDocs.length === 0) {
-        console.log('No API found for namespace '+message.namespace + ' with basePath '+
message.basepath)
+        console.log('No API found for namespace '+message.namespace + ' with basePath '+
message.basepath);
       }
       var cliApis = utils2.generateCliResponse(endpointDocs);
       console.log('getApi success');

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <commits@openwhisk.apache.org>'].

Mime
View raw message