'use strict' ; import * as vscode from 'vscode' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { FabricEnvironmentRegistry , FabricEnvironmentRegistryEntry , LogType , EnvironmentType } from 'ibm-blockchain-platform-common' ; import { ExtensionsInteractionUtil } from '../util/ExtensionsInteractionUtil' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { SettingConfigurations } from '../configurations' ; export async function logInAndDiscover ( ) { const fabricEnvironmentRegistry = FabricEnvironmentRegistry . instance ( ) ; const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'Log in and discover' ) ; await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . DISCOVER_SAAS_ENVS , true , vscode . ConfigurationTarget . Global ) ; const ibpResources = await ExtensionsInteractionUtil . cloudAccountGetIbpResources ( ) ; if ( ibpResources . length === 0 ) { await vscode . commands . executeCommand ( ExtensionCommands . REFRESH_ENVIRONMENTS ) ; return ; } let addedResources = 0 ; const accessToken = await ExtensionsInteractionUtil . cloudAccountGetAccessToken ( ) ; if ( ! accessToken ) { return ; } for ( const resource of ibpResources ) { try { const fabricEnvironmentEntry = new FabricEnvironmentRegistryEntry ( ) ; const environmentName = resource . name ; const allEnvironments = await fabricEnvironmentRegistry . getAll ( ) ; const dockerName = environmentName . replace ( /[^A-Za-z0-9]/g , '' ) ; for ( const _environment of allEnvironments ) { const _environmentDockerName = _environment . name . replace ( /[^A-Za-z0-9]/g , '' ) ; if ( _environment . name === environmentName || _environmentDockerName === dockerName ) { throw new Error ( 'An environment with this name already exists or is too similar.' ) ; } } const accessInfo = await ExtensionsInteractionUtil . cloudAccountGetApiEndpoint ( resource , accessToken ) ; if ( accessInfo ) { fabricEnvironmentEntry . name = environmentName ; fabricEnvironmentEntry . url = accessInfo ; fabricEnvironmentEntry . environmentType = EnvironmentType . SAAS_OPS_TOOLS_ENVIRONMENT ; await fabricEnvironmentRegistry . add ( fabricEnvironmentEntry ) ; addedResources ++ ; } } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Failed to add a new environment: ${ error . message }` , `Failed to add a new environment: ${ error . toString ( ) }` ) ; } } outputAdapter . log ( LogType . SUCCESS , `Automatically added ${ addedResources } environment(s) from IBM Cloud` ) ; await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . DISCOVER_SAAS_ENVS , false , vscode . ConfigurationTarget . Global ) ; }