Upload and Update status and endpoints updated 60/960/1
authorLaurence Maultsby <laurence.maultsby@riftio.com>
Tue, 17 Jan 2017 14:37:31 +0000 (09:37 -0500)
committerLaurence Maultsby <laurence.maultsby@riftio.com>
Tue, 17 Jan 2017 14:37:31 +0000 (09:37 -0500)
Signed-off-by: Laurence Maultsby <laurence.maultsby@riftio.com>
skyquake/plugins/composer/api/composer.js
skyquake/plugins/composer/api/packageFileHandler.js
skyquake/plugins/composer/routes.js
skyquake/plugins/composer/src/src/libraries/CatalogPackageManagerUploadDropZone.js
skyquake/plugins/composer/src/src/stores/CatalogPackageManagerStore.js

index ce12860..8df1a91 100644 (file)
@@ -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
index f199610..cc9cca7 100644 (file)
@@ -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;
index 9fcf104..2c78cc9 100644 (file)
@@ -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) {
index 87afb6a..a586750 100644 (file)
@@ -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 : '');
                },
index 22d6359..3a82114 100644 (file)
@@ -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;