Skip to content

Commit

Permalink
Enable commands and gateway tree to handle multiple gateways (contrib…
Browse files Browse the repository at this point in the history
…utes to IBM-Blockchain#776)

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Apr 1, 2019
1 parent a3b4c8b commit 9349684
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 36 deletions.
2 changes: 2 additions & 0 deletions client/integrationTest/integrationTestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ExtensionCommands } from '../ExtensionCommands';
import { FabricWalletRegistryEntry } from '../src/fabric/FabricWalletRegistryEntry';
import { FabricConnectionManager } from '../src/fabric/FabricConnectionManager';
import { IFabricClientConnection } from '../src/fabric/IFabricClientConnection';
import { FabricRuntimeManager } from '../src/fabric/FabricRuntimeManager';

// tslint:disable no-unused-expression
const should: Chai.Should = chai.should();
Expand Down Expand Up @@ -148,6 +149,7 @@ export class IntegrationTestUtil {
gatewayEntry = new FabricGatewayRegistryEntry();
gatewayEntry.name = name;
gatewayEntry.managedRuntime = true;
gatewayEntry.connectionProfilePath = FabricRuntimeManager.instance().getRuntime().getConnectionProfilePath();
}

let walletEntry: FabricWalletRegistryEntry;
Expand Down
14 changes: 4 additions & 10 deletions client/src/commands/UserInputUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutput
import { LogType } from '../logging/OutputAdapter';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { FabricGatewayRegistry } from '../fabric/FabricGatewayRegistry';
import { FabricRuntime } from '../fabric/FabricRuntime';
import { ParsedCertificate } from '../fabric/ParsedCertificate';
import { FabricWalletRegistryEntry } from '../fabric/FabricWalletRegistryEntry';
import { FabricWalletRegistry } from '../fabric/FabricWalletRegistry';
Expand Down Expand Up @@ -65,9 +64,7 @@ export class UserInputUtil {
static readonly ADD_CERT_KEY_OPTION: string = 'Enter paths to Certificate and Key files';
static readonly ADD_ID_SECRET_OPTION: string = 'Select a gateway and provide an enrollment ID and secret';

public static showGatewayQuickPickBox(prompt: string, showManagedRuntime?: boolean): Thenable<IBlockchainQuickPickItem<FabricGatewayRegistryEntry> | undefined> {
const gateways: Array<FabricGatewayRegistryEntry> = FabricGatewayRegistry.instance().getAll();

public static async showGatewayQuickPickBox(prompt: string, showManagedRuntime?: boolean): Promise<IBlockchainQuickPickItem<FabricGatewayRegistryEntry> | undefined> {
const quickPickOptions: vscode.QuickPickOptions = {
ignoreFocusOut: false,
canPickMany: false,
Expand All @@ -76,16 +73,13 @@ export class UserInputUtil {

const allGateways: Array<FabricGatewayRegistryEntry> = [];

let connection: FabricGatewayRegistryEntry;
if (showManagedRuntime) {
// Allow users to choose local_fabric
const runtime: FabricRuntime = FabricRuntimeManager.instance().getRuntime();
connection = new FabricGatewayRegistryEntry();
connection.name = runtime.getName();
connection.managedRuntime = true;
allGateways.push(connection);
const runtimeGateways: Array<FabricGatewayRegistryEntry> = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
allGateways.push(...runtimeGateways);
}

const gateways: Array<FabricGatewayRegistryEntry> = FabricGatewayRegistry.instance().getAll();
allGateways.push(...gateways);

const gatewaysQuickPickItems: Array<IBlockchainQuickPickItem<FabricGatewayRegistryEntry>> = allGateways.map((gateway: FabricGatewayRegistryEntry) => {
Expand Down
1 change: 0 additions & 1 deletion client/src/commands/connectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export async function connect(gatewayRegistryEntry: FabricGatewayRegistryEntry,
}
}

gatewayRegistryEntry.connectionProfilePath = await runtime.getConnectionProfilePath();
runtimeData = 'managed runtime';
}

Expand Down
40 changes: 18 additions & 22 deletions client/src/explorer/gatewayExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { ContractTreeItem } from './model/ContractTreeItem';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { FabricRuntime } from '../fabric/FabricRuntime';
import { LocalGatewayTreeItem } from './model/LocalGatewayTreeItem';
import { FabricGatewayRegistry } from '../fabric/FabricGatewayRegistry';
import { ExtensionCommands } from '../../ExtensionCommands';
Expand Down Expand Up @@ -159,27 +158,24 @@ export class BlockchainGatewayExplorerProvider implements BlockchainExplorerProv
const allGateways: FabricGatewayRegistryEntry[] = this.fabricGatewayRegistry.getAll();

try {
const runtime: FabricRuntime = FabricRuntimeManager.instance().getRuntime();

const gateway: FabricGatewayRegistryEntry = new FabricGatewayRegistryEntry();
gateway.name = runtime.getName();
gateway.managedRuntime = true;

const command: vscode.Command = {
command: ExtensionCommands.CONNECT,
title: '',
arguments: [gateway]
};

const treeItem: LocalGatewayTreeItem = await LocalGatewayTreeItem.newLocalGatewayTreeItem(
this,
gateway.name,
gateway,
vscode.TreeItemCollapsibleState.None,
command
);

tree.push(treeItem);
const runtimeGateways: FabricGatewayRegistryEntry[] = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
for (const runtimeGateway of runtimeGateways) {
const command: vscode.Command = {
command: ExtensionCommands.CONNECT,
title: '',
arguments: [runtimeGateway]
};

const treeItem: LocalGatewayTreeItem = await LocalGatewayTreeItem.newLocalGatewayTreeItem(
this,
runtimeGateway.name,
runtimeGateway,
vscode.TreeItemCollapsibleState.None,
command
);

tree.push(treeItem);
}

} catch (error) {
outputAdapter.log(LogType.ERROR, `Error populating Blockchain Explorer View: ${error.message}`, `Error populating Blockchain Explorer View: ${error.toString()}`);
Expand Down
2 changes: 1 addition & 1 deletion client/src/fabric/FabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class FabricRuntime extends EventEmitter {
return connectionProfile;
}

public async getConnectionProfilePath(): Promise<string> {
public getConnectionProfilePath(): string {
const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = UserInputUtil.getDirPath(extDir);
const dir: string = path.join(homeExtDir, this.name);
Expand Down
11 changes: 11 additions & 0 deletions client/src/fabric/FabricRuntimeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { FabricWalletGeneratorFactory } from './FabricWalletGeneratorFactory';
import { VSCodeBlockchainDockerOutputAdapter } from '../logging/VSCodeBlockchainDockerOutputAdapter';
import { IFabricWalletGenerator } from './IFabricWalletGenerator';
import { IFabricRuntimeConnection } from './IFabricRuntimeConnection';
import { FabricGatewayRegistryEntry } from './FabricGatewayRegistryEntry';

export class FabricRuntimeManager {

Expand Down Expand Up @@ -91,6 +92,16 @@ export class FabricRuntimeManager {
}
}

public async getGatewayRegistryEntries(): Promise<FabricGatewayRegistryEntry[]> {
const runtime: FabricRuntime = this.getRuntime();
const registryEntry: FabricGatewayRegistryEntry = new FabricGatewayRegistryEntry({
name: runtime.getName(),
managedRuntime: true,
connectionProfilePath: runtime.getConnectionProfilePath()
});
return [registryEntry];
}

private readRuntimeUserSettings(): any {
const runtimeSettings: any = vscode.workspace.getConfiguration().get('fabric.runtime') as {
ports: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ chai.use(sinonChai);
const should: Chai.Should = chai.should();

// tslint:disable no-unused-expression
describe('userInputUtil', () => {
describe('UserInputUtil', () => {

let mySandBox: sinon.SinonSandbox;
let quickPickStub: sinon.SinonStub;
Expand Down Expand Up @@ -197,6 +197,8 @@ describe('userInputUtil', () => {
const managedRuntime: FabricGatewayRegistryEntry = new FabricGatewayRegistryEntry();
managedRuntime.name = 'local_fabric';
managedRuntime.managedRuntime = true;
const connectionProfilePath: string = UserInputUtil.getDirPath('~/.fabric-vscode/local_fabric/connection.json');
managedRuntime.connectionProfilePath = connectionProfilePath;

quickPickStub.resolves();
await UserInputUtil.showGatewayQuickPickBox('Choose a gateway', true);
Expand Down
2 changes: 2 additions & 0 deletions client/test/explorer/gatewayExplorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ describe('gatewayExplorer', () => {
mockRuntime.getName.returns('local_fabric');
mockRuntime.isBusy.returns(false);
mockRuntime.isRunning.resolves(true);
mockRuntime.getConnectionProfilePath.returns('connection.json');
mySandBox.stub(FabricRuntimeManager.instance(), 'getRuntime').returns(mockRuntime);

const blockchainGatewayExplorerProvider: BlockchainGatewayExplorerProvider = myExtension.getBlockchainGatewayExplorerProvider();
Expand All @@ -240,6 +241,7 @@ describe('gatewayExplorer', () => {
const gateway: FabricGatewayRegistryEntry = new FabricGatewayRegistryEntry();
gateway.name = 'local_fabric';
gateway.managedRuntime = true;
gateway.connectionProfilePath = 'connection.json';
const myCommand: vscode.Command = {
command: ExtensionCommands.CONNECT,
title: '',
Expand Down
2 changes: 1 addition & 1 deletion client/test/fabric/FabricRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ describe('FabricRuntime', () => {
describe('#getConnectionProfilePath', () => {

it('should get the runtime connection profile path', async () => {
const connectionPath: string = await runtime.getConnectionProfilePath();
const connectionPath: string = runtime.getConnectionProfilePath();
connectionPath.should.equal(path.join(rootPath, '..', '..', 'out', 'data', 'local_fabric', 'connection.json'));
});

Expand Down
16 changes: 16 additions & 0 deletions client/test/fabric/FabricRuntimeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { FabricWallet } from '../../src/fabric/FabricWallet';
import { FabricWalletGenerator } from '../../src/fabric/FabricWalletGenerator';
import * as vscode from 'vscode';
import { IFabricRuntimeConnection } from '../../src/fabric/IFabricRuntimeConnection';
import { FabricGatewayRegistryEntry } from '../../src/fabric/FabricGatewayRegistryEntry';
import { UserInputUtil } from '../../src/commands/UserInputUtil';

const should: Chai.Should = chai.should();

Expand Down Expand Up @@ -447,4 +449,18 @@ describe('FabricRuntimeManager', () => {
});

});

describe('#getGatewayRegistryEntries', () => {

it('should return an array of gateway registry entires', async () => {
const registryEntries: FabricGatewayRegistryEntry[] = await runtimeManager.getGatewayRegistryEntries();
registryEntries.should.have.lengthOf(1);
registryEntries[0].name.should.equal('local_fabric');
registryEntries[0].managedRuntime.should.be.true;
const connectionProfilePath: string = UserInputUtil.getDirPath('~/.fabric-vscode/local_fabric/connection.json');
registryEntries[0].connectionProfilePath.should.equal(connectionProfilePath);
});

});

});

0 comments on commit 9349684

Please sign in to comment.