Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Update gateway registries (#1537)
Browse files Browse the repository at this point in the history
Update gateway registries to use new file based registry

contributes to #1517

Signed-off-by: Caroline Fletcher <caroline.fletcher@uk.ibm.com>
  • Loading branch information
cazfletch authored and Jakeeyturner committed Oct 17, 2019
1 parent b9bcf40 commit d410fed
Show file tree
Hide file tree
Showing 54 changed files with 396 additions and 1,063 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ The IBM Blockchain Platform extension provides an explorer and commands accessib
| Disassociate A Wallet | Remove the association between a wallet and a gateway |
| Disconnect From Environment | Disconnect from the environment you're currently connected to |
| Disconnect From Gateway | Disconnect from the blockchain gateway you're currently connected to |
| Edit Gateway | Edit connection profile for connecting to a blockchain gateway |
| Evaluate Transaction | Evaluate a smart contract transaction |
| Export Connection Profile | Export connection profile for a blockchain gateway |
| Export Package | Export a smart contract package to use outside VS Code |
Expand Down
3 changes: 2 additions & 1 deletion configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export class SettingConfigurations {

// FABRIC CONFIGURATIONS
static readonly FABRIC_RUNTIME: string = 'ibm-blockchain-platform.fabric.runtime';
static readonly FABRIC_GATEWAYS: string = 'ibm-blockchain-platform.fabric.gateways';
static readonly FABRIC_CLIENT_TIMEOUT: string = 'ibm-blockchain-platform.fabric.client.timeout';
static readonly FABRIC_CHAINCODE_TIMEOUT: string = 'ibm-blockchain-platform.fabric.chaincode.timeout';
static readonly FABRIC_ENVIRONMENTS: string = 'ibm-blockchain-platform.fabric.environments';

// Needed for migration
static readonly OLD_FABRIC_WALLETS: string = 'ibm-blockchain-platform.fabric.wallets';
static readonly OLD_FABRIC_GATEWAYS: string = 'ibm-blockchain-platform.fabric.gateways';

// EXTENSION CONFIGURATIONS
static readonly EXTENSION_DIRECTORY: string = 'ibm-blockchain-platform.ext.directory';
Expand All @@ -39,4 +39,5 @@ export class SettingConfigurations {

export class FileConfigurations {
static readonly FABRIC_WALLETS: string = 'wallets';
static readonly FABRIC_GATEWAYS: string = 'gateways';
}
29 changes: 3 additions & 26 deletions cucumber/helpers/gatewayHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ import { UserInputUtilHelper } from './userInputUtilHelper';
import { ExtensionCommands } from '../../ExtensionCommands';
import { FabricGatewayRegistryEntry } from '../../extension/registries/FabricGatewayRegistryEntry';
import { FabricGatewayRegistry } from '../../extension/registries/FabricGatewayRegistry';
import { FabricRuntimeManager } from '../../extension/fabric/FabricRuntimeManager';
import { FabricWalletRegistryEntry } from '../../extension/registries/FabricWalletRegistryEntry';
import { FabricWalletRegistry } from '../../extension/registries/FabricWalletRegistry';
import { FabricWalletUtil } from '../../extension/fabric/FabricWalletUtil';
import { WalletAndIdentityHelper } from './walletAndIdentityHelper';
import { BlockchainGatewayExplorerProvider } from '../../extension/explorer/gatewayExplorer';
import { BlockchainTreeItem } from '../../extension/explorer/model/BlockchainTreeItem';
import { UserInputUtil } from '../../extension/commands/UserInputUtil';
Expand All @@ -37,7 +35,6 @@ import { FabricEnvironmentRegistry } from '../../extension/registries/FabricEnvi
import { FabricEnvironment } from '../../extension/fabric/FabricEnvironment';
import { FabricNode, FabricNodeType } from '../../extension/fabric/FabricNode';
import { ExtensionUtil } from '../../extension/util/ExtensionUtil';
import { FabricRuntimeUtil } from '../../extension/fabric/FabricRuntimeUtil';

chai.use(sinonChai);
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -94,24 +91,10 @@ export class GatewayHelper {
public async connectToFabric(name: string, walletName: string, identityName: string = 'greenConga', expectAssociated: boolean = true): Promise<void> {
let gatewayEntry: FabricGatewayRegistryEntry;

try {
gatewayEntry = await FabricGatewayRegistry.instance().get(name);
} catch (error) {
const gatewayEntries: FabricGatewayRegistryEntry[] = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
gatewayEntry = gatewayEntries[0];
}
gatewayEntry = await FabricGatewayRegistry.instance().get(name);

if (!expectAssociated) {
let walletEntry: FabricWalletRegistryEntry;

try {
walletEntry = await FabricWalletRegistry.instance().get(walletName);
} catch (error) {
walletEntry = new FabricWalletRegistryEntry();
walletEntry.name = FabricWalletUtil.LOCAL_WALLET;
walletEntry.walletPath = WalletAndIdentityHelper.localWalletPath;
walletEntry.managedWallet = true;
}
const walletEntry: FabricWalletRegistryEntry = await FabricWalletRegistry.instance().get(walletName);

this.userInputUtilHelper.showWalletsQuickPickStub.resolves({
name: walletEntry.name,
Expand Down Expand Up @@ -182,13 +165,7 @@ export class GatewayHelper {
}

public async exportConnectionProfile(gatewayName: string): Promise<void> {
let gatewayEntry: FabricGatewayRegistryEntry;
if (gatewayName === FabricRuntimeUtil.LOCAL_FABRIC_DISPLAY_NAME) {
const entry: FabricGatewayRegistryEntry[] = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
gatewayEntry = entry[0];
} else {
gatewayEntry = await FabricGatewayRegistry.instance().get(gatewayName);
}
const gatewayEntry: FabricGatewayRegistryEntry = await FabricGatewayRegistry.instance().get(gatewayName);

const profilePath: string = path.join(__dirname, '..', '..', '..', 'cucumber', 'tmp', 'profiles');
await fs.ensureDir(profilePath);
Expand Down
4 changes: 4 additions & 0 deletions cucumber/steps/gateway.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ module.exports = function(): any {
*/

this.Given("the gateway '{string}' is created", this.timeout, async (gateway: string) => {
if (gateway === FabricRuntimeUtil.LOCAL_FABRIC_DISPLAY_NAME) {
gateway = FabricRuntimeUtil.LOCAL_FABRIC;
}

this.gateway = gateway;

await this.gatewayHelper.createGateway(gateway);
Expand Down
3 changes: 2 additions & 1 deletion cucumber/steps/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { GatewayHelper } from '../helpers/gatewayHelper';
import { EnvironmentHelper } from '../helpers/environmentHelper';
import { SampleHelper } from '../helpers/sampleHelper';
import { FabricWalletRegistry } from '../../extension/registries/FabricWalletRegistry';
import { FabricGatewayRegistry } from '../../extension/registries/FabricGatewayRegistry';

// tslint:disable:no-unused-expression

Expand Down Expand Up @@ -98,7 +99,6 @@ module.exports = function(): any {
}
await fs.mkdir(tmpRepo);

await TestUtil.storeGatewaysConfig();
await TestUtil.storeRuntimesConfig();
await TestUtil.storeExtensionDirectoryConfig();
await TestUtil.storeRepositoriesConfig();
Expand All @@ -108,6 +108,7 @@ module.exports = function(): any {
await vscode.workspace.getConfiguration().update(SettingConfigurations.EXTENSION_REPOSITORIES, [], vscode.ConfigurationTarget.Global);

await FabricWalletRegistry.instance().clear();
await FabricGatewayRegistry.instance().clear();

await ExtensionUtil.activateExtension();

Expand Down
75 changes: 1 addition & 74 deletions extension/commands/UserInputUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
'use strict';
import * as vscode from 'vscode';
import * as homeDir from 'home-dir';
import * as fs from 'fs-extra';
import * as path from 'path';
import { FabricConnectionManager } from '../fabric/FabricConnectionManager';
Expand All @@ -31,7 +30,6 @@ import { FabricWalletRegistry } from '../registries/FabricWalletRegistry';
import { IFabricEnvironmentConnection } from '../fabric/IFabricEnvironmentConnection';
import { IFabricClientConnection } from '../fabric/IFabricClientConnection';
import { FabricNode, FabricNodeType } from '../fabric/FabricNode';
import { SettingConfigurations } from '../../configurations';
import { FabricEnvironmentRegistryEntry } from '../registries/FabricEnvironmentRegistryEntry';
import { FabricEnvironmentManager } from '../fabric/FabricEnvironmentManager';
import { FabricEnvironment } from '../fabric/FabricEnvironment';
Expand Down Expand Up @@ -174,13 +172,7 @@ export class UserInputUtil {

const allGateways: Array<FabricGatewayRegistryEntry> = [];

if (showManagedRuntime) {
// Allow users to choose local_fabric
const runtimeGateways: Array<FabricGatewayRegistryEntry> = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
allGateways.push(...runtimeGateways);
}

let gateways: Array<FabricGatewayRegistryEntry> = await FabricGatewayRegistry.instance().getAll();
let gateways: Array<FabricGatewayRegistryEntry> = await FabricGatewayRegistry.instance().getAll(showManagedRuntime);

if (showAssociatedGateways !== undefined) {
gateways = gateways.filter((gateway: FabricGatewayRegistryEntry) => {
Expand Down Expand Up @@ -495,71 +487,6 @@ export class UserInputUtil {
return vscode.window.showQuickPick(options, quickPickOptions);
}

public static async openUserSettings(name: string): Promise<void> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();

let settingsPath: string;

try {
// Get the 'user settings' file path
if (process.platform === 'win32') {
// Windows
settingsPath = path.join(process.env.APPDATA, 'Code', 'User', 'settings.json');
} else if (process.platform === 'darwin') {
// Mac
settingsPath = path.join(homeDir(), 'Library', 'Application Support', 'Code', 'User', 'settings.json');
} else {
// Linux
settingsPath = path.join(homeDir(), '.config', 'Code', 'User', 'settings.json');
}

// Open the user settings (but don't display yet)
const document: vscode.TextDocument = await vscode.workspace.openTextDocument(vscode.Uri.file(settingsPath));
const settingsText: string[] = document.getText().split('\n');

let highlightStart: number;

// Find the user settings line number
for (let index: number = 0; index < settingsText.length; index++) {
const section: string = `"${SettingConfigurations.FABRIC_GATEWAYS}": [`;
if (settingsText[index].includes(section)) {
// We've found the section
const entry: string = '"name": "' + name;

for (let searchIndex: number = index; searchIndex < settingsText.length; searchIndex++) {
// Search for the specific name and set the line number if found
if (settingsText[searchIndex].includes(entry)) {
highlightStart = searchIndex;
break;
}
}

if (highlightStart) {
// If the starting highlighting position is found, we can break from the loop
break;
}
}
}

if (!highlightStart) {
outputAdapter.log(LogType.ERROR, `Could not find ${name} entry in user settings`);
await vscode.window.showTextDocument(document);
return;
}

// Define the section to highlight
const startLine: number = highlightStart;
const endLine: number = highlightStart + 2;

// Show the user settings and highlight the connection
await vscode.window.showTextDocument(document, {
selection: new vscode.Range(new vscode.Position(startLine, 0), new vscode.Position(endLine, 0))
});
} catch (error) {
outputAdapter.log(LogType.ERROR, error.message, error.toString());
}
}

public static async showConfirmationWarningMessage(prompt: string): Promise<boolean> {
const reallyDoIt: vscode.MessageItem = await vscode.window.showWarningMessage(prompt, { title: 'Yes' }, { title: 'No' });
if (!reallyDoIt) {
Expand Down
10 changes: 6 additions & 4 deletions extension/commands/addGatewayCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ async function createGatewayFromEnvironment(gatewayName: string, environmentName
const fabricGatewayEntry: FabricGatewayRegistryEntry = new FabricGatewayRegistryEntry();
fabricGatewayEntry.name = gatewayName;
fabricGatewayEntry.associatedWallet = peerNode.wallet;
await FabricGatewayHelper.generateConnectionProfile(gatewayName, peerNode, caNode);

const fabricGatewayRegistry: FabricGatewayRegistry = FabricGatewayRegistry.instance();
await fabricGatewayRegistry.add(fabricGatewayEntry);

await FabricGatewayHelper.generateConnectionProfile(gatewayName, peerNode, caNode);

return fabricGatewayEntry;
}

Expand All @@ -136,13 +138,13 @@ async function createGatewayFromCCP(gatewayName: string): Promise<FabricGatewayR
}

const fabricGatewayEntry: FabricGatewayRegistryEntry = new FabricGatewayRegistryEntry();
// Copy the user given connection profile to the gateway directory (in the blockchain extension directory)
await FabricGatewayHelper.copyConnectionProfile(gatewayName, connectionProfilePath);

fabricGatewayEntry.name = gatewayName;
fabricGatewayEntry.associatedWallet = '';

const fabricGatewayRegistry: FabricGatewayRegistry = FabricGatewayRegistry.instance();
await fabricGatewayRegistry.add(fabricGatewayEntry);
// Copy the user given connection profile to the gateway directory (in the blockchain extension directory)
await FabricGatewayHelper.copyConnectionProfile(gatewayName, connectionProfilePath);

return fabricGatewayEntry;
}
6 changes: 3 additions & 3 deletions extension/commands/addWalletIdentityCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { WalletTreeItem } from '../explorer/wallets/WalletTreeItem';
import { IFabricEnvironmentConnection } from '../fabric/IFabricEnvironmentConnection';
import { FabricEnvironmentManager } from '../fabric/FabricEnvironmentManager';
import { FabricGatewayHelper } from '../fabric/FabricGatewayHelper';
import { FabricRuntimeUtil } from '../fabric/FabricRuntimeUtil';

export async function addWalletIdentity(walletItem: WalletTreeItem | FabricWalletRegistryEntry, mspid: string): Promise<string> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();
Expand Down Expand Up @@ -150,8 +151,7 @@ export async function addWalletIdentity(walletItem: WalletTreeItem | FabricWalle
if (isLocalWallet) {
// wallet is managed so use local_fabric as the gateway
// assume there is only one
const runtimeGateways: Array<FabricGatewayRegistryEntry> = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
gatewayRegistryEntry = runtimeGateways[0];
gatewayRegistryEntry = await FabricGatewayRegistry.instance().get(FabricRuntimeUtil.LOCAL_FABRIC);

// make sure local_fabric is started
const isRunning: boolean = await FabricRuntimeManager.instance().getRuntime().isRunning();
Expand All @@ -168,7 +168,7 @@ export async function addWalletIdentity(walletItem: WalletTreeItem | FabricWalle
// select from other gateways
// Check there is at least one
let gateways: Array<FabricGatewayRegistryEntry> = [];
gateways = await FabricGatewayRegistry.instance().getAll();
gateways = await FabricGatewayRegistry.instance().getAll(false);
if (gateways.length === 0) {
outputAdapter.log(LogType.ERROR, `Please add a gateway in order to enroll a new identity`);
return;
Expand Down
9 changes: 4 additions & 5 deletions extension/commands/associateWalletCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function associateWallet(gatewayTreeItem: GatewayDissociatedTreeIte
// Ask for gateway
// Check there is at least one that is not local_fabric
let gateways: Array<FabricGatewayRegistryEntry> = [];
gateways = await FabricGatewayRegistry.instance().getAll();
gateways = await FabricGatewayRegistry.instance().getAll(false);
if (gateways.length === 0) {
outputAdapter.log(LogType.ERROR, `Add a gateway to associate a wallet. ${FabricRuntimeUtil.LOCAL_FABRIC_DISPLAY_NAME} is associated with ${FabricWalletUtil.LOCAL_WALLET_DISPLAY_NAME}.`, `Add a gateway to associate a wallet. ${FabricRuntimeUtil.LOCAL_FABRIC_DISPLAY_NAME} is associated with ${FabricWalletUtil.LOCAL_WALLET_DISPLAY_NAME}.`);
return;
Expand All @@ -53,7 +53,7 @@ export async function associateWallet(gatewayTreeItem: GatewayDissociatedTreeIte
// Get a wallet to associate
// Check there is at least one that is not local_fabric_wallet
let wallets: Array<FabricWalletRegistryEntry> = [];
wallets = await FabricWalletRegistry.instance().getAll();
wallets = await FabricWalletRegistry.instance().getAll(false);
if (wallets.length === 0) {
outputAdapter.log(LogType.ERROR, `You must first add a wallet, to then associate with this gateway`);
return;
Expand All @@ -71,10 +71,9 @@ export async function associateWallet(gatewayTreeItem: GatewayDissociatedTreeIte
gateway.associatedWallet = associatedWallet;
await fabricGatewayRegistry.update(gateway);

outputAdapter.log(LogType.SUCCESS, `Successfully associated "${associatedWallet}" wallet with "${gateway.name}" gateway`);

} catch (error) {
outputAdapter.log(LogType.ERROR, `Unable to associate wallet: ${error.message}`, `Unable to associate wallet: ${error.toString()}`);
throw new Error(`Unable to associate wallet: ${error.message}`);
}

outputAdapter.log(LogType.SUCCESS, `Successfully associated "${associatedWallet}" wallet with "${gateway.name}" gateway`);
}
7 changes: 4 additions & 3 deletions extension/commands/debugCommandListCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import * as vscode from 'vscode';
import { UserInputUtil, IBlockchainQuickPickItem } from './UserInputUtil';
import { ExtensionCommands } from '../../ExtensionCommands';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { IFabricEnvironmentConnection } from '../fabric/IFabricEnvironmentConnection';
import { FabricConnectionManager } from '../fabric/FabricConnectionManager';
import { FabricGatewayRegistryEntry } from '../registries/FabricGatewayRegistryEntry';
import { FabricEnvironmentManager } from '../fabric/FabricEnvironmentManager';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { FabricRuntimeUtil } from '../fabric/FabricRuntimeUtil';
import { FabricGatewayRegistry } from '../registries/FabricGatewayRegistry';

export async function debugCommandList(commandName?: string): Promise<void> {

Expand Down Expand Up @@ -66,9 +67,9 @@ export async function debugCommandList(commandName?: string): Promise<void> {

if (!FabricConnectionManager.instance().getConnection()) {
// Connect to local_fabric gateway before submitting/evaluating transaction
const runtimeGateways: Array<FabricGatewayRegistryEntry> = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
const runtimeGateway: FabricGatewayRegistryEntry = await FabricGatewayRegistry.instance().get(FabricRuntimeUtil.LOCAL_FABRIC);
// Assume one runtime gateway registry entry
await vscode.commands.executeCommand(ExtensionCommands.CONNECT_TO_GATEWAY, runtimeGateways[0]);
await vscode.commands.executeCommand(ExtensionCommands.CONNECT_TO_GATEWAY, runtimeGateway);
if (!FabricConnectionManager.instance().getConnection()) {
// either the user cancelled or ther was an error so don't carry on
return;
Expand Down
Loading

0 comments on commit d410fed

Please sign in to comment.