Skip to content

Commit

Permalink
Merge pull request #22 from open-traffic-generator/run-opts
Browse files Browse the repository at this point in the history
Improvements for `run` command
  • Loading branch information
bortok authored Oct 12, 2022
2 parents d97f21a + edbdc4a commit e50355b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ otgen display --mode table

## Command reference

### Global options

```Shell
otgen
[--log level] # Logging level: err | warn | info | debug (default "err")
```

### `create` and `add`

Create a new OTG configuration item that can be further passed to stdin of `otgen run` command.
Expand Down Expand Up @@ -65,11 +72,11 @@ otgen create device # Create OTG device configuration

### `run`

Request an OTG API endpoint to run OTG configuration.
Requests OTG API endpoint to apply OTG configuration and run Traffic Flows.

```Shell
otgen run
[--api https://otg-api-endpoint] # OTG server API endpoint (default is https://localhost)
[--api https://otg-api-endpoint] # URL of OTG API endpoint. Overrides ENV:OTG_API (default "https://localhost")
[--insecure] # Ignore X.509 certificate validation
[--file otg.yml | --file otg.json] # OTG model file. If not provided, will use stdin
[--yaml | --json] # Format of OTG input
Expand Down Expand Up @@ -135,6 +142,8 @@ For such parameters it may be more convinient to change default values used by `
Environmental variables is one of the mechanisms used by `otgen` to control default values. See below the full list of the variables recognized by `otgen` to redefine default values.
```Shell
OTG_API # URL of OTG API endpoint
OTG_LOCATION_%PORT_NAME% # location for test port with a name PORT_NAME, for example:
OTG_LOCATION_P1 # location for test port "p1"
OTG_LOCATION_P2 # location for test port "p2"
Expand All @@ -153,6 +162,7 @@ OTG_FLOW_DST_IPV6 # Destination IPv6 address to use for flow
These are the values `otgen` uses if no variables or arguments were provided.
```Shell
export OTG_API="https://localhost"
export OTG_LOCATION_P1="localhost:5555" # ixia-c-traffic-engine for p1 (tx) listening on localhost:5555
export OTG_LOCATION_P2="localhost:5556" # ixia-c-traffic-engine for p2 (rx) listening on localhost:5556
export OTG_FLOW_SMAC_P1="02:00:00:00:01:aa"
Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import (
"github.com/spf13/cobra"
)

const (
LOG_DEFAULT_LEVEL = "err"
)

var logLevel string // Logging level: error | info | debug

// Create a new instance of the logger
var log = logrus.New()

Expand Down Expand Up @@ -60,6 +66,7 @@ func init() {
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otgen.yaml)")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log", "", LOG_DEFAULT_LEVEL, "Logging level: err | warn | info | debug")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down
29 changes: 25 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ import (
"time"

"github.com/open-traffic-generator/snappi/gosnappi"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
OTG_API = "${OTG_API}" // Env var for API endpoint
OTG_DEFAULT_API = "https://localhost" // Default API endpoint value
)

var otgURL string // URL of OTG server API endpoint
var otgIgnoreX509 bool // Ignore X.509 certificate validation of OTG API endpoint
var otgYaml bool // Format of OTG input is YAML. Mutually exclusive with --json
Expand All @@ -45,9 +51,9 @@ var xeta = float32(0.0) // How long to wait before forcing traffic to
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "Request an OTG API endpoint to run OTG model",
Short: "Requests OTG API endpoint to apply OTG configuration and run Traffic Flows",
Long: `
Request an OTG API endpoint to run OTG model.
Requests OTG API endpoint to apply OTG configuration and run Traffic Flows.
For more information, go to https://github.com/open-traffic-generator/otgen
`,
Expand All @@ -67,6 +73,21 @@ For more information, go to https://github.com/open-traffic-generator/otgen

runTraffic(initOTG())
},
PreRunE: func(cmd *cobra.Command, args []string) error {
switch logLevel {
case "err":
log.SetLevel(logrus.ErrorLevel)
case "warn":
log.SetLevel(logrus.WarnLevel)
case "info":
log.SetLevel(logrus.InfoLevel)
case "debug":
log.SetLevel(logrus.DebugLevel)
default:
log.Fatalf("Unsupported log level: %s", logLevel)
}
return nil
},
}

func init() {
Expand All @@ -81,7 +102,7 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
runCmd.Flags().StringVarP(&otgURL, "api", "a", "https://localhost", "URL of OTG API endpoint. Example: https://otg-api-endpoint")
runCmd.Flags().StringVarP(&otgURL, "api", "a", envSubstOrDefault(OTG_API, OTG_DEFAULT_API), "URL of OTG API endpoint. Overrides ENV:OTG_API")
runCmd.Flags().BoolVarP(&otgIgnoreX509, "insecure", "k", false, "Ignore X.509 certificate validation of OTG API endpoint")
runCmd.Flags().BoolVarP(&otgYaml, "yaml", "y", false, "Format of OTG input is YAML. Mutually exclusive with --json. Assumed format by default")
runCmd.Flags().BoolVarP(&otgJson, "json", "j", false, "Format of OTG input is JSON. Mutually exclusive with --yaml")
Expand Down Expand Up @@ -281,7 +302,7 @@ func checkResponse(res interface{}, err error) {
printMetricsResponseRawJson(v)
case gosnappi.ResponseWarning:
for _, w := range v.Warnings() {
log.Info("WARNING:", w)
log.Warn("WARNING:", w)
}
default:
log.Fatal("Unknown response type:", v)
Expand Down

0 comments on commit e50355b

Please sign in to comment.