From 6ca503654047f83c57c2728eb83f765a5c2dc578 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 2 Jul 2018 13:09:41 +0200 Subject: [PATCH] createContainer function implemented --- AABrowserAPI/TeamSetup.js | 147 ++++++++++++++++++++++++++++++++++++-- Server/SessionManager.js | 2 +- Server/Storage.js | 84 +++++++++++++++++----- 3 files changed, 208 insertions(+), 25 deletions(-) diff --git a/AABrowserAPI/TeamSetup.js b/AABrowserAPI/TeamSetup.js index a65abc607..1f9f32e02 100644 --- a/AABrowserAPI/TeamSetup.js +++ b/AABrowserAPI/TeamSetup.js @@ -30,10 +30,11 @@ /* Procedure to create a new Team, User and Bot: - - 1. Add it to the Ecosystem. - 2. Agregarlo al archivo de sessiones. --> copiar la session local a Develop - 3. Crear el container del nuevo devTeam. + + 1. Create Continer. This will also tell us if the Team name is not already in use. FALTA ERROR HANDLING Y EN STORAGE EL LOG DE ERRORS + 2. Add it to the Ecosystem. + 3. Add it to Sessions. + 4. Crear el folder del nuevo devTeam en el container aaplatform. 5. Copiar el bot a devTeam/bots --> Cambiarle el nombre al folder 6. Modificar la configuracion del bot reemplazando el devTeam y el Nombre. @@ -45,7 +46,28 @@ */ - addToEcosystem(); + createContainer(); + + function createContainer() { + + if (CONSOLE_LOG === true) { console.log("[INFO] TeamSetup -> newTeam -> createContainer -> Entering function."); } + + storage.createContainer(pTeamCodeName, onContainerCreated); + + function onContainerCreated(err) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] TeamSetup -> newTeam -> createContainer -> onContainerCreated -> Could not create the container. "); + console.log("[ERROR] TeamSetup -> newTeam -> createContainer -> onContainerCreated -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + } function addToEcosystem() { @@ -70,6 +92,7 @@ return; } + pFileContent = pFileContent.trim(); let ecosystem = JSON.parse(pFileContent); let newTeam = { @@ -133,11 +156,121 @@ function addToSessions() { + try { + if (CONSOLE_LOG === true) { console.log("[INFO] TeamSetup -> newTeam -> addToSessions -> Entering function."); } + + storage.readData("AdvancedAlgos", "AAPlatform", "open.sessions.json", false, onDataRead); + + function onDataRead(err, pFileContent) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] TeamSetup -> newTeam -> addToSessions -> onDataRead -> Entering function."); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> onDataRead -> Could not read a file. "); + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> onDataRead -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + pFileContent = pFileContent.trim(); + let sessions = JSON.parse(pFileContent); + let token = createRandomToken(); + + function createRandomToken() { + + let text = ""; + let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + for (var i = 0; i < 128; i++) + text += possible.charAt(Math.floor(Math.random() * possible.length)); + + return text; + } + + let newSession = { + sessionToken: token, + userName: pUserName, + devTeams: [ + { + codeName: pTeamCodeName, + displayName: pTeamDisplayName, + userBots: [ + { + displayName: pBotName, + codeName: pBotName, + type: "Trading", + repo: pBotName + "-Trading-Bot", + processes: [ + { + name: "Trading-Process" + } + ] + } + ], + devTeamDependencies: ["AAMasters", "AAVikings"] + } + ], + exchangeKeys: [ + { + exchange: "Poloniex", + key: "", + secret: "" + } + ] + }; + + sessions.push(newSession); + + let fileContent = JSON.stringify(sessions); + + storage.writeData("AdvancedAlgos", "AAPlatform", "open.sessions.json", fileContent, onDataWritten); + + function onDataWritten(err) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] TeamSetup -> newTeam -> addToSessions -> onDataRead -> onDataWritten -> Entering function."); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> onDataRead -> onDataWritten -> Could not write a file. "); + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> onDataRead -> onDataWritten -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + createContainer(); + + } catch (err) { + + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> onDataRead -> onDataWritten -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + } + } + + } catch (err) { + + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> onDataRead -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + } + } + + } catch (err) { + + console.log("[ERROR] TeamSetup -> newTeam -> addToSessions -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + + } } - callBackFunction(global.DEFAULT_OK_RESPONSE); - } catch (err) { console.log("[ERROR] TeamSetup -> newTeam -> err.message = " + err.message); diff --git a/Server/SessionManager.js b/Server/SessionManager.js index 412f21eed..73a05e60b 100644 --- a/Server/SessionManager.js +++ b/Server/SessionManager.js @@ -21,7 +21,7 @@ let storage = STORAGE.newStorage(); storage.initialize(undefined, pServerConfig); - storage.readData("AdvancedAlgos", "AAPlatform", "Open.Sessions.json", false, onData); + storage.readData("AdvancedAlgos", "AAPlatform", "open.sessions.json", false, onData); function onData(err, pText) { diff --git a/Server/Storage.js b/Server/Storage.js index 36f711cdb..3bc36571c 100644 --- a/Server/Storage.js +++ b/Server/Storage.js @@ -3,6 +3,7 @@ let thisObject = { readData: readData, writeData: writeData, + createContainer: createContainer, initialize: initialize } @@ -114,12 +115,10 @@ try { - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pFile = " + pFile); } - - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> " + pOrg + '.' + pPath + '.' + pFile + " NOT found at cache."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pFile = " + pFile); } let storage = require('azure-storage'); let connectionString; @@ -149,18 +148,17 @@ try { - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] API -> writeData -> onFileCreated -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> response = " + JSON.stringify(response)); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pFile = " + pFile); } if (err !== null || text === null) { - if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> Error Received from API Library. "); } - if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } - if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> Returning an empty JSON object string. "); } + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> writeData -> onFileCreated -> Error Received from Storage Library. "); } + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; @@ -170,13 +168,65 @@ callBackFunction(global.DEFAULT_OK_RESPONSE); } catch (err) { - console.log("[ERROR] API -> writeData -> onFileCreated -> err.message = " + err.message); + console.log("[ERROR] Storage -> writeData -> onFileCreated -> err.message = " + err.message); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } catch (err) { - console.log("[ERROR] API -> writeData -> err.message = " + err.message); + console.log("[ERROR] Storage -> writeData -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + + function createContainer(pContainerName, pPath, pFile, pFileContent, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> createContainer -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> createContainer -> pContainerName = " + pContainerName); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + + pContainerName.toLowerCase(); + + blobService.createContainer(pContainerName, onContainerCreated); + + function onContainerCreated(err) { + + if (err) { + + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> createContainer -> onContainerCreated -> Error Received from Storage Library. "); } + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> createContainer -> onContainerCreated -> err = " + JSON.stringify(err)); } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + } + + } catch (err) { + console.log("[ERROR] Storage -> createContainer -> err.message = " + err.message); callBackFunction(global.DEFAULT_FAIL_RESPONSE); } }