github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/common/policies/implicitmeta_util.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 policies
    18  
    19  import (
    20  	cb "github.com/hyperledger/fabric/protos/common"
    21  	"github.com/hyperledger/fabric/protos/utils"
    22  )
    23  
    24  // ImplicitMetaPolicyWithSubPolicy creates an implicitmeta policy
    25  func ImplicitMetaPolicyWithSubPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigPolicy {
    26  	return &cb.ConfigPolicy{
    27  		Policy: &cb.Policy{
    28  			Type: int32(cb.Policy_IMPLICIT_META),
    29  			Value: utils.MarshalOrPanic(&cb.ImplicitMetaPolicy{
    30  				Rule:      rule,
    31  				SubPolicy: subPolicyName,
    32  			}),
    33  		},
    34  	}
    35  }
    36  
    37  // TemplateImplicitMetaPolicy creates a policy at the specified path with the given policyName and subPolicyName
    38  func TemplateImplicitMetaPolicyWithSubPolicy(path []string, policyName string, subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigGroup {
    39  	root := cb.NewConfigGroup()
    40  	group := root
    41  	for _, element := range path {
    42  		group.Groups[element] = cb.NewConfigGroup()
    43  		group = group.Groups[element]
    44  	}
    45  
    46  	group.Policies[policyName] = ImplicitMetaPolicyWithSubPolicy(subPolicyName, rule)
    47  	return root
    48  }
    49  
    50  // TemplateImplicitMetaPolicy creates a policy at the specified path with the given policyName
    51  // It utilizes the policyName for the subPolicyName as well, as this is the standard usage pattern
    52  func TemplateImplicitMetaPolicy(path []string, policyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigGroup {
    53  	return TemplateImplicitMetaPolicyWithSubPolicy(path, policyName, policyName, rule)
    54  }
    55  
    56  // TempateImplicitMetaAnyPolicy returns TemplateImplicitMetaPolicy with cb.ImplicitMetaPolicy_ANY as the rule
    57  func TemplateImplicitMetaAnyPolicy(path []string, policyName string) *cb.ConfigGroup {
    58  	return TemplateImplicitMetaPolicy(path, policyName, cb.ImplicitMetaPolicy_ANY)
    59  }
    60  
    61  // TempateImplicitMetaAnyPolicy returns TemplateImplicitMetaPolicy with cb.ImplicitMetaPolicy_ALL as the rule
    62  func TemplateImplicitMetaAllPolicy(path []string, policyName string) *cb.ConfigGroup {
    63  	return TemplateImplicitMetaPolicy(path, policyName, cb.ImplicitMetaPolicy_ALL)
    64  }
    65  
    66  // TempateImplicitMetaAnyPolicy returns TemplateImplicitMetaPolicy with cb.ImplicitMetaPolicy_MAJORITY as the rule
    67  func TemplateImplicitMetaMajorityPolicy(path []string, policyName string) *cb.ConfigGroup {
    68  	return TemplateImplicitMetaPolicy(path, policyName, cb.ImplicitMetaPolicy_MAJORITY)
    69  }