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

Commit b920301

Browse files
[FAB-3324] Get organization units
Change-Id: I9c9dab93e3d71b47fd4a860447bf521d4f4d025f Signed-off-by: biljana lukovic <biljana.lukovic@securekey.com>
1 parent e12e9c5 commit b920301

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

fabric-client/chain.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type Chain interface {
8888
SendTransaction(tx *Transaction) ([]*TransactionResponse, error)
8989
SendInstallProposal(chaincodeName string, chaincodePath string, chaincodeVersion string, chaincodePackage []byte, targets []Peer) ([]*TransactionProposalResponse, string, error)
9090
SendInstantiateProposal(chaincodeName string, chainID string, args []string, chaincodePath string, chaincodeVersion string, targets []Peer) ([]*TransactionProposalResponse, string, error)
91-
91+
GetOrganizationUnits() ([]string, error)
9292
QueryExtensionInterface() ChainExtension
9393
}
9494

@@ -424,6 +424,25 @@ func (c *chain) GetMSPManager() msp.MSPManager {
424424
return c.mspManager
425425
}
426426

427+
// GetOrganizationUnits - to get identifier for the organization configured on the channel
428+
func (c *chain) GetOrganizationUnits() ([]string, error) {
429+
chainMSPManager := c.GetMSPManager()
430+
msps, err := chainMSPManager.GetMSPs()
431+
if err != nil {
432+
logger.Info("Cannot get channel manager")
433+
return nil, fmt.Errorf("Organization uits were not set: %v", err)
434+
}
435+
var orgIdentifiers []string
436+
for _, v := range msps {
437+
orgName, err := v.GetIdentifier()
438+
if err != nil {
439+
logger.Info("Organization does not have an identifier")
440+
}
441+
orgIdentifiers = append(orgIdentifiers, orgName)
442+
}
443+
return orgIdentifiers, nil
444+
}
445+
427446
// Initialize initializes the chain
428447
/**
429448
* Retrieve the configuration from the orderer and initializes this chain (channel)

fabric-client/chain_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,60 @@ func TestChainInitializeFromOrderer(t *testing.T) {
314314
}
315315
}
316316

317+
func TestOrganizationUnits(t *testing.T) {
318+
org1MSPID := "ORG1MSP"
319+
org2MSPID := "ORG2MSP"
320+
321+
chain, _ := setupTestChain()
322+
orgUnits, err := chain.GetOrganizationUnits()
323+
if len(orgUnits) > 0 {
324+
t.Fatalf("Returned non configured organizational unit : %v", err)
325+
}
326+
builder := &mocks.MockConfigBlockBuilder{
327+
MockConfigGroupBuilder: mocks.MockConfigGroupBuilder{
328+
ModPolicy: "Admins",
329+
MSPNames: []string{
330+
chain.GetName(),
331+
org1MSPID,
332+
org2MSPID,
333+
},
334+
OrdererAddress: "localhost:7054",
335+
},
336+
Index: 0,
337+
LastConfigIndex: 0,
338+
}
339+
orderer := &mockOrderer{DeliverResponse: NewMockDeliverResponse(builder.Build())}
340+
chain.AddOrderer(orderer)
341+
342+
err = chain.Initialize(nil)
343+
if err != nil {
344+
t.Fatalf("channel Initialize failed : %v", err)
345+
}
346+
orgUnits, err = chain.GetOrganizationUnits()
347+
if err != nil {
348+
t.Fatalf("CANNOT retrieve organizational units : %v", err)
349+
}
350+
if !isValueInList(chain.GetName(), orgUnits) {
351+
t.Fatalf("Could not find %s in the list of organizations", chain.GetName())
352+
}
353+
if !isValueInList(org1MSPID, orgUnits) {
354+
t.Fatalf("Could not find %s in the list of organizations", org1MSPID)
355+
}
356+
if !isValueInList(org2MSPID, orgUnits) {
357+
t.Fatalf("Could not find %s in the list of organizations", org2MSPID)
358+
}
359+
360+
}
361+
362+
func isValueInList(value string, list []string) bool {
363+
for _, v := range list {
364+
if v == value {
365+
return true
366+
}
367+
}
368+
return false
369+
}
370+
317371
func TestChainInitializeFromUpdate(t *testing.T) {
318372
org1MSPID := "ORG1MSP"
319373
org2MSPID := "ORG2MSP"

0 commit comments

Comments
 (0)