2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved.
5 ===================================================================
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
30 } from '@loopback/repository';
44 } from '@loopback/rest';
45 import { Blueprint } from '../models';
46 import { inject } from '@loopback/core';
47 import { BlueprintService } from '../services';
48 import * as fs from 'fs';
49 import * as multiparty from 'multiparty';
50 import * as request_lib from 'request';
51 import { processorApiConfig, appConfig } from '../config/app-config';
52 import { bluePrintManagementServiceGrpcClient } from '../clients/blueprint-management-service-grpc-client';
54 export class BlueprintRestController {
56 @inject('services.BlueprintService')
57 public bpservice: BlueprintService,
60 @get('/controllerblueprint/all', {
63 description: 'Blueprint model instance',
64 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
69 return await this.bpservice.getAllblueprints();
72 @get('/controllerblueprint/paged', {
75 description: 'Blueprint model instance with pagination',
76 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
80 async getPagedBlueprints(
81 @param.query.number('limit') limit: number,
82 @param.query.number('offset') offset: number,
83 @param.query.string('sort') sort: string) {
84 return await this.bpservice.getPagedBueprints(limit, offset, sort);
87 @get('/controllerblueprint/meta-data/{keyword}', {
90 description: 'Blueprint model instance',
91 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
95 async getAllPacakgesByKeword(@param.path.string('keyword') keyword: string) {
96 return await this.bpservice.getBlueprintsByKeyword(keyword);
101 @get('/controllerblueprint/searchByTags/{tags}', {
104 content: { 'application/json': {} },
108 async getByTags(@param.path.string('tags') tags: string) {
109 return await this.bpservice.getByTags(tags);
112 @post('/controllerblueprint/create-blueprint')
115 description: 'multipart/form-data value.',
118 'multipart/form-data': {
120 'x-parser': 'stream',
121 schema: { type: 'object' },
126 @inject(RestBindings.Http.RESPONSE) response: Response,
127 ): Promise<Response> {
128 return new Promise((resolve, reject) => {
129 this.getFileFromMultiPartForm(request).then(file => {
130 // if (appConfig.action.deployBlueprint.grpcEnabled)
131 if (appConfig.action.grpcEnabled)
132 return this.uploadFileToBlueprintProcessorGrpc(file, "DRAFT", response);
134 return this.uploadFileToBlueprintController(file, "/blueprint-model/", response);
139 // return new Promise((resolve, reject) => {
140 // this.getFileFromMultiPartForm(request).then(file => {
141 // this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
152 @post('/controllerblueprint/publish')
155 description: 'multipart/form-data value.',
158 'multipart/form-data': {
160 'x-parser': 'stream',
161 schema: { type: 'object' },
166 @inject(RestBindings.Http.RESPONSE) response: Response,
167 ): Promise<Response> {
168 return new Promise((resolve, reject) => {
169 this.getFileFromMultiPartForm(request).then(file => {
170 // if (appConfig.action.deployBlueprint.grpcEnabled)
171 if (appConfig.action.grpcEnabled)
172 return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
174 return this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response);
179 // return new Promise((resolve, reject) => {
180 // this.getFileFromMultiPartForm(request).then(file => {
181 // this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
192 @post('/controllerblueprint/enrich-blueprint')
195 description: 'multipart/form-data value.',
198 'multipart/form-data': {
200 'x-parser': 'stream',
201 schema: { type: 'object' },
206 @inject(RestBindings.Http.RESPONSE) response: Response,
207 ): Promise<Response> {
208 return new Promise((resolve, reject) => {
209 this.getFileFromMultiPartForm(request).then(file => {
210 if (appConfig.action.grpcEnabled)
211 return this.uploadFileToBlueprintProcessorGrpc(file, "ENRICH", response);
213 return this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response)
214 // this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
225 @get('/controllerblueprint/download-blueprint/{name}/{version}')
227 @param.path.string('name') name: string,
228 @param.path.string('version') version: string,
229 @inject(RestBindings.Http.RESPONSE) response: Response,
230 ): Promise<Response> {
232 if (appConfig.action.grpcEnabled)
233 return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
235 return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/" + name + "/version/" + version, response);
239 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
240 return new Promise((resolve, reject) => {
241 let form = new multiparty.Form();
242 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
243 if (err) reject(err);
244 let file = files['file'][0]; // get the file from the returned files object
246 reject('File was not found in form data.');
254 @post('/controllerblueprint/deploy-blueprint')
257 description: 'multipart/form-data value.',
260 'multipart/form-data': {
262 'x-parser': 'stream',
263 schema: { type: 'object' },
268 @inject(RestBindings.Http.RESPONSE) response: Response,
269 ): Promise<Response> {
270 return new Promise((resolve, reject) => {
271 this.getFileFromMultiPartForm(request).then(file => {
272 // if (appConfig.action.deployBlueprint.grpcEnabled)
273 if (appConfig.action.grpcEnabled)
274 return this.uploadFileToBlueprintProcessorGrpc(file, "PUBLISH", response);
276 return this.uploadFileToBlueprintProcessor(file, "/execution-service/upload/", response);
283 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
284 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
287 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
288 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
291 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
295 Authorization: authToken,
296 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
300 value: fs.createReadStream(file.path),
303 contentType: 'application/zip'
309 var removeTempFile = () => {
310 fs.unlink(file.path, (err: any) => {
317 return new Promise((resolve, reject) => {
318 request_lib.post(options)
319 .on("error", err => {
323 .once("finish", () => {
330 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
331 return this.downloadFileFromBlueprintService(processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
334 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
338 Authorization: authToken,
341 return new Promise((resolve, reject) => {
342 request_lib.get(options)
343 .on("error", err => {
347 .once("finish", () => {
353 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
354 return new Promise<Response>((resolve, reject) => {
355 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
356 response.send(output.status.message);
359 response.status(500).send(err);
364 async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
365 return new Promise<Response>((resolve, reject) => {
366 bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
368 response.send(output.status.message);
371 response.status(500).send(err);