github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/peer/config/commoncore/core.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  package commoncore
    19  
    20  import (
    21  	"sigs.k8s.io/yaml"
    22  )
    23  
    24  func ConvertBootstrapToArray(intf interface{}) ([]byte, error) {
    25  	if intf == nil {
    26  		return nil, nil
    27  	}
    28  
    29  	if coreBytes, ok := intf.([]byte); ok {
    30  		return convertBootstrapToArray(coreBytes)
    31  	}
    32  
    33  	bytes, err := yaml.Marshal(intf)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return convertBootstrapToArray(bytes)
    39  }
    40  
    41  // convertBootstrapToArray returns an updated core config where peer.gossip.bootstrap is
    42  // an array of strings ([]string) instead of a string.
    43  //
    44  // Peer.gossip.bootstrap can be passed to the operator as a string or []string in the peer's
    45  // core config; however, the operator defines the field in the Core config struct definition
    46  // as a []string due to how Fabric parses the field
    47  // (https://github.com/hyperledger/fabric/blob/release-1.4/peer/node/start.go#L897).
    48  func convertBootstrapToArray(coreBytes []byte) ([]byte, error) {
    49  	if coreBytes == nil {
    50  		return nil, nil
    51  	}
    52  
    53  	type Core map[string]interface{}
    54  
    55  	coreObj := Core{}
    56  	err := yaml.Unmarshal(coreBytes, &coreObj)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	peer, ok := coreObj["peer"].(map[string]interface{})
    62  	if peer == nil {
    63  		// If peer not found, simply return original config
    64  		return coreBytes, nil
    65  	}
    66  
    67  	gossip, ok := peer["gossip"].(map[string]interface{})
    68  	if !ok {
    69  		// If peer.gossip not found, simply return original config
    70  		return coreBytes, nil
    71  	}
    72  
    73  	bootstrap, ok := gossip["bootstrap"].(string)
    74  	if !ok {
    75  		// If peer.gossip.bootstrap not found or unable to be converted
    76  		// into a string, simply return original config
    77  		return coreBytes, nil
    78  	}
    79  
    80  	if bootstrap == "" {
    81  		gossip["bootstrap"] = nil
    82  	} else {
    83  		gossip["bootstrap"] = []string{bootstrap}
    84  	}
    85  
    86  	newCore, err := yaml.Marshal(coreObj)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	return newCore, nil
    92  }