Skip to content

Commit

Permalink
Initial code for ConfigResult
Browse files Browse the repository at this point in the history
This PR modified the method signature for Configure as described in #18.
It doesn't cover all features described in that issue. As it's a
breaking change for the early API it would be good to merge it sooner
rather than later and add to it once it's in place.
  • Loading branch information
ogenstad committed Apr 22, 2021
1 parent 8fb36e4 commit 4786736
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
12 changes: 12 additions & 0 deletions pkg/netrasp/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package netrasp

// ConfigResult is returned by a Configure operation and contains output and results.
type ConfigResult struct {
ConfigCommands []ConfigCommand
}

// ConfigCommand contains a configuration command together with the output from that command.
type ConfigCommand struct {
Command string
Output string
}
17 changes: 9 additions & 8 deletions pkg/netrasp/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ func (i ios) Close(ctx context.Context) error {
}

// Configure device.
func (i ios) Configure(ctx context.Context, commands []string) (string, error) {
var output string
func (i ios) Configure(ctx context.Context, commands []string) (ConfigResult, error) {
var result ConfigResult
_, err := i.Run(ctx, "configure terminal")
if err != nil {
return "", fmt.Errorf("unable to enter config mode: %w", err)
return result, fmt.Errorf("unable to enter config mode: %w", err)
}
for _, command := range commands {
result, err := i.Run(ctx, command)
output, err := i.Run(ctx, command)
configCommand := ConfigCommand{Command: command, Output: output}
result.ConfigCommands = append(result.ConfigCommands, configCommand)
if err != nil {
return output, fmt.Errorf("unable to run command '%s': %w", command, err)
return result, fmt.Errorf("unable to run command '%s': %w", command, err)
}
output += result
}
_, err = i.Run(ctx, "end")
if err != nil {
return output, fmt.Errorf("unable to exit from config mode: %w", err)
return result, fmt.Errorf("unable to exit from config mode: %w", err)
}

return output, nil
return result, nil
}

// Dial opens a connection to a device.
Expand Down
2 changes: 1 addition & 1 deletion pkg/netrasp/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Platform interface {
// Disconnect from a device
Close(context.Context) error
// Configure device with the provided commands
Configure(context.Context, []string) (string, error)
Configure(context.Context, []string) (ConfigResult, error)
// Open a connection to a device
Dial(context.Context) error
// Elevate privileges on device
Expand Down
20 changes: 11 additions & 9 deletions pkg/netrasp/sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,32 @@ func (s sros) Close(ctx context.Context) error {
}

// Configure device.
func (s sros) Configure(ctx context.Context, commands []string) (string, error) {
var output string
func (s sros) Configure(ctx context.Context, commands []string) (ConfigResult, error) {
var result ConfigResult

_, err := s.Run(ctx, "edit-config exclusive")
if err != nil {
return "", fmt.Errorf("unable to enter exclusive edit mode: %w", err)
return result, fmt.Errorf("unable to enter exclusive edit mode: %w", err)
}
for _, command := range commands {
result, err := s.Run(ctx, command)
output, err := s.Run(ctx, command)
configCommand := ConfigCommand{Command: command, Output: output}
result.ConfigCommands = append(result.ConfigCommands, configCommand)
if err != nil {
return output, fmt.Errorf("unable to run command '%s': %w", command, err)
return result, fmt.Errorf("unable to run command '%s': %w", command, err)
}
output += result
}
_, err = s.Run(ctx, "commit")
if err != nil {
return output, fmt.Errorf("unable to commit configuration: %w", err)
return result, fmt.Errorf("unable to commit configuration: %w", err)
}

_, err = s.Run(ctx, "quit-config")
if err != nil {
return output, fmt.Errorf("unable to quit from configuration mode: %w", err)
return result, fmt.Errorf("unable to quit from configuration mode: %w", err)
}

return output, nil
return result, nil
}

// Dial opens a connection to a device.
Expand Down

0 comments on commit 4786736

Please sign in to comment.