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 { controllerApiConfig, 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/searchByTags/{tags}', {
75 content: { 'application/json': {} },
79 async getByTags(@param.path.string('tags') tags: string) {
80 return await this.bpservice.getByTags(tags);
83 @post('/controllerblueprint/create-blueprint')
86 description: 'multipart/form-data value.',
89 'multipart/form-data': {
92 schema: { type: 'object' },
97 @inject(RestBindings.Http.RESPONSE) response: Response,
98 ): Promise<Response> {
99 return new Promise((resolve, reject) => {
100 this.getFileFromMultiPartForm(request).then(file => {
101 this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp => {
112 @post('/controllerblueprint/publish')
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 this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp => {
141 @post('/controllerblueprint/enrich-blueprint')
144 description: 'multipart/form-data value.',
147 'multipart/form-data': {
149 'x-parser': 'stream',
150 schema: { type: 'object' },
155 @inject(RestBindings.Http.RESPONSE) response: Response,
156 ): Promise<Response> {
157 return new Promise((resolve, reject) => {
158 this.getFileFromMultiPartForm(request).then(file => {
159 this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp => {
170 @get('/controllerblueprint/download-blueprint/{name}/{version}')
172 @param.path.string('name') name: string,
173 @param.path.string('version') version: string,
174 @inject(RestBindings.Http.RESPONSE) response: Response,
175 ): Promise<Response> {
176 return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/" + name + "/version/" + version, response);
179 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File> {
180 return new Promise((resolve, reject) => {
181 let form = new multiparty.Form();
182 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
183 if (err) reject(err);
184 let file = files['file'][0]; // get the file from the returned files object
186 reject('File was not found in form data.');
194 @post('/controllerblueprint/deploy-blueprint')
197 description: 'multipart/form-data value.',
200 'multipart/form-data': {
202 'x-parser': 'stream',
203 schema: { type: 'object' },
208 @inject(RestBindings.Http.RESPONSE) response: Response,
209 ): Promise<Response> {
210 return new Promise((resolve, reject) => {
211 this.getFileFromMultiPartForm(request).then(file => {
212 if (appConfig.action.deployBlueprint.grpcEnabled)
213 return this.uploadFileToBlueprintProcessorGrpc(file, response);
215 return this.uploadFileToBlueprintProcessor(file, "/execution-service/upload/", response);
222 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response> {
223 return this.uploadFileToBlueprintService(file, controllerApiConfig.http.url + uri, controllerApiConfig.http.authToken, response);
226 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response> {
227 return this.uploadFileToBlueprintService(file, processorApiConfig.http.url + uri, processorApiConfig.http.authToken, response);
230 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response> {
234 Authorization: authToken,
235 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
239 value: fs.createReadStream(file.path),
242 contentType: 'application/zip'
248 var removeTempFile = () => {
249 fs.unlink(file.path, (err: any) => {
256 return new Promise((resolve, reject) => {
257 request_lib.post(options)
258 .on("error", err => {
262 .once("finish", () => {
269 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
270 return this.downloadFileFromBlueprintService(controllerApiConfig.http.url + uri, controllerApiConfig.http.authToken, response);
273 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
277 Authorization: authToken,
280 return new Promise((resolve, reject) => {
281 request_lib.get(options)
282 .on("error", err => {
286 .once("finish", () => {
292 async uploadFileToBlueprintProcessorGrpc(file: multiparty.File, response: Response): Promise<Response> {
293 return new Promise<Response>((resolve, reject) => {
294 bluePrintManagementServiceGrpcClient.uploadBlueprint(file.path).then(output => {
295 response.send(output.status.message);
298 response.status(500).send(err);