Loopback code for GRPC functionality 43/97243/2
authorEzhilarasi <ezhrajam@in.ibm.com>
Thu, 17 Oct 2019 10:28:08 +0000 (15:58 +0530)
committerEzhilarasi R <ezhrajam@in.ibm.com>
Fri, 18 Oct 2019 10:10:01 +0000 (10:10 +0000)
GRPC integration for Draft, Publish, Download
Change-Id: I24e138fae766da7a112ac9f49a8ba036d81b5bb5
Issue-ID: CCSDK-1785
Signed-off-by: Ezhilarasi <ezhrajam@in.ibm.com>
cds-ui/server/src/clients/blueprint-management-service-grpc-client.ts
cds-ui/server/src/config/app-config.ts
cds-ui/server/src/controllers/blueprint-rest.controller.ts

index b66b2a7..8975f45 100644 (file)
@@ -17,7 +17,7 @@ import * as fs from 'fs';
 import * as uuidv1 from 'uuid/v1';
 const grpc = require('grpc');
 import * as protoLoader from '@grpc/proto-loader';
-import {processorApiConfig} from '../config/app-config';
+import { processorApiConfig } from '../config/app-config';
 
 const PROTO_PATH = processorApiConfig.grpc.bluePrintManagement.protoPath;
 
@@ -44,7 +44,7 @@ metadata.add('Authorization', processorApiConfig.grpc.authToken);
 
 class BluePrintManagementServiceGrpcClient {
 
-    async uploadBlueprint(filePath: string): Promise<any> {
+    async uploadBlueprint(filePath: string, actionName: string): Promise<any> {
 
         let input = {
             commonHeader: {
@@ -55,6 +55,11 @@ class BluePrintManagementServiceGrpcClient {
             },
             fileChunk: {
                 chunk: fs.readFileSync(filePath)
+            },
+            actionIdentifiers: {
+                mode: "sync",
+                blueprintName: "cds.zip",
+                actionName: actionName
             }
         }
 
@@ -80,6 +85,34 @@ class BluePrintManagementServiceGrpcClient {
         });
 
     }
+
+    async downloadBlueprint(blueprintName: string,blueprintVersion: string): Promise<any> {
+
+        let input = {
+            commonHeader: {
+                timestamp: new Date(),
+                originatorId: "cds-ui",
+                requestId: uuidv1(),
+                subRequestId: "1234-56",
+            },
+            actionIdentifiers: {
+                mode: "sync",
+                blueprintName: blueprintName,
+                blueprintVersion: blueprintVersion
+            }
+        }
+
+        return new Promise<any>((resolve, reject) => {
+            stub.downloadBlueprint(input, metadata, (err: any, output: any) => {
+                if (err) {
+                    reject(err);
+                    return;
+                }
+                resolve(output);
+            });
+        });
+
+    }
 }
 
 export const bluePrintManagementServiceGrpcClient = new BluePrintManagementServiceGrpcClient();
index a2cebe5..9b253b8 100644 (file)
@@ -15,9 +15,9 @@
 */
 export const appConfig = Object.freeze({
     action: Object.freeze({
-        deployBlueprint: Object.freeze({
+        // deployBlueprint: Object.freeze({
             grpcEnabled: process.env.APP_ACTION_DEPLOY_BLUEPRINT_GRPC_ENABLED || true
-        })
+        // })
     })
 });
 
index 7895898..1eef6fb 100644 (file)
@@ -98,15 +98,26 @@ export class BlueprintRestController {
   ): Promise<Response> {
     return new Promise((resolve, reject) => {
       this.getFileFromMultiPartForm(request).then(file => {
-        this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
-          resolve(resp);
-        }, err => {
-          reject(err);
-        });
+        // if (appConfig.action.deployBlueprint.grpcEnabled)
+        if (appConfig.action.grpcEnabled)
+          return this.uploadFileToBlueprintProcessorGrpc(file, "DRAFT", response);
+        else
+          return this.uploadFileToBlueprintController(file, "/blueprint-model/", response);
       }, err => {
         reject(err);
       });
     });
