'use strict' ; import * as vscode from 'vscode' ; import Axios from 'axios' ; import { CloudAccountApi } from '../interfaces/cloud-account-api' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { LogType } from 'ibm-blockchain-platform-common' ; import { URL } from 'url' ; import { DependencyProperties } from '../dependencies/Dependencies' ; export class ExtensionsInteractionUtil { public static isIBMCloudExtensionInstalled ( ) { return ! ! vscode . extensions . getExtension ( DependencyProperties . IBM_CLOUD_ACCOUNT_EXTENSION ) ; } public static async getIBMCloudExtension ( ) { const cloudAccountExtension = vscode . extensions . getExtension ( DependencyProperties . IBM_CLOUD_ACCOUNT_EXTENSION ) ; if ( ! cloudAccountExtension ) { throw new Error ( 'IBM Cloud Account extension must be installed' ) ; } const commands = await vscode . commands . getCommands ( ) ; const hasPing = commands . includes ( 'ibmcloud-account.ping' ) ; if ( hasPing ) { await vscode . commands . executeCommand ( 'ibmcloud-account.ping' ) ; } else if ( ! cloudAccountExtension . isActive ) { await cloudAccountExtension . activate ( ) ; } return cloudAccountExtension . exports ; } public static async cloudAccountGetAccessToken ( userInteraction = true ) { const cloudAccount = await this . getIBMCloudExtension ( ) ; const isLoggedIn = await cloudAccount . loggedIn ( ) ; let result ; if ( ! isLoggedIn ) { if ( userInteraction ) { result = await vscode . commands . executeCommand ( 'ibmcloud-account.login' ) ; if ( ! result ) { return ; } } else { return ; } } else { const hasAccount = await cloudAccount . accountSelected ( ) ; if ( ! hasAccount ) { if ( userInteraction ) { result = await vscode . commands . executeCommand ( 'ibmcloud-account.selectAccount' ) ; if ( ! result ) { return ; } } else { return ; } } } const accessToken = await cloudAccount . getAccessToken ( ) ; return accessToken ; } public static async cloudAccountIsLoggedIn ( ) { const cloudAccount = await this . getIBMCloudExtension ( ) ; return await cloudAccount . loggedIn ( ) ; } public static async cloudAccountHasSelectedAccount ( ) { const cloudAccount = await this . getIBMCloudExtension ( ) ; return await cloudAccount . accountSelected ( ) ; } public static async cloudAccountAnyIbpResources ( ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; let anyIbpResources = false ; const accessToken = await this . cloudAccountGetAccessToken ( ) ; if ( ! accessToken ) { return ; } const requestOptions = { headers : { Authorization : `Bearer ${ accessToken }` } } ; const baseUrl = 'https://resource-controller.cloud.ibm.com' ; let resourcesUrl = `${ baseUrl }/v2/resource_instances` ; while ( resourcesUrl && ! anyIbpResources ) { try { const response = await Axios . get ( resourcesUrl , requestOptions ) ; anyIbpResources = response . data . resources . some ( ( resource ) => resource . resource_plan_id === 'blockchain-standard' ) ; if ( response . data . next_url ) { resourcesUrl = `${ baseUrl }${ response . data . next_url }` ; } else { break ; } } catch ( error ) { resourcesUrl = null ; outputAdapter . log ( LogType . ERROR , `Error fetching IBP resources: ${ error . message }` , `Error fetching IBP resources: ${ error . toString ( ) }` ) ; } } return anyIbpResources ; } public static async cloudAccountGetIbpResources ( ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; const ibpResources = [ ] ; const accessToken = await this . cloudAccountGetAccessToken ( ) ; if ( ! accessToken ) { return ; } const requestOptions = { headers : { Authorization : `Bearer ${ accessToken }` } } ; const baseUrl = 'https://resource-controller.cloud.ibm.com' ; let resourcesUrl = `${ baseUrl }/v2/resource_instances` ; while ( resourcesUrl ) { try { const response = await Axios . get ( resourcesUrl , requestOptions ) ; for ( const resource of response . data . resources ) { if ( resource . resource_plan_id === 'blockchain-standard' ) { ibpResources . push ( resource ) ; } } if ( response . data . next_url ) { resourcesUrl = `${ baseUrl }${ response . data . next_url }` ; } else { break ; } } catch ( error ) { resourcesUrl = null ; outputAdapter . log ( LogType . ERROR , `Error fetching IBP resources: ${ error . message }` , `Error fetching IBP resources: ${ error . toString ( ) }` ) ; } } return ibpResources ; } public static async cloudAccountGetApiEndpoint ( ibpResource , accessToken ) { const requestOptions = { headers : { Authorization : `Bearer ${ accessToken }` } } ; const dashboardUrl = new URL ( ibpResource . dashboard_url ) ; const encodedCrn = encodeURIComponent ( ibpResource . crn ) ; const consoleStatus = await Axios . get ( `${ dashboardUrl . origin }/api/alternative-auth/resources/${ encodedCrn }/optools` , requestOptions ) ; if ( consoleStatus . status !== 200 ) { throw new Error ( `Got status ${ consoleStatus . status }. Please make sure the IBM Blockchain Platform Console deployment has finished before adding environment.` ) ; } return consoleStatus . data . endpoint ; } }