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.getPagedBueprints(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 @get('/controllerblueprint/download-blueprint/{name}/{version}')
261 @param.path.string('name') name: string,
262 @param.path.string('version') version: string,
263 @inject(RestBindings.Http.RESPONSE) response: Response,
264 ): Promise<Response> {
266 if (appConfig.action.grpcEnabled)
267 return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
269 return this.downloadFileFromBlueprintController('/blueprint-model/download/by-name/' + name + '/version/' + version, response);
273 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
274 return new Promise((resolve, reject) => {
275 let form = new multiparty.Form();
276 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
277 if (err) reject(err);
278 let file = files['file'][0]; // get the file from the returned files object
280 reject('File was not found in form data.');
288 @post('/controllerblueprint/deploy-blueprint')
291 description: 'multipart/form-data value.',
294 'multipart/form-data': {
296 'x-parser': 'stream',
297 schema: { type: 'object' },
302 @inject(RestBindings.Http.RESPONSE) response: Response,
303 ): Promise<Response> {
304 return new Promise((resolve, reject) => {
305 this.getFileFromMultiPartForm(request).then(file => {
306 // if (appConfig.action.deployBlueprint.grpcEnabled)
307 if (appConfig.action.grpcEnabled)
308 return this.uploadFileToBlueprintProcessorGrpc(file, 'PUBLISH', response);
310 return this.uploadFileToBlueprintProcessor(file, '/blueprint-model/publish', response);
317 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
318 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
321 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
322 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
325 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
329 Authorization: authToken,
330 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
334 value: fs.createReadStream(file.path),
337 contentType: 'application/zip',
343 var removeTempFile = () => {
344 fs.unlink(file.path, (err: any) => {
351 return new Promise((resolve, reject) => {
352 request_lib.post(options)
353 .on('error', err => {
357 .once('finish', () => {
364 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
365 return this.downloadFileFromBlueprintService(processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
368 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
372 Authorization: authToken,
375 return new Promise((resolve, reject) => {
376 request_lib.get(options)
377 .on('error', err => {
381 .once('finish', () => {
387 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
388 return new Promise<Response>((resolve, reject) => {
389 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
390 response.send(output.status.message);
393 response.status(500).send(err);
399 async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
400 return new Promise<Response>((resolve, reject) => {
401 bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
403 response.send(output.status.message);
406 response.status(500).send(err);