'use strict' ; import * as vscode from 'vscode' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { Reporter } from '../util/Reporter' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { FabricGatewayHelper } from '../fabric/FabricGatewayHelper' ; import { FabricEnvironmentRegistryEntry , FabricNode , FabricNodeType , FabricRuntimeUtil , LogType , FabricGatewayRegistry , FabricGatewayRegistryEntry , FabricEnvironmentRegistry , EnvironmentFlags } from 'ibm-blockchain-platform-common' ; export async function addGateway ( ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; try { outputAdapter . log ( LogType . INFO , undefined , 'addGateway' ) ; const fabricGatewayRegistry = FabricGatewayRegistry . instance ( ) ; const items = [ UserInputUtil . ADD_GATEWAY_FROM_CCP , UserInputUtil . ADD_GATEWAY_FROM_ENVIRONMENT ] ; const gatewayMethod = await UserInputUtil . showQuickPick ( 'Choose a method to add a gateway' , items ) as string ; if ( ! gatewayMethod ) { return ; } let chosenEnvironment ; let gatewayName ; if ( gatewayMethod === UserInputUtil . ADD_GATEWAY_FROM_ENVIRONMENT ) { const environments = await FabricEnvironmentRegistry . instance ( ) . getAll ( [ ] , [ EnvironmentFlags . LOCAL ] ) ; if ( environments . length === 0 ) { throw new Error ( `No environments to choose from. Gateways cannot be created from local environments.` ) ; } chosenEnvironment = await UserInputUtil . showFabricEnvironmentQuickPickBox ( 'Choose an environment to create a gateway from' , false , true , [ ] ) as IBlockchainQuickPickItem < FabricEnvironmentRegistryEntry > ; if ( ! chosenEnvironment ) { return ; } gatewayName = await UserInputUtil . showInputBox ( 'Enter a name for the gateway' , chosenEnvironment . label + '_gw' ) ; if ( ! gatewayName ) { return ; } } else { gatewayName = await UserInputUtil . showInputBox ( 'Enter a name for the gateway' ) ; if ( ! gatewayName ) { return ; } } const exists = await fabricGatewayRegistry . exists ( gatewayName ) ; if ( exists || gatewayName === FabricRuntimeUtil . LOCAL_FABRIC ) { throw new Error ( 'A gateway with this name already exists.' ) ; } let gatewayRegistryEntry ; if ( gatewayMethod === UserInputUtil . ADD_GATEWAY_FROM_CCP ) { gatewayRegistryEntry = await createGatewayFromCCP ( gatewayName ) ; } else { gatewayRegistryEntry = await createGatewayFromEnvironment ( gatewayName , chosenEnvironment . data ) ; } if ( ! gatewayRegistryEntry ) { return ; } outputAdapter . log ( LogType . SUCCESS , 'Successfully added a new gateway' ) ; Reporter . instance ( ) . sendTelemetryEvent ( 'addGatewayCommand' ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Failed to add a new gateway: ${ error . message }` , `Failed to add a new gateway: ${ error . toString ( ) }` ) ; } } async function createGatewayFromEnvironment ( gatewayName , environmentRegistryEntry ) { const chosenOrg = await UserInputUtil . showOrgQuickPick ( 'Choose an organisation to create the gateway for' , environmentRegistryEntry ) ; if ( ! chosenOrg ) { return ; } let caNode ; try { const chosenCA = await UserInputUtil . showNodesInEnvironmentQuickPick ( 'Choose a certificate authority for the gateway connection' , environmentRegistryEntry , [ FabricNodeType . CERTIFICATE_AUTHORITY ] ) as IBlockchainQuickPickItem < FabricNode > ; if ( ! chosenCA ) { return ; } caNode = chosenCA . data ; } catch ( error ) { VSCodeBlockchainOutputAdapter . instance ( ) . log ( LogType . INFO , 'Could not find a certifcate authority to add to the connection profile' ) ; } const peerNode = chosenOrg . data ; const connectionProfilePath = await FabricGatewayHelper . generateConnectionProfile ( gatewayName , peerNode , caNode ) ; const fabricGatewayEntry = new FabricGatewayRegistryEntry ( ) ; fabricGatewayEntry . name = gatewayName ; fabricGatewayEntry . associatedWallet = peerNode . wallet ; fabricGatewayEntry . environmentGroup = environmentRegistryEntry . name ; fabricGatewayEntry . connectionProfilePath = connectionProfilePath ; fabricGatewayEntry . fromEnvironment = environmentRegistryEntry . name ; const fabricGatewayRegistry = FabricGatewayRegistry . instance ( ) ; await fabricGatewayRegistry . add ( fabricGatewayEntry ) ; return fabricGatewayEntry ; } async function createGatewayFromCCP ( gatewayName ) { const openDialogOptions = { canSelectFiles : true , canSelectFolders : false , canSelectMany : false , openLabel : 'Select' , filters : { 'Connection Profiles' : [ 'json' , 'yaml' , 'yml' ] } } ; const connectionProfilePath = await UserInputUtil . browse ( 'Enter a file path to a connection profile file' , UserInputUtil . BROWSE_LABEL , openDialogOptions ) as string ; if ( ! connectionProfilePath ) { return ; } const fabricGatewayEntry = new FabricGatewayRegistryEntry ( ) ; fabricGatewayEntry . name = gatewayName ; fabricGatewayEntry . associatedWallet = '' ; const finalCcpPath = await FabricGatewayHelper . copyConnectionProfile ( gatewayName , connectionProfilePath ) ; fabricGatewayEntry . connectionProfilePath = finalCcpPath ; const fabricGatewayRegistry = FabricGatewayRegistry . instance ( ) ; await fabricGatewayRegistry . add ( fabricGatewayEntry ) ; return fabricGatewayEntry ; }