diff --git a/pkg/netrasp/config.go b/pkg/netrasp/config.go new file mode 100644 index 0000000..9afba5b --- /dev/null +++ b/pkg/netrasp/config.go @@ -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 +} diff --git a/pkg/netrasp/ios.go b/pkg/netrasp/ios.go index 9676f99..8593049 100644 --- a/pkg/netrasp/ios.go +++ b/pkg/netrasp/ios.go @@ -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. diff --git a/pkg/netrasp/platform.go b/pkg/netrasp/platform.go index 990cebb..159696f 100644 --- a/pkg/netrasp/platform.go +++ b/pkg/netrasp/platform.go @@ -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 diff --git a/pkg/netrasp/sros.go b/pkg/netrasp/sros.go index 08e7a54..f07ca4b 100644 --- a/pkg/netrasp/sros.go +++ b/pkg/netrasp/sros.go @@ -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.