|
| 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 handlers |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/hyperledger/fabric/common/configtx/api" |
| 23 | + mspconfig "github.com/hyperledger/fabric/common/configtx/handlers/msp" |
| 24 | + "github.com/hyperledger/fabric/msp" |
| 25 | + cb "github.com/hyperledger/fabric/protos/common" |
| 26 | + |
| 27 | + "github.com/op/go-logging" |
| 28 | +) |
| 29 | + |
| 30 | +// Org config keys |
| 31 | +const ( |
| 32 | + // MSPKey is value key for marshaled *mspconfig.MSPConfig |
| 33 | + MSPKey = "MSP" |
| 34 | +) |
| 35 | + |
| 36 | +var logger = logging.MustGetLogger("common/configtx/handlers") |
| 37 | + |
| 38 | +type orgConfig struct { |
| 39 | + msp msp.MSP |
| 40 | +} |
| 41 | + |
| 42 | +// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler |
| 43 | +// In general, it should only be referenced as an Impl for the configtx.Manager |
| 44 | +type OrgConfig struct { |
| 45 | + id string |
| 46 | + pendingConfig *orgConfig |
| 47 | + config *orgConfig |
| 48 | + |
| 49 | + mspConfig *mspconfig.MSPConfigHandler |
| 50 | +} |
| 51 | + |
| 52 | +// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper |
| 53 | +func NewOrgConfig(id string, mspConfig *mspconfig.MSPConfigHandler) *OrgConfig { |
| 54 | + return &OrgConfig{ |
| 55 | + id: id, |
| 56 | + config: &orgConfig{}, |
| 57 | + mspConfig: mspConfig, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// BeginConfig is used to start a new config proposal |
| 62 | +func (oc *OrgConfig) BeginConfig() { |
| 63 | + logger.Debugf("Beginning a possible new org config") |
| 64 | + if oc.pendingConfig != nil { |
| 65 | + logger.Panicf("Programming error, cannot call begin in the middle of a proposal") |
| 66 | + } |
| 67 | + oc.pendingConfig = &orgConfig{} |
| 68 | +} |
| 69 | + |
| 70 | +// RollbackConfig is used to abandon a new config proposal |
| 71 | +func (oc *OrgConfig) RollbackConfig() { |
| 72 | + logger.Debugf("Rolling back proposed org config") |
| 73 | + oc.pendingConfig = nil |
| 74 | +} |
| 75 | + |
| 76 | +// CommitConfig is used to commit a new config proposal |
| 77 | +func (oc *OrgConfig) CommitConfig() { |
| 78 | + logger.Debugf("Committing new org config") |
| 79 | + if oc.pendingConfig == nil { |
| 80 | + logger.Panicf("Programming error, cannot call commit without an existing proposal") |
| 81 | + } |
| 82 | + oc.config = oc.pendingConfig |
| 83 | + oc.pendingConfig = nil |
| 84 | +} |
| 85 | + |
| 86 | +// ProposeConfig is used to add new config to the config proposal |
| 87 | +func (oc *OrgConfig) ProposeConfig(key string, configValue *cb.ConfigValue) error { |
| 88 | + switch key { |
| 89 | + case MSPKey: |
| 90 | + logger.Debugf("Initializing org MSP for id %s", oc.id) |
| 91 | + return oc.mspConfig.ProposeConfig(key, configValue) |
| 92 | + default: |
| 93 | + logger.Warningf("Uknown org config item with key %s", key) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// Handler returns the associated api.Handler for the given path |
| 99 | +func (oc *OrgConfig) Handler(path []string) (api.Handler, error) { |
| 100 | + if len(path) == 0 { |
| 101 | + return oc, nil |
| 102 | + } |
| 103 | + |
| 104 | + return nil, fmt.Errorf("Organizations do not further nesting") |
| 105 | +} |
0 commit comments