Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 227d4ed

Browse files
committed
[FAB-11063] IgnoreEndpoint minor fixes
- addressed code review commets from previous push Change-Id: Ifb164b27fac502939eb154d120aed0866810b4b0 Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent 3701453 commit 227d4ed

File tree

3 files changed

+90
-63
lines changed

3 files changed

+90
-63
lines changed

pkg/fab/endpointconfig.go

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,7 @@ func (c *EndpointConfig) OrderersConfig() []fab.OrdererConfig {
128128

129129
// OrdererConfig returns the requested orderer
130130
func (c *EndpointConfig) OrdererConfig(nameOrURL string) (*fab.OrdererConfig, bool) {
131-
132-
matchingOrdererConfig := c.tryMatchingOrdererConfig(nameOrURL, true)
133-
134-
if matchingOrdererConfig == nil {
135-
logger.Debugf("Could not find Orderer for [%s] ", nameOrURL)
136-
return nil, false
137-
}
138-
139-
return matchingOrdererConfig, true
131+
return c.tryMatchingOrdererConfig(nameOrURL, true)
140132
}
141133

142134
// PeersConfig Retrieves the fabric peers for the specified org from the
@@ -148,14 +140,7 @@ func (c *EndpointConfig) PeersConfig(org string) ([]fab.PeerConfig, bool) {
148140

149141
// PeerConfig Retrieves a specific peer from the configuration by name or url
150142
func (c *EndpointConfig) PeerConfig(nameOrURL string) (*fab.PeerConfig, bool) {
151-
152-
matchPeerConfig := c.tryMatchingPeerConfig(nameOrURL, true)
153-
if matchPeerConfig == nil {
154-
logger.Debugf("Could not find Peer for [%s] ", nameOrURL)
155-
return nil, false
156-
}
157-
158-
return matchPeerConfig, true
143+
return c.tryMatchingPeerConfig(nameOrURL, true)
159144
}
160145

161146
// NetworkConfig returns the network configuration defined in the config file
@@ -914,8 +899,8 @@ func (c *EndpointConfig) loadPeerConfigsByOrg() {
914899
peers := []fab.PeerConfig{}
915900

916901
for _, peerName := range orgPeers {
917-
p := c.tryMatchingPeerConfig(peerName, false)
918-
if p == nil {
902+
p, ok := c.tryMatchingPeerConfig(peerName, false)
903+
if !ok {
919904
continue
920905
}
921906

@@ -953,8 +938,8 @@ func (c *EndpointConfig) loadOrdererConfigs() error {
953938
ordererConfigs := []fab.OrdererConfig{}
954939
for name := range c.networkConfig.Orderers {
955940

956-
matchedOrderer := c.tryMatchingOrdererConfig(name, false)
957-
if matchedOrderer == nil {
941+
matchedOrderer, ok := c.tryMatchingOrdererConfig(name, false)
942+
if !ok {
958943
continue
959944
}
960945

@@ -979,8 +964,8 @@ func (c *EndpointConfig) loadChannelPeers() error {
979964
for channelID, channelConfig := range c.networkConfig.Channels {
980965
peers := []fab.ChannelPeer{}
981966
for peerName, chPeerConfig := range channelConfig.Peers {
982-
p := c.tryMatchingPeerConfig(strings.ToLower(peerName), false)
983-
if p == nil {
967+
p, ok := c.tryMatchingPeerConfig(strings.ToLower(peerName), false)
968+
if !ok {
984969
continue
985970
}
986971

@@ -1016,8 +1001,8 @@ func (c *EndpointConfig) loadChannelOrderers() error {
10161001
orderers := []fab.OrdererConfig{}
10171002
for _, ordererName := range channelConfig.Orderers {
10181003

1019-
orderer := c.tryMatchingOrdererConfig(strings.ToLower(ordererName), false)
1020-
if orderer == nil {
1004+
orderer, ok := c.tryMatchingOrdererConfig(strings.ToLower(ordererName), false)
1005+
if !ok {
10211006
return errors.Errorf("Could not find Orderer Config for channel orderer [%s]", ordererName)
10221007
}
10231008
orderers = append(orderers, *orderer)
@@ -1102,7 +1087,7 @@ func (c *EndpointConfig) isOrdererToBeIgnored(ordererName string) bool {
11021087
return false
11031088
}
11041089

1105-
func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL bool) *fab.PeerConfig {
1090+
func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL bool) (*fab.PeerConfig, bool) {
11061091

11071092
//loop over peer entity matchers to find the matching peer
11081093
for _, matcher := range c.peerMatchers {
@@ -1115,14 +1100,14 @@ func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL
11151100
//direct lookup if peer matchers are not configured or no matchers matched
11161101
peerConfig, ok := c.networkConfig.Peers[strings.ToLower(peerSearchKey)]
11171102
if ok {
1118-
return &peerConfig
1103+
return &peerConfig, true
11191104
}
11201105

11211106
if searchByURL {
11221107
//lookup by URL
11231108
for _, staticPeerConfig := range c.networkConfig.Peers {
11241109
if strings.EqualFold(staticPeerConfig.URL, peerSearchKey) {
1125-
return &staticPeerConfig
1110+
return &staticPeerConfig, true
11261111
}
11271112
}
11281113
}
@@ -1136,22 +1121,22 @@ func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL
11361121
// }
11371122
//}
11381123

1139-
return nil
1124+
return nil, false
11401125
}
11411126

1142-
func (c *EndpointConfig) matchPeer(peerSearchKey string, matcher matcherEntry) *fab.PeerConfig {
1127+
func (c *EndpointConfig) matchPeer(peerSearchKey string, matcher matcherEntry) (*fab.PeerConfig, bool) {
11431128

11441129
if matcher.matchConfig.IgnoreEndpoint {
11451130
logger.Debugf(" Ignoring peer `%s` since entity matcher IgnoreEndpoint flag is on", peerSearchKey)
1146-
return nil
1131+
return nil, false
11471132
}
11481133

11491134
mappedHost := c.regexMatchAndReplace(matcher.regex, peerSearchKey, matcher.matchConfig.MappedHost)
11501135

11511136
matchedPeer := c.getMappedPeer(mappedHost)
11521137
if matchedPeer == nil {
11531138
logger.Debugf("Could not find mapped host [%s] for peer [%s]", matcher.matchConfig.MappedHost, peerSearchKey)
1154-
return nil
1139+
return nil, false
11551140
}
11561141

11571142
//URLSubstitutionExp if found use from entity matcher otherwise use from mapped host
@@ -1174,7 +1159,7 @@ func (c *EndpointConfig) matchPeer(peerSearchKey string, matcher matcherEntry) *
11741159
matchedPeer.URL = c.getDefaultMatchingURL(peerSearchKey)
11751160
}
11761161

1177-
return matchedPeer
1162+
return matchedPeer, true
11781163
}
11791164

11801165
//getDefaultMatchingURL if search key is a URL then returns search key as URL otherwise returns empty
@@ -1206,7 +1191,7 @@ func (c *EndpointConfig) getMappedPeer(host string) *fab.PeerConfig {
12061191
return &mappedConfig
12071192
}
12081193

1209-
func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searchByURL bool) *fab.OrdererConfig {
1194+
func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searchByURL bool) (*fab.OrdererConfig, bool) {
12101195

12111196
//loop over orderer entity matchers to find the matching orderer
12121197
for _, matcher := range c.ordererMatchers {
@@ -1219,14 +1204,14 @@ func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searc
12191204
//direct lookup if orderer matchers are not configured or no matchers matched
12201205
orderer, ok := c.networkConfig.Orderers[strings.ToLower(ordererSearchKey)]
12211206
if ok {
1222-
return &orderer
1207+
return &orderer, true
12231208
}
12241209

12251210
if searchByURL {
12261211
//lookup by URL
12271212
for _, ordererCfg := range c.OrderersConfig() {
12281213
if strings.EqualFold(ordererCfg.URL, ordererSearchKey) {
1229-
return &ordererCfg
1214+
return &ordererCfg, true
12301215
}
12311216
}
12321217
}
@@ -1240,14 +1225,14 @@ func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searc
12401225
// }
12411226
//}
12421227

1243-
return nil
1228+
return nil, false
12441229
}
12451230

1246-
func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEntry) *fab.OrdererConfig {
1231+
func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEntry) (*fab.OrdererConfig, bool) {
12471232

12481233
if matcher.matchConfig.IgnoreEndpoint {
12491234
logger.Debugf(" Ignoring peer `%s` since entity matcher IgnoreEndpoint flag is on", ordererSearchKey)
1250-
return nil
1235+
return nil, false
12511236
}
12521237

12531238
mappedHost := c.regexMatchAndReplace(matcher.regex, ordererSearchKey, matcher.matchConfig.MappedHost)
@@ -1256,7 +1241,7 @@ func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEn
12561241
matchedOrderer := c.getMappedOrderer(mappedHost)
12571242
if matchedOrderer == nil {
12581243
logger.Debugf("Could not find mapped host [%s] for orderer [%s]", matcher.matchConfig.MappedHost, ordererSearchKey)
1259-
return nil
1244+
return nil, false
12601245
}
12611246

12621247
//URLSubstitutionExp if found use from entity matcher otherwise use from mapped host
@@ -1274,7 +1259,7 @@ func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEn
12741259
matchedOrderer.URL = c.getDefaultMatchingURL(ordererSearchKey)
12751260
}
12761261

1277-
return matchedOrderer
1262+
return matchedOrderer, true
12781263
}
12791264

12801265
func (c *EndpointConfig) getMappedOrderer(host string) *fab.OrdererConfig {

pkg/fab/matchers_test.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,29 +405,62 @@ func getBackendsFromFiles(files ...string) ([]core.ConfigBackend, error) {
405405
// orderer excluded in orderer search by URL
406406
// peer/orderer excluded in networkconfig
407407
func TestMatchersIgnoreEndpoint(t *testing.T) {
408+
409+
//prepare backends for test
408410
backends, err := getBackendsFromFiles(sampleMatchersIgnoreEndpoint, configTestFilePath)
409411
assert.Nil(t, err, "not supposed to get error")
410412
assert.Equal(t, 2, len(backends))
411413

414+
//get config from backend
412415
config, err := ConfigFromBackend(backends...)
413416
assert.Nil(t, err, "not supposed to get error")
414417
assert.NotNil(t, config)
415418

416419
//Test if orderer excluded in channel orderers
420+
testIgnoreEndpointChannelOrderers(t, config)
421+
422+
//Test if peer excluded in channel peers
423+
testIgnoreEndpointChannelPeers(t, config)
424+
425+
//Test if orderer/peer excluded in channel config
426+
testIgnoreEndpointChannelConfig(t, config)
427+
428+
//Test if peer excluded in org peers
429+
testIgnoreEndpointOrgPeers(t, config)
430+
431+
//Test if peer excluded in network peers
432+
testIgnoreEndpointNetworkPeers(t, config)
433+
434+
//Test if peer excluded in peer search by URL
435+
testIgnoreEndpointPeerSearch(t, config)
436+
437+
//Test if orderer excluded in all orderers
438+
testIgnoreEndpointAllOrderers(t, config)
439+
440+
//Test if orderer excluded in orderer search by name/URL
441+
testIgnoreEndpointOrdererSearch(t, config)
442+
443+
//test NetworkConfig
444+
testIgnoreEndpointNetworkConfig(t, config)
445+
}
446+
447+
func testIgnoreEndpointChannelOrderers(t *testing.T, config fab.EndpointConfig) {
417448
orderers, ok := config.ChannelOrderers(testChannelID)
418449
assert.True(t, ok)
419450
assert.NotEmpty(t, orderers)
420451
assert.Equal(t, 1, len(orderers))
421452
checkOrdererConfigExcluded(orderers, "orderer.exclude.example.com", t)
453+
}
422454

423-
//Test if peer excluded in channel peers
455+
func testIgnoreEndpointChannelPeers(t *testing.T, config fab.EndpointConfig) {
424456
channelPeers, ok := config.ChannelPeers(testChannelID)
425457
assert.True(t, ok)
426458
assert.NotEmpty(t, channelPeers)
427459
assert.Equal(t, 2, len(channelPeers))
428460
checkChannelPeerExcluded(channelPeers, "peer1.org", t)
461+
}
429462

430-
//Test if orderer/peer excluded in channel config
463+
func testIgnoreEndpointChannelConfig(t *testing.T, config fab.EndpointConfig) {
431464
chNwConfig, ok := config.ChannelConfig(testChannelID)
432465
assert.True(t, ok)
433466
assert.NotNil(t, chNwConfig)
@@ -437,10 +470,11 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
437470
_, ok = chNwConfig.Peers["peer1.org2.example.com"]
438471
assert.False(t, ok, "should not have excluded peer's entry in channel network config")
439472
assert.NotEmpty(t, chNwConfig.Orderers)
440-
assert.Equal(t, 1, len(orderers))
441-
assert.NotEqual(t, "orderer.exclude.example.com", orderers[0])
473+
assert.Equal(t, 1, len(chNwConfig.Orderers))
474+
assert.NotEqual(t, "orderer.exclude.example.com", chNwConfig.Orderers[0])
475+
}
442476

443-
//Test if peer excluded in org peers
477+
func testIgnoreEndpointOrgPeers(t *testing.T, config fab.EndpointConfig) {
444478
// test org 1 peers
445479
orgPeers, ok := config.PeersConfig("org1")
446480
assert.True(t, ok)
@@ -452,11 +486,15 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
452486
assert.True(t, ok)
453487
assert.NotEmpty(t, orgPeers)
454488
checkPeerConfigExcluded(orgPeers, "peer1.org2", t)
489+
}
455490

456-
//Test if peer excluded in network peers
491+
func testIgnoreEndpointNetworkPeers(t *testing.T, config fab.EndpointConfig) {
457492
nwPeers := config.NetworkPeers()
458493
assert.NotEmpty(t, nwPeers)
459494
checkNetworkPeerExcluded(nwPeers, "peer1.org", t)
495+
}
496+
497+
func testIgnoreEndpointPeerSearch(t *testing.T, config fab.EndpointConfig) {
460498

461499
//Test if peer excluded in peer search by URL
462500
peerConfig, ok := config.PeerConfig("peer1.org1.example.com:7151")
@@ -492,15 +530,18 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
492530
peerConfig, ok = config.PeerConfig("peer0.org2.example.com")
493531
assert.True(t, ok)
494532
assert.NotNil(t, peerConfig)
533+
}
495534

496-
//Test if orderer excluded in all orderers
497-
535+
func testIgnoreEndpointAllOrderers(t *testing.T, config fab.EndpointConfig) {
498536
ordererConfigs := config.OrderersConfig()
499-
assert.True(t, ok)
500537
assert.NotEmpty(t, ordererConfigs)
501538
checkOrdererConfigExcluded(ordererConfigs, "orderer.exclude.", t)
539+
}
540+
541+
func testIgnoreEndpointOrdererSearch(t *testing.T, config fab.EndpointConfig) {
502542

503543
//Test if orderer excluded in orderer search by name
544+
504545
ordererConfig, ok := config.OrdererConfig("orderer.exclude.example.com")
505546
assert.False(t, ok)
506547
assert.Nil(t, ordererConfig)
@@ -536,12 +577,14 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
536577
assert.False(t, ok)
537578
assert.Nil(t, ordererConfig)
538579

539-
//test NetworkConfig
580+
}
581+
582+
func testIgnoreEndpointNetworkConfig(t *testing.T, config fab.EndpointConfig) {
540583
networkConfig := config.NetworkConfig()
541584
assert.NotNil(t, networkConfig)
542585
assert.Equal(t, 2, len(networkConfig.Peers))
543586
assert.Equal(t, 1, len(networkConfig.Orderers))
544-
_, ok = networkConfig.Peers["peer1.org1.example.com"]
587+
_, ok := networkConfig.Peers["peer1.org1.example.com"]
545588
assert.False(t, ok)
546589
_, ok = networkConfig.Peers["peer1.org2.example.com"]
547590
assert.False(t, ok)
@@ -553,7 +596,6 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
553596
assert.False(t, ok)
554597
_, ok = networkConfig.Orderers["orderer.example.com"]
555598
assert.True(t, ok)
556-
557599
}
558600

559601
func checkOrdererConfigExcluded(ordererConfigs []fab.OrdererConfig, excluded string, t *testing.T) {

0 commit comments

Comments
 (0)