github.com/ewagmig/fabric@v2.1.1+incompatible/common/channelconfig/organization.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	"fmt"
    11  
    12  	cb "github.com/hyperledger/fabric-protos-go/common"
    13  	mspprotos "github.com/hyperledger/fabric-protos-go/msp"
    14  	"github.com/hyperledger/fabric/msp"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  const (
    19  	// MSPKey is the key for the MSP definition in orderer groups
    20  	MSPKey = "MSP"
    21  )
    22  
    23  // OrganizationProtos are used to deserialize the organization config
    24  type OrganizationProtos struct {
    25  	MSP *mspprotos.MSPConfig
    26  }
    27  
    28  // OrganizationConfig stores the configuration for an organization
    29  type OrganizationConfig struct {
    30  	protos *OrganizationProtos
    31  
    32  	mspConfigHandler *MSPConfigHandler
    33  	msp              msp.MSP
    34  	mspID            string
    35  	name             string
    36  }
    37  
    38  // NewOrganizationConfig creates a new config for an organization
    39  func NewOrganizationConfig(name string, orgGroup *cb.ConfigGroup, mspConfigHandler *MSPConfigHandler) (*OrganizationConfig, error) {
    40  	if len(orgGroup.Groups) > 0 {
    41  		return nil, fmt.Errorf("organizations do not support sub-groups")
    42  	}
    43  
    44  	oc := &OrganizationConfig{
    45  		protos:           &OrganizationProtos{},
    46  		name:             name,
    47  		mspConfigHandler: mspConfigHandler,
    48  	}
    49  
    50  	if err := DeserializeProtoValuesFromGroup(orgGroup, oc.protos); err != nil {
    51  		return nil, errors.Wrap(err, "failed to deserialize values")
    52  	}
    53  
    54  	if err := oc.Validate(); err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	return oc, nil
    59  }
    60  
    61  // Name returns the name this org is referred to in config
    62  func (oc *OrganizationConfig) Name() string {
    63  	return oc.name
    64  }
    65  
    66  // MSPID returns the MSP ID associated with this org
    67  func (oc *OrganizationConfig) MSPID() string {
    68  	return oc.mspID
    69  }
    70  
    71  // MSP returns the actual MSP implementation for this org.
    72  func (oc *OrganizationConfig) MSP() msp.MSP {
    73  	return oc.msp
    74  }
    75  
    76  // Validate returns whether the configuration is valid
    77  func (oc *OrganizationConfig) Validate() error {
    78  	return oc.validateMSP()
    79  }
    80  
    81  func (oc *OrganizationConfig) validateMSP() error {
    82  	var err error
    83  
    84  	logger.Debugf("Setting up MSP for org %s", oc.name)
    85  	oc.msp, err = oc.mspConfigHandler.ProposeMSP(oc.protos.MSP)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	oc.mspID, _ = oc.msp.GetIdentifier()
    91  
    92  	if oc.mspID == "" {
    93  		return fmt.Errorf("MSP for org %s has empty MSP ID", oc.name)
    94  	}
    95  
    96  	return nil
    97  }