github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/config/organization.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  
    22  	mspconfig "github.com/hyperledger/fabric/common/config/msp"
    23  	"github.com/hyperledger/fabric/msp"
    24  	mspprotos "github.com/hyperledger/fabric/protos/msp"
    25  )
    26  
    27  // Org config keys
    28  const (
    29  	// MSPKey is value key for marshaled *mspconfig.MSPConfig
    30  	MSPKey = "MSP"
    31  )
    32  
    33  type OrganizationProtos struct {
    34  	MSP *mspprotos.MSPConfig
    35  }
    36  
    37  type OrganizationConfig struct {
    38  	*standardValues
    39  	protos *OrganizationProtos
    40  
    41  	organizationGroup *OrganizationGroup
    42  
    43  	msp   msp.MSP
    44  	mspID string
    45  }
    46  
    47  // Config stores common configuration information for organizations
    48  type OrganizationGroup struct {
    49  	*Proposer
    50  	*OrganizationConfig
    51  	name             string
    52  	mspConfigHandler *mspconfig.MSPConfigHandler
    53  }
    54  
    55  // NewConfig creates an instnace of the organization Config
    56  func NewOrganizationGroup(name string, mspConfigHandler *mspconfig.MSPConfigHandler) *OrganizationGroup {
    57  	og := &OrganizationGroup{
    58  		name:             name,
    59  		mspConfigHandler: mspConfigHandler,
    60  	}
    61  	og.Proposer = NewProposer(og)
    62  	return og
    63  }
    64  
    65  // Name returns the name this org is referred to in config
    66  func (og *OrganizationGroup) Name() string {
    67  	return og.name
    68  }
    69  
    70  // MSPID returns the MSP ID associated with this org
    71  func (og *OrganizationGroup) MSPID() string {
    72  	return og.mspID
    73  }
    74  
    75  // NewGroup always errors
    76  func (og *OrganizationGroup) NewGroup(name string) (ValueProposer, error) {
    77  	return nil, fmt.Errorf("Organization does not support subgroups")
    78  }
    79  
    80  // Allocate creates the proto resources neeeded for a proposal
    81  func (og *OrganizationGroup) Allocate() Values {
    82  	return NewOrganizationConfig(og)
    83  }
    84  
    85  func NewOrganizationConfig(og *OrganizationGroup) *OrganizationConfig {
    86  	oc := &OrganizationConfig{
    87  		protos: &OrganizationProtos{},
    88  
    89  		organizationGroup: og,
    90  	}
    91  
    92  	var err error
    93  	oc.standardValues, err = NewStandardValues(oc.protos)
    94  	if err != nil {
    95  		logger.Panicf("Programming error: %s", err)
    96  	}
    97  	return oc
    98  }
    99  
   100  // Validate returns whether the configuration is valid
   101  func (oc *OrganizationConfig) Validate(tx interface{}, groups map[string]ValueProposer) error {
   102  	return oc.validateMSP(tx)
   103  }
   104  
   105  func (oc *OrganizationConfig) Commit() {
   106  	oc.organizationGroup.OrganizationConfig = oc
   107  }
   108  
   109  func (oc *OrganizationConfig) validateMSP(tx interface{}) error {
   110  	var err error
   111  
   112  	logger.Debugf("Setting up MSP for org %s", oc.organizationGroup.name)
   113  	oc.msp, err = oc.organizationGroup.mspConfigHandler.ProposeMSP(tx, oc.protos.MSP)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	oc.mspID, _ = oc.msp.GetIdentifier()
   119  
   120  	if oc.mspID == "" {
   121  		return fmt.Errorf("MSP for org %s has empty MSP ID", oc.organizationGroup.name)
   122  	}
   123  
   124  	if oc.organizationGroup.OrganizationConfig != nil && oc.organizationGroup.mspID != oc.mspID {
   125  		return fmt.Errorf("Organization %s attempted to change its MSP ID from %s to %s", oc.organizationGroup.name, oc.organizationGroup.mspID, oc.mspID)
   126  	}
   127  
   128  	return nil
   129  }