From e4df686d0a75f68183f594363253f059d1ed3a2b Mon Sep 17 00:00:00 2001 From: Laurence Maultsby Date: Tue, 17 Jan 2017 09:37:31 -0500 Subject: [PATCH] Upload and Update status and endpoints updated Signed-off-by: Laurence Maultsby --- skyquake/plugins/composer/api/composer.js | 111 ++++++++++++++---- .../composer/api/packageFileHandler.js | 13 +- skyquake/plugins/composer/routes.js | 9 ++ .../CatalogPackageManagerUploadDropZone.js | 2 +- .../src/stores/CatalogPackageManagerStore.js | 9 +- 5 files changed, 108 insertions(+), 36 deletions(-) diff --git a/skyquake/plugins/composer/api/composer.js b/skyquake/plugins/composer/api/composer.js index ce128609c..8df1a9123 100644 --- a/skyquake/plugins/composer/api/composer.js +++ b/skyquake/plugins/composer/api/composer.js @@ -272,33 +272,89 @@ Composer.create = function(req) { }); }); }; +// Composer.update = function(req) { +// var api_server = req.query['api_server']; +// var catalogType = req.params.catalogType; +// var id = req.params.id; +// var data = req.body; +// console.log('Updating', catalogType, 'id', id, 'on', api_server); +// var jsonData = {}; +// jsonData[catalogType] = {}; +// jsonData[catalogType] = data; +// return new Promise(function(resolve, reject) { +// var requestHeaders = {}; +// _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, { +// 'Authorization': req.get('Authorization') +// }); +// request({ +// uri: utils.confdPort(api_server) + APIVersion + '/api/config/' + catalogType + '-catalog' + '/' + catalogType + '/' + id, +// method: 'PUT', +// headers: requestHeaders, +// forever: constants.FOREVER_ON, +// rejectUnauthorized: false, +// json: jsonData +// }, function(error, response, body) { +// if (utils.validateResponse('Composer.update', error, response, body, resolve, reject)) { +// resolve({ +// statusCode: response.statusCode +// }); +// } +// }); +// }); +// + Composer.update = function(req) { + console.log(' Updating file', req.file.originalname, 'as', req.file.filename); var api_server = req.query['api_server']; - var catalogType = req.params.catalogType; - var id = req.params.id; - var data = req.body; - console.log('Updating', catalogType, 'id', id, 'on', api_server); - var jsonData = {}; - jsonData[catalogType] = {}; - jsonData[catalogType] = data; + // dev_download_server is for testing purposes. + // It is the direct IP address of the Node server where the + // package will be hosted. + var download_host = req.query['dev_download_server']; + + if (!download_host) { + download_host = req.protocol + '://' + req.headers.host; + } + var input = { + 'external-url': download_host + '/composer/update/' + req.file.filename, + 'package-type': 'VNFD', + 'package-id': uuid() + } return new Promise(function(resolve, reject) { - var requestHeaders = {}; - _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, { - 'Authorization': req.get('Authorization') - }); - request({ - uri: utils.confdPort(api_server) + APIVersion + '/api/config/' + catalogType + '-catalog' + '/' + catalogType + '/' + id, - method: 'PUT', - headers: requestHeaders, - forever: constants.FOREVER_ON, - rejectUnauthorized: false, - json: jsonData - }, function(error, response, body) { - if (utils.validateResponse('Composer.update', error, response, body, resolve, reject)) { - resolve({ - statusCode: response.statusCode - }); - } + Promise.all([ + rp({ + uri: utils.confdPort(api_server) + '/api/operations/package-update', + method: 'POST', + headers: _.extend({}, constants.HTTP_HEADERS.accept.collection, { + 'Authorization': req.get('Authorization') + }), + forever: constants.FOREVER_ON, + rejectUnauthorized: false, + resolveWithFullResponse: true, + json: true, + body: { + input: input + } + }) + ]).then(function(result) { + var data = {}; + data['transaction_id'] = result[0].body['output']['transaction-id']; + + // Add a status checker on the transaction and then to delete the file later + PackageFileHandler.checkCreatePackageStatusAndHandleFile(req, data['transaction_id'], true); + + // Return status to composer UI to update the status. + resolve({ + statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK, + data: data + }); + }).catch(function(error) { + var res = {}; + console.log('Problem with Composer.upload', error); + res.statusCode = error.statusCode || 500; + res.errorMessage = { + error: 'Failed to upload package ' + req.file.originalname + '. Error: ' + error + }; + reject(res); }); }); }; @@ -341,7 +397,7 @@ Composer.upload = function(req) { // Add a status checker on the transaction and then to delete the file later PackageFileHandler.checkCreatePackageStatusAndHandleFile(req, data['transaction_id']); - + // Return status to composer UI to update the status. resolve({ statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK, @@ -358,6 +414,9 @@ Composer.upload = function(req) { }); }); }; + + + Composer.addFile = function(req) { console.log(' Uploading file', req.file.originalname, 'as', req.file.filename); var api_server = req.query['api_server']; @@ -392,7 +451,7 @@ Composer.addFile = function(req) { }) ]).then(function(result) { var data = {}; - data['transaction_id'] = result[0].body['output']['transaction-id']; + data['transaction_id'] = result[0].body['output']['task-id']; resolve({ statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK, data: data diff --git a/skyquake/plugins/composer/api/packageFileHandler.js b/skyquake/plugins/composer/api/packageFileHandler.js index f199610a7..cc9cca7c3 100644 --- a/skyquake/plugins/composer/api/packageFileHandler.js +++ b/skyquake/plugins/composer/api/packageFileHandler.js @@ -14,15 +14,16 @@ function deleteFile(filename) { }, constants.PACKAGE_FILE_DELETE_DELAY_MILLISECONDS); }; -function checkStatus(req, transactionId) { +function checkStatus(req, transactionId, isUpdate) { var upload_server = req.query['upload_server']; var headers = _.extend({}, { 'Authorization': req.get('Authorization') } ); + var type = isUpdate ? 'update' : 'upload'; request({ - url: upload_server + ':' + constants.PACKAGE_MANAGER_SERVER_PORT + '/api/upload/' + transactionId + '/state', + url: upload_server + ':' + constants.PACKAGE_MANAGER_SERVER_PORT + '/api/' + type + '/' + transactionId + '/state', type: 'GET', headers: headers, forever: constants.FOREVER_ON, @@ -44,16 +45,16 @@ function checkStatus(req, transactionId) { deleteFile(req.file.filename); } else { setTimeout(function() { - checkStatus(req, transactionId); + checkStatus(req, transactionId, isUpdate); }, constants.PACKAGE_FILE_ONBOARD_TRANSACTION_STATUS_CHECK_DELAY_MILLISECONDS); } } }); }; -PackageFileHandler.checkCreatePackageStatusAndHandleFile = function(req, transactionId) { - checkStatus(req, transactionId); +PackageFileHandler.checkCreatePackageStatusAndHandleFile = function(req, transactionId, isUpdate) { + checkStatus(req, transactionId, isUpdate); }; -module.exports = PackageFileHandler; \ No newline at end of file +module.exports = PackageFileHandler; diff --git a/skyquake/plugins/composer/routes.js b/skyquake/plugins/composer/routes.js index 9fcf10479..2c78cc952 100644 --- a/skyquake/plugins/composer/routes.js +++ b/skyquake/plugins/composer/routes.js @@ -112,6 +112,15 @@ router.post('/upload', cors(), upload.single('package'), function (req, res, nex }); router.use('/upload', cors(), express.static('upload/packages')); +router.post('/update', cors(), upload.single('package'), function (req, res, next) { + Composer.update(req).then(function(data) { + utils.sendSuccessResponse(data, res); + }, function(error) { + utils.sendErrorResponse(error, res); + }); +}); +router.use('/update', cors(), express.static('upload/packages')); + router.post('/api/file-manager', cors(), upload.single('package'), function (req, res, next) { diff --git a/skyquake/plugins/composer/src/src/libraries/CatalogPackageManagerUploadDropZone.js b/skyquake/plugins/composer/src/src/libraries/CatalogPackageManagerUploadDropZone.js index 87afb6afa..a5867502d 100644 --- a/skyquake/plugins/composer/src/src/libraries/CatalogPackageManagerUploadDropZone.js +++ b/skyquake/plugins/composer/src/src/libraries/CatalogPackageManagerUploadDropZone.js @@ -47,7 +47,7 @@ function initializeDropZone(element = '#dropzone', button = false, action = ACTI paramName: 'package', url() { if (action === ACTIONS.update) { - return getCatalogPackageManagerServerOrigin() + '/api/update'; + return getCatalogPackageManagerServerOrigin() + '/composer/update?api_server=' + Utils.getSearchParams(window.location).api_server + '&upload_server=' + Utils.getSearchParams(window.location).upload_server + ( dev_download_server ? '&dev_download_server=' + dev_download_server : ''); } return getCatalogPackageManagerServerOrigin() + '/composer/upload?api_server=' + Utils.getSearchParams(window.location).api_server + '&upload_server=' + Utils.getSearchParams(window.location).upload_server + ( dev_download_server ? '&dev_download_server=' + dev_download_server : ''); }, diff --git a/skyquake/plugins/composer/src/src/stores/CatalogPackageManagerStore.js b/skyquake/plugins/composer/src/src/stores/CatalogPackageManagerStore.js index 22d63590f..3a82114a0 100644 --- a/skyquake/plugins/composer/src/src/stores/CatalogPackageManagerStore.js +++ b/skyquake/plugins/composer/src/src/stores/CatalogPackageManagerStore.js @@ -1,6 +1,6 @@ /* - * + * * Copyright 2016 RIFT.IO Inc * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -119,12 +119,15 @@ class CatalogPackageManagerStore { onUploadCatalogPackageStatusUpdated(response) { const upload = updateStatusInfo(response); this.updatePackage(upload); + console.log('updating package upload') // if pending with no transaction id - do nothing // bc DropZone.js will notify upload progress if (upload.pending && upload.transactionId) { + console.log('checking status') delayStatusCheck(this.getInstance().requestCatalogPackageUploadStatus, upload); } else if (upload.success) { this.getInstance().loadCatalogs(); + console.log('finished uploading to node, requesting status from rest') } } @@ -202,7 +205,7 @@ function updateStatusInfo(response) { statusInfo.pending = true; statusInfo.progress = 100; statusInfo.message = 'Upload completed.'; - statusInfo.transactionId = responseData['transaction-id'] || catalogPackage.transactionId; + statusInfo.transactionId = responseData['transaction_id'] || responseData['transaction-id'] || catalogPackage.transactionId; break; case 'upload-error': statusInfo.error = true; @@ -211,7 +214,7 @@ function updateStatusInfo(response) { case 'download-requested': statusInfo.pending = true; statusInfo.progress = 25; - statusInfo.transactionId = responseData['transaction-id'] || catalogPackage.transactionId; + statusInfo.transactionId = responseData['transaction_id'] || responseData['transaction-id'] || catalogPackage.transactionId; break; case 'pending': statusInfo.pending = true; -- 2.25.1