diff options
author | Ezhilarasi <ezhrajam@in.ibm.com> | 2019-04-04 21:06:09 +0530 |
---|---|---|
committer | Ezhilarasi R <ezhrajam@in.ibm.com> | 2019-04-05 07:10:12 +0000 |
commit | e4b82f76f386a1aad395951e5c5d2cf2ba688c6b (patch) | |
tree | 736ab254e6053aeb1b0eeb0ccd86f4eae10f442c /cds-ui/server/src | |
parent | 41af46c85eafe85b20833c99def94ab530afa01b (diff) |
Service for get and post Blueprint
Change-Id: Ie99f2ae09e65754e5da5384501f4f8574399da42
Issue-ID: CCSDK-1068
Signed-off-by: Ezhilarasi <ezhrajam@in.ibm.com>
Diffstat (limited to 'cds-ui/server/src')
-rw-r--r-- | cds-ui/server/src/controllers/blueprint-rest.controller.ts | 244 | ||||
-rw-r--r-- | cds-ui/server/src/datasources/blueprint.datasource.json | 22 | ||||
-rw-r--r-- | cds-ui/server/src/datasources/blueprint.datasource.ts | 14 | ||||
-rw-r--r-- | cds-ui/server/src/datasources/index.ts | 1 | ||||
-rw-r--r-- | cds-ui/server/src/datasources/rest.datasource.json (renamed from cds-ui/server/src/datasources/rest.config.json) | 0 | ||||
-rw-r--r-- | cds-ui/server/src/datasources/rest.datasource.ts | 2 | ||||
-rw-r--r-- | cds-ui/server/src/services/blueprint.service.ts | 19 | ||||
-rw-r--r-- | cds-ui/server/src/services/index.ts | 1 |
8 files changed, 213 insertions, 90 deletions
diff --git a/cds-ui/server/src/controllers/blueprint-rest.controller.ts b/cds-ui/server/src/controllers/blueprint-rest.controller.ts index 960f09fbe..ae028afc3 100644 --- a/cds-ui/server/src/controllers/blueprint-rest.controller.ts +++ b/cds-ui/server/src/controllers/blueprint-rest.controller.ts @@ -38,123 +38,189 @@ import { put, del, requestBody, + Request, + Response, + RestBindings, } from '@loopback/rest'; import {Blueprint} from '../models'; -import {BlueprintRepository} from '../repositories'; +import { inject } from '@loopback/core'; +import { BlueprintService } from '../services'; +import * as fs from 'fs'; +import * as multiparty from 'multiparty'; +import * as request_lib from 'request'; + +const REST_BLUEPRINT_CONTROLLER_BASE_URL = process.env.REST_BLUEPRINT_CONTROLLER_BASE_URL || "http://localhost:8080/api/v1"; +const REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER = process.env.REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER || "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw=="; export class BlueprintRestController { constructor( - @repository(BlueprintRepository) - public blueprintRepository : BlueprintRepository, + // @repository(BlueprintRepository) + // public blueprintRepository : BlueprintRepository, + @inject('services.BlueprintService') + public bpservice: BlueprintService, ) {} - @post('/blueprints', { + @get('/blueprints', { responses: { '200': { description: 'Blueprint model instance', - content: {'application/json': {schema: {'x-ts-type': Blueprint}}}, + content: { 'application/json': { schema: { 'x-ts-type': Blueprint } } }, }, }, }) - async create(@requestBody() blueprint: Blueprint): Promise<Blueprint> { - return await this.blueprintRepository.create(blueprint); - } + async getall() { + return await this.bpservice.getAllblueprints(REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER); - @get('/blueprints/count', { - responses: { - '200': { - description: 'Blueprint model count', - content: {'application/json': {schema: CountSchema}}, - }, - }, - }) - async count( - @param.query.object('where', getWhereSchemaFor(Blueprint)) where?: Where, - ): Promise<Count> { - return await this.blueprintRepository.count(where); } - @get('/blueprints', { - responses: { - '200': { - description: 'Array of Blueprint model instances', - content: { - 'application/json': { - schema: {type: 'array', items: {'x-ts-type': Blueprint}}, - }, + @post('/create-blueprint') + async upload( + @requestBody({ + description: 'multipart/form-data value.', + required: true, + content: { + 'multipart/form-data': { + // Skip body parsing + 'x-parser': 'stream', + schema: {type: 'object'}, }, }, - }, - }) - async find( - @param.query.object('filter', getFilterSchemaFor(Blueprint)) filter?: Filter, - ): Promise<Blueprint[]> { - return await this.blueprintRepository.find(filter); + }) + request: Request, + @inject(RestBindings.Http.RESPONSE) response: Response, + ): Promise<object> { + return new Promise((resolve, reject) => { + this.getFileFromMultiPartForm(request).then(file=>{ + this.uploadFileToBlueprintController(file, "/blueprint-model/").then(resp=>{ + response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']); + resolve(JSON.parse(resp.body)); + }, err=>{ + reject(err); + }); + }, err=>{ + reject(err); + }); + }); } - @patch('/blueprints', { - responses: { - '200': { - description: 'Blueprint PATCH success count', - content: {'application/json': {schema: CountSchema}}, + @post('/enrich-blueprint') + async enrich( + @requestBody({ + description: 'multipart/form-data value.', + required: true, + content: { + 'multipart/form-data': { + // Skip body parsing + 'x-parser': 'stream', + schema: {type: 'object'}, + }, }, - }, - }) - async updateAll( - @requestBody() blueprint: Blueprint, - @param.query.object('where', getWhereSchemaFor(Blueprint)) where?: Where, - ): Promise<Count> { - return await this.blueprintRepository.updateAll(blueprint, where); + }) + request: Request, + @inject(RestBindings.Http.RESPONSE) response: Response, + ): Promise<any> { + return new Promise((resolve, reject) => { + this.getFileFromMultiPartForm(request).then(file=>{ + this.uploadFileToBlueprintController(file, "/blueprint-model/enrich/").then(resp=>{ + response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']); + response.setHeader("Content-Disposition", resp.headers['content-disposition']); + resolve(resp.body); + }, err=>{ + reject(err); + }); + }, err=>{ + reject(err); + }); + }); } - @get('/blueprints/{id}', { - responses: { - '200': { - description: 'Blueprint model instance', - content: {'application/json': {schema: {'x-ts-type': Blueprint}}}, - }, - }, - }) - async findById(@param.path.number('id') id: number): Promise<Blueprint> { - return await this.blueprintRepository.findById(id); + @get('/download-blueprint/{id}') + async download( + @param.path.string('id') id: string, + @inject(RestBindings.Http.REQUEST) request: Request, + @inject(RestBindings.Http.RESPONSE) response: Response, + ): Promise<any> { + return new Promise((resolve, reject) => { + this.downloadFileFromBlueprintController("/blueprint-model/download/" + id).then(resp=>{ + response.setHeader("X-ONAP-RequestID", resp.headers['x-onap-requestid']); + response.setHeader("Content-Disposition", resp.headers['content-disposition']); + resolve(resp.body); + }, err=>{ + reject(err); + }); + }); } - @patch('/blueprints/{id}', { - responses: { - '204': { - description: 'Blueprint PATCH success', - }, - }, - }) - async updateById( - @param.path.number('id') id: number, - @requestBody() blueprint: Blueprint, - ): Promise<void> { - await this.blueprintRepository.updateById(id, blueprint); + async getFileFromMultiPartForm(request: Request): Promise<any>{ + return new Promise((resolve, reject) => { + // let options = { + // uploadDir: MULTIPART_FORM_UPLOAD_DIR + // } + let form = new multiparty.Form(); + form.parse(request, (err: any, fields: any, files: { [x: string]: any[]; }) => { + if (err) reject(err); + let file = files['file'][0]; // get the file from the returned files object + if(!file){ + reject('File was not found in form data.'); + }else{ + resolve(file); + } + }); + }) } - @put('/blueprints/{id}', { - responses: { - '204': { - description: 'Blueprint PUT success', + async uploadFileToBlueprintController(file: any, uri: string): Promise<any>{ + let options = { + url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri, + headers: { + Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER, + 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' }, - }, - }) - async replaceById( - @param.path.number('id') id: number, - @requestBody() blueprint: Blueprint, - ): Promise<void> { - await this.blueprintRepository.replaceById(id, blueprint); - } + formData: { + file: { + value: fs.createReadStream(file.path), + options: { + filename: 'cba.zip', + contentType: 'application/zip' + } + } + } + }; - @del('/blueprints/{id}', { - responses: { - '204': { - description: 'Blueprint DELETE success', - }, - }, - }) - async deleteById(@param.path.number('id') id: number): Promise<void> { - await this.blueprintRepository.deleteById(id); + return new Promise((resolve, reject) => { + request_lib.post(options, (err: any, resp: any, body: any) => { + if (err) { + //delete tmp file + fs.unlink(file.path, (err: any) => { + if (err) { + console.error(err); + return + } + }) + reject(err); + }else{ + resolve(resp); + } + }) + }) } + + async downloadFileFromBlueprintController(uri: string): Promise<any> { + let options = { + url: REST_BLUEPRINT_CONTROLLER_BASE_URL + uri, + headers: { + Authorization: REST_BLUEPRINT_CONTROLLER_BASIC_AUTH_HEADER, + } + }; + + return new Promise((resolve, reject) => { + request_lib.get(options, (err: any, resp: any, body: any) => { + if (err) { + reject(err); + }else{ + resolve(resp); + } + }) + }) } +}
\ No newline at end of file diff --git a/cds-ui/server/src/datasources/blueprint.datasource.json b/cds-ui/server/src/datasources/blueprint.datasource.json new file mode 100644 index 000000000..86b1c2c15 --- /dev/null +++ b/cds-ui/server/src/datasources/blueprint.datasource.json @@ -0,0 +1,22 @@ +{ + "name": "blueprint", + "connector": "rest", + "baseURL": "http://localhost:8080/api/v1/", + "crud": false, + "operations": [{ + "template": { + "method": "GET", + "url": "http://localhost:8080/api/v1/blueprint-model/", + "headers": { + "accepts": "application/json", + "content-type": "application/json", + "authorization": "{authtoken}" + }, + "responsePath": "$.*" + }, + "functions": { + "getAllblueprints": ["authtoken"] + + } + }] +}
\ No newline at end of file diff --git a/cds-ui/server/src/datasources/blueprint.datasource.ts b/cds-ui/server/src/datasources/blueprint.datasource.ts new file mode 100644 index 000000000..008612693 --- /dev/null +++ b/cds-ui/server/src/datasources/blueprint.datasource.ts @@ -0,0 +1,14 @@ +import {inject} from '@loopback/core'; +import {juggler} from '@loopback/repository'; +import * as config from './blueprint.datasource.json'; + +export class BlueprintDataSource extends juggler.DataSource { + static dataSourceName = 'blueprint'; + + constructor( + @inject('datasources.config.blueprint', {optional: true}) + dsConfig: object = config, + ) { + super(dsConfig); + } +} diff --git a/cds-ui/server/src/datasources/index.ts b/cds-ui/server/src/datasources/index.ts index 1726b4515..f7a934cd8 100644 --- a/cds-ui/server/src/datasources/index.ts +++ b/cds-ui/server/src/datasources/index.ts @@ -21,3 +21,4 @@ limitations under the License. export * from './db.datasource'; +export * from './blueprint.datasource'; diff --git a/cds-ui/server/src/datasources/rest.config.json b/cds-ui/server/src/datasources/rest.datasource.json index 86f33fbd4..86f33fbd4 100644 --- a/cds-ui/server/src/datasources/rest.config.json +++ b/cds-ui/server/src/datasources/rest.datasource.json diff --git a/cds-ui/server/src/datasources/rest.datasource.ts b/cds-ui/server/src/datasources/rest.datasource.ts index 42cdb8018..1b5a44d79 100644 --- a/cds-ui/server/src/datasources/rest.datasource.ts +++ b/cds-ui/server/src/datasources/rest.datasource.ts @@ -21,7 +21,7 @@ limitations under the License. import { juggler } from '@loopback/service-proxy'; -import * as config from './rest.config.json'; +import * as config from './rest.datasource.json'; export class RestDataSource extends juggler.DataSource { diff --git a/cds-ui/server/src/services/blueprint.service.ts b/cds-ui/server/src/services/blueprint.service.ts new file mode 100644 index 000000000..f48253652 --- /dev/null +++ b/cds-ui/server/src/services/blueprint.service.ts @@ -0,0 +1,19 @@ +import {getService} from '@loopback/service-proxy'; +import {inject, Provider} from '@loopback/core'; +import {BlueprintDataSource} from '../datasources'; + +export interface BlueprintService { + getAllblueprints(authtoken: string): Promise<any>; +} + +export class BlueprintServiceProvider implements Provider<BlueprintService> { + constructor( + // blueprint must match the name property in the datasource json file + @inject('datasources.blueprint') + protected dataSource: BlueprintDataSource = new BlueprintDataSource(), + ) {} + + value(): Promise<BlueprintService> { + return getService(this.dataSource); + } +}
\ No newline at end of file diff --git a/cds-ui/server/src/services/index.ts b/cds-ui/server/src/services/index.ts new file mode 100644 index 000000000..26b01ed0b --- /dev/null +++ b/cds-ui/server/src/services/index.ts @@ -0,0 +1 @@ +export * from './blueprint.service'; |