'use strict' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { FabricConnectionFactory } from '../fabric/FabricConnectionFactory' ; import { Reporter } from '../util/Reporter' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { ExtensionUtil } from '../util/ExtensionUtil' ; import * as vscode from 'vscode' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { FabricEnvironmentManager , ConnectedState } from '../fabric/environments/FabricEnvironmentManager' ; import { FabricEnvironmentRegistryEntry , IFabricEnvironmentConnection , FabricNode , LogType , FabricEnvironment , EnvironmentType , MicrofabEnvironment } from 'ibm-blockchain-platform-common' ; import { EnvironmentFactory } from '../fabric/environments/EnvironmentFactory' ; import { LocalMicroEnvironment } from '../fabric/environments/LocalMicroEnvironment' ; export async function fabricEnvironmentConnect ( fabricEnvironmentRegistryEntry , showSuccess = true ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; let startRefresh = true ; if ( showSuccess ) { outputAdapter . log ( LogType . INFO , undefined , `connecting to fabric environment` ) ; } let fabricEnvironment ; try { if ( ! fabricEnvironmentRegistryEntry ) { const chosenEntry = await UserInputUtil . showFabricEnvironmentQuickPickBox ( 'Choose an environment to connect with' , false , true ) as IBlockchainQuickPickItem < FabricEnvironmentRegistryEntry > ; if ( ! chosenEntry ) { return ; } fabricEnvironmentRegistryEntry = chosenEntry . data ; } fabricEnvironment = EnvironmentFactory . getEnvironment ( fabricEnvironmentRegistryEntry ) ; if ( fabricEnvironmentRegistryEntry . environmentType === EnvironmentType . MICROFAB_ENVIRONMENT ) { const isAlive = await ( fabricEnvironment as MicrofabEnvironment ) . isAlive ( ) ; if ( ! isAlive ) { outputAdapter . log ( LogType . ERROR , `Unable to connect to Microfab runtime ${ ( fabricEnvironment as MicrofabEnvironment ) . getURL ( ) }` ) ; return ; } } if ( fabricEnvironmentRegistryEntry . managedRuntime ) { let running = await ( fabricEnvironment as LocalMicroEnvironment ) . isRunning ( ) ; if ( ! running ) { try { await vscode . commands . executeCommand ( ExtensionCommands . START_FABRIC , fabricEnvironmentRegistryEntry ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Unable to connect as starting the Fabric failed` ) ; return ; } running = await ( fabricEnvironment as LocalMicroEnvironment ) . isRunning ( ) ; if ( ! running ) { return ; } } const isAlive = await ( fabricEnvironment as LocalMicroEnvironment ) . waitFor ( ) ; if ( ! isAlive ) { return ; } } let nodes = await fabricEnvironment . getNodes ( ) ; if ( fabricEnvironmentRegistryEntry . environmentType === EnvironmentType . OPS_TOOLS_ENVIRONMENT || fabricEnvironmentRegistryEntry . environmentType === EnvironmentType . SAAS_OPS_TOOLS_ENVIRONMENT ) { let informOfChanges = true ; if ( nodes . length === 0 ) { const importNodes = await UserInputUtil . showConfirmationWarningMessage ( `Problem connecting to environment ${ fabricEnvironmentRegistryEntry . name }: no visible nodes. Would you like to filter nodes?` ) ; if ( ! importNodes ) { return ; } informOfChanges = false ; } try { await ExtensionUtil . executeCommandInternal ( ExtensionCommands . EDIT_NODE_FILTERS , fabricEnvironmentRegistryEntry , false , UserInputUtil . ADD_ENVIRONMENT_FROM_OPS_TOOLS , informOfChanges , showSuccess , true ) ; } catch ( error ) { if ( error . message . match ( /might be out of date/ ) !== null ) { startRefresh = false ; } else { throw error ; } } nodes = await fabricEnvironment . getNodes ( ) ; if ( nodes . length === 0 ) { FabricEnvironmentManager . instance ( ) . disconnect ( ) ; return ; } } const requireSetup = await fabricEnvironment . requireSetup ( ) ; if ( requireSetup && ! ( fabricEnvironment instanceof LocalMicroEnvironment ) ) { FabricEnvironmentManager . instance ( ) . connect ( undefined , fabricEnvironmentRegistryEntry , ConnectedState . SETUP ) ; VSCodeBlockchainOutputAdapter . instance ( ) . log ( LogType . IMPORTANT , 'You must complete setup for this environment to enable deploy and register identity operations on the nodes. Click each node in the list to perform the required setup steps' ) ; FabricEnvironmentManager . instance ( ) . stopEnvironmentRefresh ( ) ; return ; } const connection = FabricConnectionFactory . createFabricEnvironmentConnection ( fabricEnvironmentRegistryEntry . name ) ; await connection . connect ( nodes ) ; try { await connection . createChannelMap ( ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Error connecting to environment ${ fabricEnvironment . getName ( ) }: ${ error . message }` , `Error connecting to environment ${ fabricEnvironment . getName ( ) }: ${ error . toString ( ) }` ) ; await vscode . commands . executeCommand ( ExtensionCommands . DISCONNECT_ENVIRONMENT ) ; return ; } FabricEnvironmentManager . instance ( ) . connect ( connection , fabricEnvironmentRegistryEntry , ConnectedState . CONNECTING , startRefresh ) ; const environmentName = fabricEnvironment . getName ( ) ; if ( showSuccess ) { outputAdapter . log ( LogType . SUCCESS , `Connected to ${ environmentName }` ) ; } let environmentData = 'managed environment' ; if ( ! fabricEnvironmentRegistryEntry . managedRuntime ) { environmentData = 'user environment' ; } const isIBMer = ExtensionUtil . checkIfIBMer ( ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'fabricEnvironmentConnectCommand' , { environmentData : environmentData , connectEnvironmentIBM : isIBMer + '' } ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Cannot connect to environment: ${ error . message }` , `Cannot connect to environment: ${ error . toString ( ) }` ) ; return ; } }