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============================================
29 } from '@loopback/repository';
43 } from '@loopback/rest';
44 import {Blueprint} from '../models';
45 import {inject} from '@loopback/core';
46 import {BlueprintService} from '../services';
47 import * as fs from 'fs';
48 import * as multiparty from 'multiparty';
49 import * as request_lib from 'request';
50 import {processorApiConfig, appConfig} from '../config/app-config';
51 import {bluePrintManagementServiceGrpcClient} from '../clients/blueprint-management-service-grpc-client';
52 import {BlueprintDetail} from '../models/blueprint.detail.model';
54 export class BlueprintRestController {
56 @inject('services.BlueprintService')
57 public bpservice: BlueprintService,
61 @get('/controllerblueprint/all', {
64 description: 'Blueprint model instance',
65 content: {'application/json': {schema: {'x-ts-type': Blueprint}}},
70 return await this.bpservice.getAllblueprints();
73 @get('/controllerblueprint/{id}', {
76 description: 'Blueprint model instance',
77 content: {'application/json': {schema: {'x-ts-type': BlueprintDetail}}},
81 async getOneBluePrint(@param.path.string('id') id: string) {
82 return await this.bpservice.getOneBluePrint(id);
86 @get('/controllerblueprint/paged', {
89 description: 'Blueprint model instance with pagination',
90 content: {'application/json': {schema: {'x-ts-type': Blueprint}}},
94 async getPagedBlueprints(
95 @param.query.number('limit') limit: number,
96 @param.query.number('offset') offset: number,
97 @param.query.string('sort') sort: string) {
98 return await this.bpservice.getPagedBueprints(limit, offset, sort);
101 @get('/controllerblueprint/metadata/paged/{keyword}', {
104 description: 'Blueprint model instance with pagination',
105 content: {'application/json': {schema: {'x-ts-type': Blueprint}}},
109 async getMetaDataPagedBlueprints(
110 @param.path.string('keyword') keyword: string,
111 @param.query.number('limit') limit: number,
112 @param.query.number('offset') offset: number,
113 @param.query.string('sort') sort: string) {
114 return await this.bpservice.getMetaDataPagedBlueprints(limit, offset, sort, keyword);
117 @get('/controllerblueprint/meta-data/{keyword}', {
120 description: 'Blueprint model instance',
121 content: {'application/json': {schema: {'x-ts-type': Blueprint}}},
125 async getAllPacakgesByKeword(@param.path.string('keyword') keyword: string) {
126 return await this.bpservice.getBlueprintsByKeyword(keyword);
129 @get('/controllerblueprint/by-name/{name}/version/{version}', {
132 description: 'Blueprint model instance',
133 content: {'application/json': {schema: {'x-ts-type': Blueprint}}},
137 async getPacakgesByNameAndVersion(@param.path.string('name') name: string, @param.path.string('version') version: string) {
138 return await this.bpservice.getBlueprintByNameAndVersion(name, version);
141 @get('/controllerblueprint/searchByTags/{tags}', {
144 content: {'application/json': {}},
148 async getByTags(@param.path.string('tags') tags: string) {
149 return await this.bpservice.getByTags(tags);
152 @post('/controllerblueprint/create-blueprint')
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, 'DRAFT', response);
174 return this.uploadFileToBlueprintController(file, '/blueprint-model/', response);
179 // return new Promise((resolve, reject) => {
180 // this.getFileFromMultiPartForm(request).then(file => {
181 // this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
192 @post('/controllerblueprint/publish')
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.deployBlueprint.grpcEnabled)
211 if (appConfig.action.grpcEnabled)
212 return this.uploadFileToBlueprintProcessorGrpc(file, 'PUBLISH', response);
214 return this.uploadFileToBlueprintController(file, '/blueprint-model/publish/', response);
219 // return new Promise((resolve, reject) => {
220 // this.getFileFromMultiPartForm(request).then(file => {
221 // this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
232 @post('/controllerblueprint/enrich-blueprint')
235 description: 'multipart/form-data value.',
238 'multipart/form-data': {
240 'x-parser': 'stream',
241 schema: {type: 'object'},
246 @inject(RestBindings.Http.RESPONSE) response: Response,
247 ): Promise<Response> {
248 return new Promise((resolve, reject) => {
249 this.getFileFromMultiPartForm(request).then(file => {
250 if (appConfig.action.grpcEnabled)
251 return this.uploadFileToBlueprintProcessorGrpc(file, 'ENRICH', response);
253 return this.uploadFileToBlueprintController(file, '/blueprint-model/enrich/', response);
254 // this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
265 @get('/controllerblueprint/download-blueprint/{name}/{version}')
267 @param.path.string('name') name: string,
268 @param.path.string('version') version: string,
269 @inject(RestBindings.Http.RESPONSE) response: Response,
270 ): Promise<Response> {
272 if (appConfig.action.grpcEnabled)
273 return this.downloadFileFromBlueprintProcessorGrpc(name, version, response);
275 return this.downloadFileFromBlueprintController('/blueprint-model/download/by-name/' + name + '/version/' + version, response);
279 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
280 return new Promise((resolve, reject) => {
281 let form = new multiparty.Form();
282 form.parse(request, (err: any, fields: any, files: {[x: string]: any[];}) => {
283 if (err) reject(err);
284 let file = files['file'][0]; // get the file from the returned files object
286 reject('File was not found in form data.');
294 @post('/controllerblueprint/deploy-blueprint')
297 description: 'multipart/form-data value.',
300 'multipart/form-data': {
302 'x-parser': 'stream',
303 schema: {type: 'object'},
308 @inject(RestBindings.Http.RESPONSE) response: Response,
309 ): Promise<Response> {
310 return new Promise((resolve, reject) => {
311 this.getFileFromMultiPartForm(request).then(file => {
312 // if (appConfig.action.deployBlueprint.grpcEnabled)
313 if (appConfig.action.grpcEnabled)
314 return this.uploadFileToBlueprintProcessorGrpc(file, 'PUBLISH', response);
316 return this.uploadFileToBlueprintProcessor(file, '/execution-service/upload/', response);
323 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
324 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
327 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
328 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
331 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
335 Authorization: authToken,
336 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
340 value: fs.createReadStream(file.path),
343 contentType: 'application/zip',
349 var removeTempFile = () => {
350 fs.unlink(file.path, (err: any) => {
357 return new Promise((resolve, reject) => {
358 request_lib.post(options)
359 .on('error', err => {
363 .once('finish', () => {
370 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
371 return this.downloadFileFromBlueprintService(processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
374 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
378 Authorization: authToken,
381 return new Promise((resolve, reject) => {
382 request_lib.get(options)
383 .on('error', err => {
387 .once('finish', () => {
393 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, actionName: string, response: Response): Promise<Response> {
394 return new Promise<Response>((resolve, reject) => {
395 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path, actionName).then(output => {
396 response.send(output.status.message);
399 response.status(500).send(err);
405 async downloadFileFromBlueprintProcessorGrpc(blueprintName: string, blueprintVersion: string, response: Response): Promise<Response> {
406 return new Promise<Response>((resolve, reject) => {
407 bluePrintManagementServiceGrpcClient.downloadBlueprint(blueprintName, blueprintVersion)
409 response.send(output.status.message);
412 response.status(500).send(err);