github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/apis/common/common.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 common
    20  
    21  import (
    22  	"fmt"
    23  	"strings"
    24  	"time"
    25  )
    26  
    27  type Duration struct {
    28  	time.Duration `json:",inline"`
    29  }
    30  
    31  // Decode is custom decoder for `envconfig` library, this
    32  // method is used to handle reading in environment variables
    33  // and converting them into the type that is expected in
    34  // our structs
    35  func (d *Duration) Decode(value string) error {
    36  	dur, err := time.ParseDuration(value)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	d.Duration = dur
    42  	return nil
    43  }
    44  
    45  // Unmarshal is custom unmarshaler for github.com/kelseyhightower/envconfig
    46  func (d *Duration) Unmarshal(s string) (err error) {
    47  	if s == "" {
    48  		return
    49  	}
    50  
    51  	d.Duration, err = time.ParseDuration(strings.Trim(string(s), `"`))
    52  	return
    53  }
    54  
    55  func (d *Duration) UnmarshalJSON(b []byte) (err error) {
    56  	if b == nil {
    57  		return
    58  	}
    59  	if string(b) == "null" {
    60  		return
    61  	}
    62  	d.Duration, err = time.ParseDuration(strings.Trim(string(b), `"`))
    63  	return
    64  }
    65  
    66  func (d *Duration) Get() time.Duration {
    67  	return d.Duration
    68  }
    69  
    70  func (d Duration) MarshalJSON() (b []byte, err error) {
    71  	return []byte(fmt.Sprintf(`"%s"`, d.String())), nil
    72  }
    73  
    74  func ParseDuration(d string) (Duration, error) {
    75  	duration, err := time.ParseDuration(strings.Trim(string(d), `"`))
    76  	if err != nil {
    77  		return Duration{}, err
    78  	}
    79  
    80  	return Duration{duration}, nil
    81  }
    82  
    83  func MustParseDuration(d string) Duration {
    84  	duration, err := time.ParseDuration(strings.Trim(string(d), `"`))
    85  	if err != nil {
    86  		return Duration{}
    87  	}
    88  
    89  	return Duration{duration}
    90  }
    91  
    92  func ConvertTimeDuration(d time.Duration) Duration {
    93  	return Duration{d}
    94  }
    95  
    96  type BCCSP struct {
    97  	ProviderName string      `json:"default,omitempty"`
    98  	SW           *SwOpts     `json:"SW,omitempty"`
    99  	PKCS11       *PKCS11Opts `json:"PKCS11,omitempty"`
   100  }
   101  
   102  // SwOpts contains options for the SWFactory
   103  type SwOpts struct {
   104  	SecLevel     int              `json:"security,omitempty"`
   105  	HashFamily   string           `json:"hash,omitempty"`
   106  	FileKeyStore FileKeyStoreOpts `json:"filekeystore,omitempty"`
   107  }
   108  
   109  type PKCS11Opts struct {
   110  	SecLevel     int               `json:"security,omitempty"`
   111  	HashFamily   string            `json:"hash,omitempty"`
   112  	Library      string            `json:"library,omitempty"`
   113  	Label        string            `json:"label,omitempty"`
   114  	Pin          string            `json:"pin,omitempty"`
   115  	Ephemeral    bool              `json:"tempkeys,omitempty"`
   116  	SoftVerify   bool              `json:"softwareVerify,omitempty"`
   117  	Immutable    bool              `json:"immutable,omitempty"`
   118  	FileKeyStore *FileKeyStoreOpts `json:"filekeystore,omitempty"`
   119  }
   120  
   121  type FileKeyStoreOpts struct {
   122  	KeyStorePath string `json:"keystore,omitempty"`
   123  }