-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI get config + version commands + tests (#176)
Signed-off-by: May Rosenbaum <[email protected]>
- Loading branch information
1 parent
700f92b
commit 01845b7
Showing
16 changed files
with
1,045 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Config Orion via CLI | ||
|
||
This command-line tool provides a simple way to config an orion database server. | ||
|
||
## Building the tool | ||
1. Run from `orion-sdk` root folder | ||
2. Run `make binary` to create an executable file named bcdbadmin under `bin` directory. | ||
|
||
## Commands | ||
|
||
Here we list and describe the available commands. | ||
We give a short explanation of their usage and describe the flags for each command. | ||
We provide real-world examples demonstrating how to use the CLI tool for various tasks. | ||
|
||
|
||
### Version Command | ||
This command prints the version of the CLI tool. | ||
1. Run from `orion-sdk` root folder. | ||
2. Run `./bin/bcdbadmin version`. This command has no flags. | ||
|
||
|
||
|
||
### Config Command | ||
This command enables to config an orion server or ask for the configuration of an orion server. | ||
|
||
#### Get Config Command | ||
1. Run from 'orion-sdk' root folder. | ||
2. For Get Config Run `bin/bcdbadmin config get [args]`. | ||
|
||
Replace `[args]` with flags. | ||
|
||
### | ||
##### Flags | ||
| Flags | Description | | ||
|-----------------------------------|-------------------------------------------------------------------------------| | ||
| `-d, --db-connection-config-path` | the absolute or relative path of CLI connection configuration file | | ||
| `-c, --cluster-config-path` | the absolute or relative path to which the server configuration will be saved | | ||
|
||
Both flags are necessary flags. If any flag is missing, the cli will raise an error. | ||
|
||
### | ||
##### Example: | ||
|
||
Running | ||
`bin/bcdbadmin config get -d "connection-session-config.yaml" -c "local/config"` | ||
reads the connection and session details needed for connecting to a server from `connection-session-config.yaml` and | ||
sends a config TX. | ||
It creates directories in `local/config` with the respective certificates, a yaml file, named shared_cluster_config.yml, that includes the cluster configuration | ||
and a yaml file, named version.yml, that includes the version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func adminCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "admin", | ||
Short: "manage administrators", | ||
Args: nil, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
} | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "add", | ||
Short: "Add an admin", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
}) | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "remove", | ||
Short: "Remove an admin", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
}) | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "update", | ||
Short: "Update an admin", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
}) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package commands | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hyperledger-labs/orion-sdk-go/examples/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAdminCommand(t *testing.T) { | ||
// 1. Create crypto material and start server | ||
tempDir, err := os.MkdirTemp(os.TempDir(), "Cli-Admin-Test") | ||
require.NoError(t, err) | ||
|
||
testServer, _, _, err := util.SetupTestEnv(t, tempDir, uint32(6003)) | ||
require.NoError(t, err) | ||
defer testServer.Stop() | ||
util.StartTestServer(t, testServer) | ||
|
||
// 2. Check admin command response | ||
rootCmd := InitializeOrionCli() | ||
rootCmd.SetArgs([]string{"admin"}) | ||
err = rootCmd.Execute() | ||
require.Error(t, err) | ||
require.Equal(t, err.Error(), "not implemented yet") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func casCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "CAs", | ||
Short: "manage CA's", | ||
Args: nil, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
} | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "add", | ||
Short: "Add CA", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
}) | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "remove", | ||
Short: "Remove CA", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("not implemented yet") | ||
}, | ||
}) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package commands | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hyperledger-labs/orion-sdk-go/examples/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCasCommand(t *testing.T) { | ||
// 1. Create crypto material and start server | ||
tempDir, err := os.MkdirTemp(os.TempDir(), "Cli-Cas-Test") | ||
require.NoError(t, err) | ||
|
||
testServer, _, _, err := util.SetupTestEnv(t, tempDir, uint32(6003)) | ||
require.NoError(t, err) | ||
defer testServer.Stop() | ||
util.StartTestServer(t, testServer) | ||
|
||
// 2. Check cas command response | ||
rootCmd := InitializeOrionCli() | ||
rootCmd.SetArgs([]string{"CAs"}) | ||
err = rootCmd.Execute() | ||
require.Error(t, err) | ||
require.Equal(t, err.Error(), "not implemented yet") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/hyperledger-labs/orion-sdk-go/pkg/bcdb" | ||
"github.com/hyperledger-labs/orion-sdk-go/pkg/config" | ||
"github.com/hyperledger-labs/orion-server/pkg/logger" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type cliConnectionConfig struct { | ||
ConnectionConfig config.ConnectionConfig `yaml:"connection"` | ||
SessionConfig config.SessionConfig `yaml:"session"` | ||
} | ||
|
||
type cliConfigParams struct { | ||
cliConfigPath string | ||
cliConfig cliConnectionConfig | ||
db bcdb.BCDB | ||
session bcdb.DBSession | ||
} | ||
|
||
// CreateDbAndOpenSession read connection and session configurations to create a db instance and open a session with the server. | ||
func (c *cliConfigParams) CreateDbAndOpenSession() error { | ||
var err error | ||
if err = c.cliConfig.ReadAndConstructCliConnConfig(c.cliConfigPath); err != nil { | ||
return errors.Wrapf(err, "failed to read CLI configuration file") | ||
} | ||
|
||
c.db, err = bcdb.Create(&c.cliConfig.ConnectionConfig) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to instanciate a databse connection") | ||
} | ||
|
||
c.session, err = c.db.Session(&c.cliConfig.SessionConfig) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to instanciate a databse session") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ReadAndConstructCliConnConfig read unmarshal the yaml config file into a cliConnectionConfig object | ||
func (c *cliConnectionConfig) ReadAndConstructCliConnConfig(filePath string) error { | ||
if filePath == "" { | ||
return errors.New("path to the shared configuration file is empty") | ||
} | ||
|
||
v := viper.New() | ||
v.SetConfigFile(filePath) | ||
|
||
if err := v.ReadInConfig(); err != nil { | ||
return errors.Wrapf(err, "error reading shared config file: %s", filePath) | ||
} | ||
|
||
if err := v.UnmarshalExact(c); err != nil { | ||
return errors.Wrapf(err, "unable to unmarshal shared config file: '%s' into struct", filePath) | ||
} | ||
|
||
clientLogger, err := logger.New( | ||
&logger.Config{ | ||
Level: "debug", | ||
OutputPath: []string{"stdout"}, | ||
ErrOutputPath: []string{"stderr"}, | ||
Encoding: "console", | ||
Name: "bcdb-client", | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
c.ConnectionConfig.Logger = clientLogger | ||
|
||
return nil | ||
} |
Oops, something went wrong.