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} from '../../config/app-config';
53 export class BlueprintRestController {
55 @inject('services.BlueprintService')
56 public bpservice: BlueprintService,
62 description: 'Blueprint model instance',
63 content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } },
68 return await this.bpservice.getAllblueprints();
71 @post('/create-blueprint')
74 description: 'multipart/form-data value.',
77 'multipart/form-data': {
80 schema: {type: 'object'},
85 @inject(RestBindings.Http.RESPONSE) response: Response,
86 ): Promise<Response> {
87 return new Promise((resolve, reject) => {
88 this.getFileFromMultiPartForm(request).then(file=>{
89 this.uploadFileToBlueprintController(file, "/blueprint-model/", response).then(resp=>{
103 description: 'multipart/form-data value.',
106 'multipart/form-data': {
108 'x-parser': 'stream',
109 schema: {type: 'object'},
114 @inject(RestBindings.Http.RESPONSE) response: Response,
115 ): Promise<Response> {
116 return new Promise((resolve, reject) => {
117 this.getFileFromMultiPartForm(request).then(file=>{
118 this.uploadFileToBlueprintController(file, "/blueprint-model/publish/", response).then(resp=>{
129 @post('/enrich-blueprint')
132 description: 'multipart/form-data value.',
135 'multipart/form-data': {
137 'x-parser': 'stream',
138 schema: {type: 'object'},
143 @inject(RestBindings.Http.RESPONSE) response: Response,
144 ): Promise<Response> {
145 return new Promise((resolve, reject) => {
146 this.getFileFromMultiPartForm(request).then(file=>{
147 this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/", response).then(resp=>{
158 @get('/download-blueprint/{name}/{version}')
160 @param.path.string('name') name: string,
161 @param.path.string('version') version:string,
162 @inject(RestBindings.Http.RESPONSE) response: Response,
163 ): Promise<Response> {
164 return this.downloadFileFromBlueprintController("/blueprint-model/download/by-name/"+name+"/version/"+version, response);
167 async getFileFromMultiPartForm(request: Request): Promise<multiparty.File>{
168 return new Promise((resolve, reject) => {
169 let form = new multiparty.Form();
170 form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => {
171 if (err) reject(err);
172 let file = files['file'][0]; // get the file from the returned files object
174 reject('File was not found in form data.');
182 @post('/deploy-blueprint')
185 description: 'multipart/form-data value.',
188 'multipart/form-data': {
190 'x-parser': 'stream',
191 schema: {type: 'object'},
196 @inject(RestBindings.Http.RESPONSE) response: Response,
197 ): Promise<Response> {
198 return new Promise((resolve, reject) => {
199 this.getFileFromMultiPartForm(request).then(file=>{
200 this.uploadFileToBlueprintProcessor(file, "/execution-service/upload/", response).then(resp=>{
211 async uploadFileToBlueprintController(file: multiparty.File, uri: string, response: Response): Promise<Response>{
212 return this.uploadFileToBlueprintService(file, controllerApiConfig.url + uri, controllerApiConfig.authToken, response);
215 async uploadFileToBlueprintProcessor(file: multiparty.File, uri: string, response: Response): Promise<Response>{
216 return this.uploadFileToBlueprintService(file, processorApiConfig.url + uri, processorApiConfig.authToken, response);
219 async uploadFileToBlueprintService(file: multiparty.File, url: string, authToken: string, response: Response): Promise<Response>{
223 Authorization: authToken,
224 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
228 value: fs.createReadStream(file.path),
231 contentType: 'application/zip'
237 var removeTempFile = () => {
238 fs.unlink(file.path, (err: any) => {
245 return new Promise((resolve, reject) => {
246 request_lib.post(options)
247 .on("error", err => {
251 .once("finish", () => {
258 async downloadFileFromBlueprintController(uri: string, response: Response): Promise<Response> {
259 return this.downloadFileFromBlueprintService(controllerApiConfig.url + uri, controllerApiConfig.authToken, response);
262 async downloadFileFromBlueprintService(url: string, authToken: string, response: Response): Promise<Response> {
266 Authorization: authToken,
269 return new Promise((resolve, reject) => {
270 request_lib.get(options)
271 .on("error", err => {
275 .once("finish", () => {