Skip to content

Commit

Permalink
[FAB-6654] NodeSDK - get eventhubs for org
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
harrisob committed Oct 18, 2017
1 parent b641c35 commit 0c9ebd1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
40 changes: 38 additions & 2 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions fabric-client/lib/Organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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 + ']' +
'}';
}
Expand Down
10 changes: 8 additions & 2 deletions fabric-client/lib/impl/NetworkConfig_1_0.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]) {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 0c9ebd1

Please sign in to comment.