Skip to content

Commit

Permalink
[FAB-5192] Return struct from implementations
Browse files Browse the repository at this point in the history
- Modified New functions to return structs
- Updated unit test coverage to skip API and mocks folders
- Modified CA test case to write to /tmp

Change-Id: I3e89083813c1814f0251e6343323ebd3d1b33902
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jul 6, 2017
1 parent f71ee06 commit ab834b8
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 252 deletions.
24 changes: 12 additions & 12 deletions api/apifabca/mocks/mockfabriccaclient.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions api/apifabclient/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,3 @@ type Peer interface {
SetRoles(roles []string)
URL() string
}

// PeersToTxnProcessors converts a slice of Peers to a slice of TxnProposalProcessors
func PeersToTxnProcessors(peers []Peer) []txn.ProposalProcessor {
tpp := make([]txn.ProposalProcessor, len(peers))

for i := range peers {
tpp[i] = peers[i]
}
return tpp
}
65 changes: 33 additions & 32 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var format = logging.MustStringFormatter(

const cmdRoot = "fabric_sdk"

type config struct {
// Config represents the configuration for the client
type Config struct {
networkConfig *apiconfig.NetworkConfig
networkConfigCached bool
}
Expand All @@ -45,7 +46,7 @@ func InitConfig(configFile string) (apiconfig.Config, error) {

// InitConfigWithCmdRoot reads in a config file and allows the
// environment variable prefixed to be specified
func InitConfigWithCmdRoot(configFile string, cmdRootPrefix string) (apiconfig.Config, error) {
func InitConfigWithCmdRoot(configFile string, cmdRootPrefix string) (*Config, error) {
myViper.SetEnvPrefix(cmdRootPrefix)
myViper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
Expand Down Expand Up @@ -79,11 +80,11 @@ func InitConfigWithCmdRoot(configFile string, cmdRootPrefix string) (apiconfig.C
}
logging.SetBackend(backendFormatter).SetLevel(logging.Level(logLevel), "fabric_sdk_go")

return &config{}, nil

return &Config{}, nil
}

func (c *config) CAConfig(org string) (*apiconfig.CAConfig, error) {
// CAConfig returns the CA configuration.
func (c *Config) CAConfig(org string) (*apiconfig.CAConfig, error) {
config, err := c.NetworkConfig()
if err != nil {
return nil, err
Expand All @@ -93,8 +94,8 @@ func (c *config) CAConfig(org string) (*apiconfig.CAConfig, error) {
return &caConfig, nil
}

//GetCAServerCertFiles Read configuration option for the server certificate files
func (c *config) CAServerCertFiles(org string) ([]string, error) {
// CAServerCertFiles Read configuration option for the server certificate files
func (c *Config) CAServerCertFiles(org string) ([]string, error) {
config, err := c.NetworkConfig()
if err != nil {
return nil, err
Expand All @@ -109,8 +110,8 @@ func (c *config) CAServerCertFiles(org string) ([]string, error) {
return certFileModPath, nil
}

//GetCAClientKeyFile Read configuration option for the fabric CA client key file
func (c *config) CAClientKeyFile(org string) (string, error) {
// CAClientKeyFile Read configuration option for the fabric CA client key file
func (c *Config) CAClientKeyFile(org string) (string, error) {
config, err := c.NetworkConfig()
if err != nil {
return "", err
Expand All @@ -120,8 +121,8 @@ func (c *config) CAClientKeyFile(org string) (string, error) {
"$GOPATH", os.Getenv("GOPATH"), -1), nil
}

//GetCAClientCertFile Read configuration option for the fabric CA client cert file
func (c *config) CAClientCertFile(org string) (string, error) {
// CAClientCertFile Read configuration option for the fabric CA client cert file
func (c *Config) CAClientCertFile(org string) (string, error) {
config, err := c.NetworkConfig()
if err != nil {
return "", err
Expand All @@ -132,7 +133,7 @@ func (c *config) CAClientCertFile(org string) (string, error) {
}

// MspID returns the MSP ID for the requested organization
func (c *config) MspID(org string) (string, error) {
func (c *Config) MspID(org string) (string, error) {
config, err := c.NetworkConfig()
if err != nil {
return "", err
Expand All @@ -147,11 +148,11 @@ func (c *config) MspID(org string) (string, error) {

// FabricClientViper returns the internal viper instance used by the
// SDK to read configuration options
func (c *config) FabricClientViper() *viper.Viper {
func (c *Config) FabricClientViper() *viper.Viper {
return myViper
}

func (c *config) cacheNetworkConfiguration() error {
func (c *Config) cacheNetworkConfiguration() error {
err := myViper.UnmarshalKey("client.network", &c.networkConfig)
if err == nil {
c.networkConfigCached = true
Expand All @@ -161,8 +162,8 @@ func (c *config) cacheNetworkConfiguration() error {
return err
}

// GetOrderersConfig returns a list of defined orderers
func (c *config) OrderersConfig() ([]apiconfig.OrdererConfig, error) {
// OrderersConfig returns a list of defined orderers
func (c *Config) OrderersConfig() ([]apiconfig.OrdererConfig, error) {
orderers := []apiconfig.OrdererConfig{}
config, err := c.NetworkConfig()
if err != nil {
Expand All @@ -179,7 +180,7 @@ func (c *config) OrderersConfig() ([]apiconfig.OrdererConfig, error) {
}

// RandomOrdererConfig returns a pseudo-random orderer from the network config
func (c *config) RandomOrdererConfig() (*apiconfig.OrdererConfig, error) {
func (c *Config) RandomOrdererConfig() (*apiconfig.OrdererConfig, error) {
config, err := c.NetworkConfig()
if err != nil {
return nil, err
Expand All @@ -203,7 +204,7 @@ func (c *config) RandomOrdererConfig() (*apiconfig.OrdererConfig, error) {
}

// OrdererConfig returns the requested orderer
func (c *config) OrdererConfig(name string) (*apiconfig.OrdererConfig, error) {
func (c *Config) OrdererConfig(name string) (*apiconfig.OrdererConfig, error) {
config, err := c.NetworkConfig()
if err != nil {
return nil, err
Expand All @@ -218,7 +219,7 @@ func (c *config) OrdererConfig(name string) (*apiconfig.OrdererConfig, error) {

// PeersConfig Retrieves the fabric peers for the specified org from the
// config file provided
func (c *config) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
func (c *Config) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
config, err := c.NetworkConfig()
if err != nil {
return nil, err
Expand All @@ -245,7 +246,7 @@ func (c *config) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
}

// NetworkConfig returns the network configuration defined in the config file
func (c *config) NetworkConfig() (*apiconfig.NetworkConfig, error) {
func (c *Config) NetworkConfig() (*apiconfig.NetworkConfig, error) {
if c.networkConfigCached {
return c.networkConfig, nil
}
Expand All @@ -257,13 +258,13 @@ func (c *config) NetworkConfig() (*apiconfig.NetworkConfig, error) {
}

// IsTLSEnabled is TLS enabled?
func (c *config) IsTLSEnabled() bool {
func (c *Config) IsTLSEnabled() bool {
return myViper.GetBool("client.tls.enabled")
}

// TLSCACertPool ...
// TODO: Should be related to configuration.
func (c *config) TLSCACertPool(tlsCertificate string) (*x509.CertPool, error) {
func (c *Config) TLSCACertPool(tlsCertificate string) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
if tlsCertificate != "" {
rawData, err := ioutil.ReadFile(tlsCertificate)
Expand All @@ -283,7 +284,7 @@ func (c *config) TLSCACertPool(tlsCertificate string) (*x509.CertPool, error) {
}

// TLSCACertPoolFromRoots ...
func (c *config) TLSCACertPoolFromRoots(ordererRootCAs [][]byte) (*x509.CertPool, error) {
func (c *Config) TLSCACertPoolFromRoots(ordererRootCAs [][]byte) (*x509.CertPool, error) {
certPool := x509.NewCertPool()

for _, root := range ordererRootCAs {
Expand All @@ -299,43 +300,43 @@ func (c *config) TLSCACertPoolFromRoots(ordererRootCAs [][]byte) (*x509.CertPool
}

// IsSecurityEnabled ...
func (c *config) IsSecurityEnabled() bool {
func (c *Config) IsSecurityEnabled() bool {
return myViper.GetBool("client.security.enabled")
}

// TcertBatchSize ...
func (c *config) TcertBatchSize() int {
func (c *Config) TcertBatchSize() int {
return myViper.GetInt("client.tcert.batch.size")
}

// SecurityAlgorithm ...
func (c *config) SecurityAlgorithm() string {
func (c *Config) SecurityAlgorithm() string {
return myViper.GetString("client.security.hashAlgorithm")
}

// SecurityLevel ...
func (c *config) SecurityLevel() int {
func (c *Config) SecurityLevel() int {
return myViper.GetInt("client.security.level")

}

// KeyStorePath returns the keystore path used by BCCSP
func (c *config) KeyStorePath() string {
func (c *Config) KeyStorePath() string {
keystorePath := strings.Replace(myViper.GetString("client.keystore.path"),
"$GOPATH", os.Getenv("GOPATH"), -1)
return path.Join(keystorePath, "keystore")
}

// CAKeystorePath returns the same path as KeyStorePath() without the
// CAKeyStorePath returns the same path as KeyStorePath() without the
// 'keystore' directory added. This is done because the fabric-ca-client
// adds this to the path
func (c *config) CAKeyStorePath() string {
func (c *Config) CAKeyStorePath() string {
return strings.Replace(myViper.GetString("client.keystore.path"),
"$GOPATH", os.Getenv("GOPATH"), -1)
}

// CryptoConfigPath ...
func (c *config) CryptoConfigPath() string {
func (c *Config) CryptoConfigPath() string {
return strings.Replace(myViper.GetString("client.cryptoconfig.path"),
"$GOPATH", os.Getenv("GOPATH"), -1)
}
Expand All @@ -356,7 +357,7 @@ func loadCAKey(rawData []byte) (*x509.Certificate, error) {
}

// CSPConfig ...
func (c *config) CSPConfig() *bccspFactory.FactoryOpts {
func (c *Config) CSPConfig() *bccspFactory.FactoryOpts {
return &bccspFactory.FactoryOpts{
ProviderName: "SW",
SwOpts: &bccspFactory.SwOpts{
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,13 @@ func crossCheckWithViperConfig(expected string, actual string, message string, t
t.Fatalf(message)
}
}

func TestInterfaces(t *testing.T) {
var apiConfig api.Config
var config Config

apiConfig = &config
if apiConfig == nil {
t.Fatalf("this shouldn't happen.")
}
}
Loading

0 comments on commit ab834b8

Please sign in to comment.