'use strict' ; import * as vscode from 'vscode' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { NodeTreeItem } from '../explorer/runtimeOps/connectedTree/NodeTreeItem' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { FabricEnvironmentRegistry , FabricEnvironmentRegistryEntry , FabricNode , LogType , FabricEnvironment , EnvironmentType , EnvironmentFlags } from 'ibm-blockchain-platform-common' ; import { EnvironmentFactory } from '../fabric/environments/EnvironmentFactory' ; export async function deleteNode ( nodeTreeItem , hideNode ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; let action ; if ( hideNode ) { action = 'hide' ; outputAdapter . log ( LogType . INFO , undefined , 'hide node' ) ; } else { action = 'delete' ; outputAdapter . log ( LogType . INFO , undefined , 'delete node' ) ; } let deleteEnvironment = false ; let disconnectEnvironment = false ; let environmentRegistryEntry ; try { let nodesToDelete ; if ( ! nodeTreeItem ) { const environments = await FabricEnvironmentRegistry . instance ( ) . getAll ( [ ] , [ EnvironmentFlags . LOCAL ] ) ; if ( environments . length === 0 ) { outputAdapter . log ( LogType . ERROR , `No environments to choose from. Nodes from local environments cannot be modified.` ) ; return ; } let chosenEnvironment ; if ( hideNode ) { chosenEnvironment = await UserInputUtil . showFabricEnvironmentQuickPickBox ( 'Choose an OpsTools environment to hide a node from' , false , true , [ EnvironmentFlags . OPS_TOOLS ] ) as IBlockchainQuickPickItem < FabricEnvironmentRegistryEntry > ; } else { chosenEnvironment = await UserInputUtil . showFabricEnvironmentQuickPickBox ( 'Choose an environment to delete a node from' , false , true , [ ] ) as IBlockchainQuickPickItem < FabricEnvironmentRegistryEntry > ; } if ( ! chosenEnvironment ) { return ; } environmentRegistryEntry = chosenEnvironment . data ; const chosenNodes = await UserInputUtil . showNodesInEnvironmentQuickPick ( `Choose a node to ${ action }` , environmentRegistryEntry , [ ] , false , true ) as IBlockchainQuickPickItem < FabricNode > [ ] ; if ( ! chosenNodes || chosenNodes . length === 0 ) { return ; } nodesToDelete = chosenNodes . map ( ( chosenNode ) => chosenNode . data ) ; } else { if ( nodeTreeItem instanceof NodeTreeItem ) { nodesToDelete = [ nodeTreeItem . node ] ; } else { nodesToDelete = [ nodeTreeItem ] ; } environmentRegistryEntry = FabricEnvironmentManager . instance ( ) . getEnvironmentRegistryEntry ( ) ; } const environment = EnvironmentFactory . getEnvironment ( environmentRegistryEntry ) ; const allNodes = await environment . getNodes ( false , true ) ; const nodes = allNodes . filter ( ( _node ) => ! _node . hidden ) ; let reallyDoIt = false ; let allOpsNodesDeleted = false ; if ( environmentRegistryEntry . environmentType === EnvironmentType . OPS_TOOLS_ENVIRONMENT || environmentRegistryEntry . environmentType === EnvironmentType . SAAS_OPS_TOOLS_ENVIRONMENT ) { if ( hideNode ) { if ( nodes . length === nodesToDelete . length ) { reallyDoIt = await UserInputUtil . showConfirmationWarningMessage ( `This will ${ action } the remaining node(s), and disconnect from the environment. Do you want to continue?` ) ; disconnectEnvironment = true ; } else { reallyDoIt = await UserInputUtil . showConfirmationWarningMessage ( `This will ${ action } the node(s). Do you want to continue?` ) ; } } else { reallyDoIt = true ; const visibleNodesUrls = nodes . map ( ( _node ) => _node . api_url ) ; const visibleTodDelete = nodesToDelete . filter ( ( _node ) => visibleNodesUrls . indexOf ( _node . api_url ) !== - 1 ) ; if ( nodesToDelete . length === allNodes . length ) { allOpsNodesDeleted = true ; } else if ( visibleTodDelete . length >= nodes . length ) { disconnectEnvironment = true ; } } } else { if ( nodes . length === nodesToDelete . length ) { reallyDoIt = await UserInputUtil . showConfirmationWarningMessage ( `This will ${ action } the remaining node(s), and the environment. Do you want to continue?` ) ; deleteEnvironment = reallyDoIt ; } else { reallyDoIt = await UserInputUtil . showConfirmationWarningMessage ( `This will ${ action } the node(s). Do you want to continue?` ) ; } } if ( ! reallyDoIt ) { return ; } if ( hideNode ) { for ( const nodeToDelete of nodesToDelete ) { nodeToDelete . hidden = true ; await environment . updateNode ( nodeToDelete ) ; } } else { for ( const nodeToDelete of nodesToDelete ) { await environment . deleteNode ( nodeToDelete ) ; } } if ( ! deleteEnvironment && ! disconnectEnvironment && ! allOpsNodesDeleted ) { const connectedRegistry = FabricEnvironmentManager . instance ( ) . getEnvironmentRegistryEntry ( ) ; if ( connectedRegistry && connectedRegistry . name === environmentRegistryEntry . name ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_ENVIRONMENT , connectedRegistry ) ; } } if ( hideNode ) { if ( nodesToDelete . length === 1 ) { outputAdapter . log ( LogType . SUCCESS , `Successfully hid node ${ nodesToDelete [ 0 ] . name }` ) ; } else { outputAdapter . log ( LogType . SUCCESS , `Successfully hid nodes` ) ; } } else { if ( nodesToDelete . length === 1 && ( environmentRegistryEntry . environmentType === EnvironmentType . OPS_TOOLS_ENVIRONMENT || environmentRegistryEntry . environmentType === EnvironmentType . SAAS_OPS_TOOLS_ENVIRONMENT ) ) { outputAdapter . log ( LogType . SUCCESS , `Node ${ nodesToDelete [ 0 ] . name } was removed from ${ environmentRegistryEntry . name }` ) ; } else if ( nodesToDelete . length === 1 ) { outputAdapter . log ( LogType . SUCCESS , `Successfully deleted node ${ nodesToDelete [ 0 ] . name }` ) ; } else { outputAdapter . log ( LogType . SUCCESS , `Successfully deleted nodes` ) ; } } } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Cannot ${ action } node: ${ error . message }` , `Cannot ${ action } node: ${ error . toString ( ) }` ) ; } if ( deleteEnvironment ) { await vscode . commands . executeCommand ( ExtensionCommands . DELETE_ENVIRONMENT , environmentRegistryEntry , true ) ; } if ( disconnectEnvironment ) { await vscode . commands . executeCommand ( ExtensionCommands . DISCONNECT_ENVIRONMENT ) ; } }