+    // return new Promise((resolve, reject) => {
+    //   this.getFileFromMultiPartForm(request).then(file => {
+    //     this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
+    //       resolve(resp);
+    //     }, err => {
+    //       reject(err);
+    //     });
+    //   }, err => {
+    //     reject(err);
+    //   });
+    // });
   }
 
   @post('/controllerblueprint/publish')
@@ -127,15 +138,26 @@ export class BlueprintRestController {
   ): Promise<Response> {
     return new Promise((resolve, reject) => {
       this.getFileFromMultiPartForm(request).then(file => {
-        this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
-          resolve(resp);
-        }, err => {
-          reject(err);
-        });
+        // if (appConfig.action.deployBlueprint.grpcEnabled)
+        if (appConfig.action.grpcEnabled)
+          return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
+        else
+          return this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response);
       }, err => {
         reject(err);
       });
     });
+    // return new Promise((resolve, reject) => {
+    //   this.getFileFromMultiPartForm(request).then(file => {
+    //     this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
+    //       resolve(resp);
+    //     }, err => {
+    //       reject(err);
+    //     });
+    //   }, err => {
+    //     reject(err);
+    //   });
+    // });
   }
 
   @post('/controllerblueprint/enrich-blueprint')
@@ -156,13 +178,17 @@ export class BlueprintRestController {
   ): Promise<Response> {
     return new Promise((resolve, reject) => {
       this.getFileFromMultiPartForm(request).then(file => {
-        this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
-          resolve(resp);
-        }, err => {
-          reject(err);
-        });
-      }, err => {
-        reject(err);
+        if (appConfig.action.grpcEnabled)
+          return this.uploadFileToBlueprintProcessorGrpc(file, "ENRICH", response);
+        else
+          return this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response)
+        //   this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
+        //     resolve(resp);
+        //   }, err => {
+        //     reject(err);
+        //   });
+        // }, err => {
+        //   reject(err);
       });
     });
   }
@@ -173,9 +199,14 @@ export class BlueprintRestController {
     @param.path.string('version') version: string,
     @inject(RestBindings.Http.RESPONSE) response: Response,
   ): Promise<Response> {
-    return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/" + name + "/version/" + version, response);
+
+    if (appConfig.action.grpcEnabled)
+      return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
+    else
+      return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/" + name + "/version/" + version, response);
   }
 
+
   async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
     return new Promise((resolve, reject) => {
       let form = new multiparty.Form();
@@ -209,8 +240,9 @@ export class BlueprintRestController {
   ): Promise<Response> {
     return new Promise((resolve, reject) => {
       this.getFileFromMultiPartForm(request).then(file => {
-        if (appConfig.action.deployBlueprint.grpcEnabled)
-          return this.uploadFileToBlueprintProcessorGrpc(file, response);
+        // if (appConfig.action.deployBlueprint.grpcEnabled)
+        if (appConfig.action.grpcEnabled)
+          return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
         else
           return this.uploadFileToBlueprintProcessor(file, "/execution-service/upload/", response);
       }, err => {
@@ -289,9 +321,9 @@ export class BlueprintRestController {
     })
   }
 
-  async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, response: Response): Promise<Response> {
+  async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
     return new Promise<Response>((resolve, reject) => {
-      bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path).then(output => {
+      bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
         response.send(output.status.message);
         resolve(response);
       }, err => {
@@ -300,4 +332,16 @@ export class BlueprintRestController {
       });
     });
   }
+  async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
+    return new Promise<Response>((resolve, reject) => {
+      bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
+        .then(output => {
+          response.send(output.status.message);
+          resolve(response);
+        }, err => {
+          response.status(500).send(err);
+          resolve(response);
+        });
+    });
+  }
 }
\ No newline at end of file