github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/common/reconcilechecks/images/fabricversion.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 19 package images 20 21 import ( 22 "fmt" 23 24 current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" 25 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/deployer" 26 ) 27 28 // FabricVersion handles validation on fabric version 29 type FabricVersion struct { 30 Versions *deployer.Versions 31 } 32 33 //go:generate counterfeiter -o mocks/fabricversion.go -fake-name FabricVersionInstance . FabricVersionInstance 34 35 // FabricVersionInstance defines the contract expected from instances 36 type FabricVersionInstance interface { 37 GetFabricVersion() string 38 } 39 40 // Normalize normalizes the fabric version to x.x.x-x 41 func (fv *FabricVersion) Normalize(instance FabricVersionInstance) string { 42 var v interface{} 43 44 switch instance.(type) { 45 case *current.IBPCA: 46 v = fv.Versions.CA 47 case *current.IBPPeer: 48 v = fv.Versions.Peer 49 case *current.IBPOrderer: 50 v = fv.Versions.Orderer 51 } 52 53 return normalizeFabricVersion(instance.GetFabricVersion(), v) 54 } 55 56 // Validate will interate through the keys in versions map and check to 57 // see if versions is present (valid) 58 func (fv *FabricVersion) Validate(instance FabricVersionInstance) error { 59 fabricVersion := instance.GetFabricVersion() 60 61 switch instance.(type) { 62 case *current.IBPCA: 63 _, found := fv.Versions.CA[fabricVersion] 64 if !found { 65 return fmt.Errorf("fabric version '%s' is not supported for CA", fabricVersion) 66 } 67 case *current.IBPPeer: 68 _, found := fv.Versions.Peer[fabricVersion] 69 if !found { 70 return fmt.Errorf("fabric version '%s' is not supported for Peer", fabricVersion) 71 } 72 case *current.IBPOrderer: 73 _, found := fv.Versions.Orderer[fabricVersion] 74 if !found { 75 return fmt.Errorf("fabric version '%s' is not supported for Orderer", fabricVersion) 76 } 77 } 78 79 return nil 80 }