From 0c9ebd11d13f1914318ce0f8cfd0ce9255300c54 Mon Sep 17 00:00:00 2001 From: Bret Harrison Date: Wed, 18 Oct 2017 06:21:47 -0400 Subject: [PATCH] [FAB-6654] NodeSDK - get eventhubs for org When using a connection profile there is a need to be able to get a list of eventhubs that are for a specific organization. The convenience method should also allow a direct access to the list of the org the client is currently in. Change-Id: I23722b09a0e0a8e7cbbecb42f29a38466caed9e3 Signed-off-by: Bret Harrison --- fabric-client/lib/Client.js | 40 +++++++++++++++++++-- fabric-client/lib/Organization.js | 22 ++++++++++++ fabric-client/lib/impl/NetworkConfig_1_0.js | 10 ++++-- test/unit/network-config.js | 5 +++ 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/fabric-client/lib/Client.js b/fabric-client/lib/Client.js index bf314c4485..9a79afbded 100644 --- a/fabric-client/lib/Client.js +++ b/fabric-client/lib/Client.js @@ -259,15 +259,51 @@ var Client = class extends BaseClient { return event_hub; } - getEventHub(name) { + /** + * Returns and {@link EventHub} object based on the event hub address + * as defined in the currently loaded network configuration for the + * peer by the name parameter. The named peer must have the "eventUrl" + * setting or a null will be returned. + * + * @param {string} peer_name - The name of the peer that has an event hub defined + * @returns {EventHub} The EventHub instance that has had the event hub address assigned + */ + getEventHub(peer_name) { var event_hub = null; if(this._network_config) { - event_hub = this._network_config.getEventHub(name); + event_hub = this._network_config.getEventHub(peer_name); } return event_hub; } + /** + * Returns a list of {@link EventHub} for the named organization as defined + * in the currently loaded network configuration. If no organization is + * provided then the organization named in the currently active network + * configuration's client section will be used. The list will be based on + * the peers in the organization that have the "eventUrl" setting. + * + * @param {string} org_name - Optional - The name of an organization + * @returns {[EventHub]} An array of EventHub instances that are defined for this organization + */ + getEventHubsForOrg(org_name) { + var event_hubs = []; + if(this._network_config) { + if(!org_name && this._network_config.hasClient()) { + let client = this._network_config.getClientConfig(); + org_name = client.organization; + } + if(org_name) { + let organization = this._network_config.getOrganization(org_name); + if(organization) { + event_hubs = organization.getEventHubs(); + } + } + } + + return event_hubs; + } /** * Returns an {@link Orderer} object with the given url and opts. An orderer object * encapsulates the properties of an orderer node and the interactions with it via diff --git a/fabric-client/lib/Organization.js b/fabric-client/lib/Organization.js index c6e586e50c..d290b57711 100644 --- a/fabric-client/lib/Organization.js +++ b/fabric-client/lib/Organization.js @@ -46,6 +46,7 @@ var Organization = class { this._name = name; this._mspid = mspid; this._peers = []; + this._event_hubs = []; this._certificateAuthorities = []; this._adminPrivateKeyPEM = null; this._adminCertPEM = null; @@ -87,6 +88,24 @@ var Organization = class { return this._peers; } + /** + * Add a {@link EventHub} to this organizations + * + * @param {EventHub} event_hub - The event hub instance to add to this organizations list of event hubs + */ + addEventHub(event_hub) { + this._event_hubs.push(event_hub); + } + + /** + * Gets the list of this organizations {@link EventHub} + * + * @returns [{EventHub}] An array of {@link EventHub} objects + */ + getEventHubs() { + return this._event_hubs; + } + /** * Add a {@link CertificateAuthority} to this organization * @@ -137,12 +156,15 @@ var Organization = class { toString() { var peers = ''; this._peers.forEach((peer) => {peers = peers + peer.toString() + ',';}); + var ehs = '' + this._event_hubs.forEach((event_hub) => {ehs = ehs + event_hub.toString() + ',';}); var cas = ''; this._certificateAuthorities.forEach((ca) => {cas = cas + ca.toString() + ',';}); return ' Organization : {' + 'name : ' + this._name + ', mspid : ' + this._mspid + ', peers : [' + peers + ']' + + ', event hubs : [' + ehs + ']' + ', certificateAuthorities : [' + cas + ']' + '}'; } diff --git a/fabric-client/lib/impl/NetworkConfig_1_0.js b/fabric-client/lib/impl/NetworkConfig_1_0.js index ff963f5bda..12a86081ff 100644 --- a/fabric-client/lib/impl/NetworkConfig_1_0.js +++ b/fabric-client/lib/impl/NetworkConfig_1_0.js @@ -183,7 +183,7 @@ var NetworkConfig_1_0 = class { var event_hub = null; if(this._network_config && this._network_config[PEERS_CONFIG]) { let peer_config = this._network_config[PEERS_CONFIG][name]; - if(peer_config) { + if(peer_config && peer_config[EVENT_URL]) { let opts = {}; opts.pem = getTLSCACert(peer_config); Object.assign(opts, peer_config[GRPC_CONNECTION_OPTIONS]); @@ -225,7 +225,13 @@ var NetworkConfig_1_0 = class { for(let i in organization_config[PEERS_CONFIG]) { let peer_name = organization_config[PEERS_CONFIG][i]; let peer = this.getPeer(peer_name); - if(peer) organization.addPeer(peer); + if(peer) { + organization.addPeer(peer); + let event_hub = this.getEventHub(peer_name); + if(event_hub) { + organization.addEventHub(event_hub); + } + } } } if(organization_config[CAS_CONFIG]) { diff --git a/test/unit/network-config.js b/test/unit/network-config.js index e215b0c9fe..3d7685599d 100644 --- a/test/unit/network-config.js +++ b/test/unit/network-config.js @@ -104,6 +104,10 @@ test('\n\n ** configuration testing **\n\n', function (t) { client.loadFromConfig({ version:'1.0.0', client : {organization : 'Org2'}}); t.equals('Org2', client._network_config._network_config.client.organization, ' org should be Org2'); var channel = client.getChannel('mychannel2'); + let event_hubs = client.getEventHubsForOrg(); + t.equals('localhost:8053', event_hubs[0].getPeerAddr(), ' Check to see if we got the right event hub'); + event_hubs = client.getEventHubsForOrg('Org1'); + t.equals('localhost:7053', event_hubs[0].getPeerAddr(), ' Check to see if we got the right event hub'); }, null, '2 Should be able to instantiate a new instance of "Channel" with the definition in the network configuration' @@ -427,6 +431,7 @@ test('\n\n ** configuration testing **\n\n', function (t) { var organization = client._network_config.getOrganization(organizations[0].getName()); var ca = organization.getCertificateAuthorities()[0]; t.equals('ca1',ca.getName(),'check the ca name'); + t.equals(organization.getEventHubs().length,0,'Check that there are no event hubs'); organization = client._network_config.getOrganization(organizations[1].getName()); ca = organization.getCertificateAuthorities()[0]; t.equals('ca2',ca.getName(),'check the ca name');