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============================================
23 import { get, param, post, Request, requestBody, Response, RestBindings, del } from '@loopback/rest';
24 import { Blueprint } from '../models';
25 import { inject } from '@loopback/core';
26 import { BlueprintService } from '../services';
27 import * as fs from 'fs';
28 import * as multiparty from 'multiparty';
29 import * as request_lib from 'request';
30 import { appConfig, processorApiConfig } from '../config/app-config';
31 import { bluePrintManagementServiceGrpcClient } from '../clients/blueprint-management-service-grpc-client';
32 import { BlueprintDetail } from '../models/blueprint.detail.model';
34 export class BlueprintRestController {
36 @inject('services.BlueprintService')
37 public bpservice: BlueprintService,
41 @get('/controllerblueprint/all', {
44 description: 'Blueprint model instance',
45 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
50 return await this.bpservice.getAllblueprints();
53 @get('/controllerblueprint/{id}', {
56 description: 'Blueprint model instance',
57 content: { 'application/json': { schema: { 'x-ts-type': BlueprintDetail } } },
61 async getOneBluePrint(@param.path.string('id') id: string) {
62 return await this.bpservice.getOneBluePrint(id);
65 @del('/controllerblueprint/{id}', {
68 description: 'Blueprint model instance',
69 content: { 'application/json': { schema: { 'x-ts-type': BlueprintDetail } } },
73 async deleteBluePrint(@param.path.string('id') id: string) {
74 return await this.bpservice.deleteBluePrint(id);
78 @get('/controllerblueprint/paged', {
81 description: 'Blueprint model instance with pagination',
82 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
86 async getPagedBlueprints(
87 @param.query.number('limit') limit: number,
88 @param.query.number('offset') offset: number,
89 @param.query.string('sort') sort: string,
90 @param.query.string('sortType') sortType: string) {
91 return await this.bpservice.getPagedBlueprints(limit, offset, sort, sortType);
94 @get('/controllerblueprint/metadata/paged/{keyword}', {
97 description: 'Blueprint model instance with pagination',
98 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
102 async getMetaDataPagedBlueprints(
103 @param.path.string('keyword') keyword: string,
104 @param.query.number('limit') limit: number,
105 @param.query.number('offset') offset: number,
106 @param.query.string('sort') sort: string,
107 @param.query.string('sortType') sortType: string) {
108 return await this.bpservice.getMetaDataPagedBlueprints(limit, offset, sort, keyword, sortType);
111 @get('/controllerblueprint/meta-data/{keyword}', {
114 description: 'Blueprint model instance',
115 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
119 async getAllPacakgesByKeword(@param.path.string('keyword') keyword: string) {
120 return await this.bpservice.getBlueprintsByKeyword(keyword);
123 @get('/controllerblueprint/by-name/{name}/version/{version}', {
126 description: 'Blueprint model instance',
127 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
131 async getPacakgesByNameAndVersion(@param.path.string('name') name: string, @param.path.string('version') version: string) {
132 return await this.bpservice.getBlueprintByNameAndVersion(name, version);
135 @get('/controllerblueprint/searchByTags/{tags}', {
138 content: { 'application/json': {} },
142 async getByTags(@param.path.string('tags') tags: string) {
143 return await this.bpservice.getByTags(tags);
146 @post('/controllerblueprint/create-blueprint')
149 description: 'multipart/form-data value.',
152 'multipart/form-data': {
154 'x-parser': 'stream',
155 schema: { type: 'object' },
160 @inject(RestBindings.Http.RESPONSE) response: Response,
161 ): Promise<Response> {
162 return new Promise((resolve, reject) => {
163 this.getFileFromMultiPartForm(request).then(file => {
164 // if (appConfig.action.deployBlueprint.grpcEnabled)
165 if (appConfig.action.grpcEnabled)
166 return this.uploadFileToBlueprintProcessorGrpc(file, 'DRAFT', response);
168 return this.uploadFileToBlueprintController(file, '/blueprint-model/', response);
173 // return new Promise((resolve, reject) => {
174 // this.getFileFromMultiPartForm(request).then(file => {
175 // this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
186 @post('/controllerblueprint/publish')
189 description: 'multipart/form-data value.',
192 'multipart/form-data': {
194 'x-parser': 'stream',
195 schema: { type: 'object' },
200 @inject(RestBindings.Http.RESPONSE) response: Response,
201 ): Promise<Response> {
202 return new Promise((resolve, reject) => {
203 this.getFileFromMultiPartForm(request).then(file => {
204 // if (appConfig.action.deployBlueprint.grpcEnabled)
205 if (appConfig.action.grpcEnabled)
206 return this.uploadFileToBlueprintProcessorGrpc(file, 'PUBLISH', response);
208 return this.uploadFileToBlueprintController(file, '/blueprint-model/publish/', response);
213 // return new Promise((resolve, reject) => {
214 // this.getFileFromMultiPartForm(request).then(file => {
215 // this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
226 @post('/controllerblueprint/enrich-blueprint')
229 description: 'multipart/form-data value.',
232 'multipart/form-data': {
234 'x-parser': 'stream',
235 schema: { type: 'object' },
240 @inject(RestBindings.Http.RESPONSE) response: Response,
241 ): Promise<Response> {
242 return new Promise((resolve, reject) => {
243 this.getFileFromMultiPartForm(request).then(file => {
244 if (appConfig.action.grpcEnabled)
245 return this.uploadFileToBlueprintProcessorGrpc(file, 'ENRICH', response);
247 return this.uploadFileToBlueprintController(file, '/blueprint-model/enrich/', response);
248 // this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
259 @post('/controllerblueprint/enrichandpublish')
260 async enrichAndPublish(
262 description: 'multipart/form-data value.',
265 'multipart/form-data': {
267 'x-parser': 'stream',
268 schema: { type: 'object' },
273 @inject(RestBindings.Http.RESPONSE) response: Response,
274 ): Promise<Response> {
275 return new Promise((resolve, reject) => {
276 this.getFileFromMultiPartForm(request).then(file => {
277 if (appConfig.action.grpcEnabled)
278 return this.uploadFileToBlueprintProcessorGrpc(file, 'ENRICH', response);
280 return this.uploadFileToBlueprintController(file, '/blueprint-model/enrichandpublish/', response);
285 @get('/controllerblueprint/download-blueprint/{name}/{version}')
287 @param.path.string('name') name: string,
288 @param.path.string('version') version: string,
289 @inject(RestBindings.Http.RESPONSE) response: Response,
290 ): Promise<Response> {
292 if (appConfig.action.grpcEnabled)
293 return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
295 return this.downloadFileFromBlueprintController('/blueprint-model/download/by-name/' + name + '/version/' + version, response);
299 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
300 return new Promise((resolve, reject) => {
301 let form = new multiparty.Form();
302 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
303 if (err) reject(err);
304 let file = files['file'][0]; // get the file from the returned files object
306 reject('File was not found in form data.');
314 @post('/controllerblueprint/deploy-blueprint')
317 description: 'multipart/form-data value.',
320 'multipart/form-data': {
322 'x-parser': 'stream',
323 schema: { type: 'object' },
328 @inject(RestBindings.Http.RESPONSE) response: Response,
329 ): Promise<Response> {
330 return new Promise((resolve, reject) => {
331 this.getFileFromMultiPartForm(request).then(file => {
332 // if (appConfig.action.deployBlueprint.grpcEnabled)
333 if (appConfig.action.grpcEnabled)
334 return this.uploadFileToBlueprintProcessorGrpc(file, 'PUBLISH', response);
336 return this.uploadFileToBlueprintProcessor(file, '/blueprint-model/publish', response);
343 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
344 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
347 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
348 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
351 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
355 Authorization: authToken,
356 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
360 value: fs.createReadStream(file.path),
363 contentType: 'application/zip',
369 var removeTempFile = () => {
370 fs.unlink(file.path, (err: any) => {
377 return new Promise((resolve, reject) => {
378 request_lib.post(options)
379 .on('error', err => {
383 .once('finish', () => {
390 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
391 return this.downloadFileFromBlueprintService(processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
394 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
398 Authorization: authToken,
401 return new Promise((resolve, reject) => {
402 request_lib.get(options)
403 .on('error', err => {
407 .once('finish', () => {
413 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
414 return new Promise<Response>((resolve, reject) => {
415 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
416 response.send(output.status.message);
419 response.status(500).send(err);
425 async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
426 return new Promise<Response>((resolve, reject) => {
427 bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
429 response.send(output.status.message);
432 response.status(500).send(err);