Skip to content

Commit

Permalink
Merge pull request #43 from scrapli/transport_overhaul
Browse files Browse the repository at this point in the history
Transport overhaul (and then some...)
  • Loading branch information
carlmontanari authored Aug 31, 2021
2 parents 2a9ca5b + 04f5f91 commit c6f2804
Show file tree
Hide file tree
Showing 143 changed files with 5,122 additions and 2,792 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ lint: ## Run linters
golangci-lint run

test: ## Test execution
go test -cover -v ./channel/... ./driver/... ./netconf/... ./logging/... ./transport/...
go test -cover -v ./cfg/... ./channel/... ./driver/... ./netconf/... ./logging/... ./transport/...

test_race: ## Test execution with race flag
go test -cover -race -v ./channel/... ./driver/... ./netconf/... ./logging/... ./transport/...
go test -cover -race -v ./cfg/... ./channel/... ./driver/... ./netconf/... ./logging/... ./transport/...

test_functional: ## Test execution including functional tests
go test -v ./driver/network/... -functional

help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
19 changes: 13 additions & 6 deletions cfg/aristaeos.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func (p *EOSCfg) GetVersion() (string, []*base.Response, error) {
return "", nil, err
}

return p.VersionPattern.FindString(versionResult.Result), []*base.Response{versionResult}, nil
return p.VersionPattern.FindString(
versionResult.Result,
), []*base.Response{
versionResult,
}, nil
}

// GetConfig get the configuration of a source datastore from the device.
Expand Down Expand Up @@ -170,7 +174,7 @@ func (p *EOSCfg) loadConfig(
stdConfig,
base.WithDesiredPrivilegeLevel(p.configSessionName),
)
if stdConfigErr != nil || configResult.Failed {
if stdConfigErr != nil || configResult.Failed != nil {
return scrapliResponses, stdConfigErr
}

Expand All @@ -181,9 +185,12 @@ func (p *EOSCfg) loadConfig(
base.WithDesiredPrivilegeLevel(p.configSessionName),
base.WithSendEager(true),
)

if eagerConfigErr != nil {
return scrapliResponses, eagerConfigErr
} else if eagerResult.Failed {
}

if eagerResult.Failed != nil {
return scrapliResponses, eagerConfigErr
}

Expand Down Expand Up @@ -232,7 +239,7 @@ func (p *EOSCfg) AbortConfig() ([]*base.Response, error) {
return scrapliResponses, err
}

_, err = p.conn.Channel.SendInput("abort", false, false, p.conn.TimeoutOps)
_, err = p.conn.Channel.SendInput("abort", false, false, p.conn.Channel.TimeoutOps)
if err != nil {
return scrapliResponses, err
}
Expand Down Expand Up @@ -322,7 +329,7 @@ func (p *EOSCfg) DiffConfig(

scrapliResponses = append(scrapliResponses, diffResult)

if diffResult.Failed {
if diffResult.Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand All @@ -343,7 +350,7 @@ func (p *EOSCfg) DiffConfig(

scrapliResponses = append(scrapliResponses, getConfigR[0])

if getConfigR[0].Failed {
if getConfigR[0].Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand Down
21 changes: 12 additions & 9 deletions cfg/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"strings"

"github.com/scrapli/scrapligo/driver/base"

"github.com/scrapli/scrapligo/util"

"github.com/scrapli/scrapligo/logging"

"github.com/scrapli/scrapligo/driver/network"
Expand Down Expand Up @@ -53,7 +56,7 @@ type Platform interface {
}

func FormatLogMessage(conn *network.Driver, level, msg string) string {
return logging.FormatLogMessage(level, conn.Host, conn.Port, msg)
return logging.FormatLogMessage(level, conn.Host, conn.Transport.BaseTransportArgs.Port, msg)
}

func setPlatformOptions(p Platform, options ...Option) error {
Expand Down Expand Up @@ -196,7 +199,7 @@ func (d *Cfg) operationOk() error {
}

func (d *Cfg) validateAndSetVersion(versionResponse *Response) error {
if versionResponse.Failed {
if versionResponse.Failed != nil {
logging.LogError(FormatLogMessage(d.conn, "error", "failed getting version from device"))
return ErrVersionError
}
Expand Down Expand Up @@ -341,7 +344,7 @@ func (d *Cfg) GetVersion() (*Response, error) {

r.Record(scrapliResponses, versionString)

if r.Failed {
if r.Failed != nil {
logging.LogDebug(FormatLogMessage(d.conn, "warning", "failed to fetch device version"))
}

Expand Down Expand Up @@ -377,7 +380,7 @@ func (d *Cfg) GetConfig(source string) (*Response, error) {

r.Record(scrapliResponses, cfgString)

if r.Failed {
if r.Failed != nil {
logging.LogError(FormatLogMessage(d.conn, "debug", "failed to fetch config from device"))
}

Expand Down Expand Up @@ -408,7 +411,7 @@ func (d *Cfg) LoadConfig(

r.Record(scrapliResponses, "")

if r.Failed {
if r.Failed != nil {
logging.LogError(
FormatLogMessage(d.conn, "error", "failed to load candidate configuration"),
)
Expand All @@ -427,7 +430,7 @@ func (d *Cfg) LoadConfigFromFile(
FormatLogMessage(d.conn, "info", "load config from file requested"),
)

c, err := base.LoadFileLines(f)
c, err := util.LoadFileLines(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -464,7 +467,7 @@ func (d *Cfg) AbortConfig() (*Response, error) {

r.Record(scrapliResponses, "")

if r.Failed {
if r.Failed != nil {
logging.LogError(
FormatLogMessage(d.conn, "error", "failed to abort candidate configuration"),
)
Expand Down Expand Up @@ -510,7 +513,7 @@ func (d *Cfg) CommitConfig(options ...OperationOption) (*Response, error) {

r.Record(scrapliResponses, "")

if r.Failed {
if r.Failed != nil {
logging.LogError(
FormatLogMessage(d.conn, "error", "failed to commit candidate configuration"),
)
Expand Down Expand Up @@ -560,7 +563,7 @@ func (d *Cfg) DiffConfig(options ...OperationOption) (*DiffResponse, error) {
r.Record(scrapliResponses, "")
r.RecordDiff(sourceConfig, candidateConfig, deviceDiff)

if r.Failed {
if r.Failed != nil {
logging.LogError(
FormatLogMessage(d.conn, "error", "failed to diff configuration"),
)
Expand Down
16 changes: 10 additions & 6 deletions cfg/ciscoiosxe.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func (p *IOSXECfg) GetVersion() (string, []*base.Response, error) {
return "", nil, err
}

return p.VersionPattern.FindString(versionResult.Result), []*base.Response{versionResult}, nil
return p.VersionPattern.FindString(
versionResult.Result,
), []*base.Response{
versionResult,
}, nil
}

// GetConfig get the configuration of a source datastore from the device.
Expand Down Expand Up @@ -200,15 +204,15 @@ func (p *IOSXECfg) LoadConfig(

config = p.prepareConfigPayload(config)

originalReturnChar := p.conn.CommsReturnChar
originalReturnChar := p.conn.Channel.CommsReturnChar
tclCommsReturnChar := "\r"

err = p.conn.AcquirePriv("tclsh")
if err != nil {
return nil, err
}

p.conn.Channel.CommsReturnChar = &tclCommsReturnChar
p.conn.Channel.CommsReturnChar = tclCommsReturnChar

r, err := p.conn.SendConfig(config, base.WithDesiredPrivilegeLevel("tclsh"))
if err != nil {
Expand All @@ -222,7 +226,7 @@ func (p *IOSXECfg) LoadConfig(
return scrapliResponses, err
}

p.conn.Channel.CommsReturnChar = &originalReturnChar
p.conn.Channel.CommsReturnChar = originalReturnChar

return scrapliResponses, nil
}
Expand Down Expand Up @@ -501,7 +505,7 @@ func (p *IOSXECfg) DiffConfig(

scrapliResponses = append(scrapliResponses, diffResult)

if diffResult.Failed {
if diffResult.Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand All @@ -522,7 +526,7 @@ func (p *IOSXECfg) DiffConfig(

scrapliResponses = append(scrapliResponses, getConfigR[0])

if getConfigR[0].Failed {
if getConfigR[0].Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand Down
19 changes: 13 additions & 6 deletions cfg/ciscoiosxr.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ func (p *IOSXRCfg) GetVersion() (string, []*base.Response, error) {
return "", nil, err
}

return p.VersionPattern.FindString(versionResult.Result), []*base.Response{versionResult}, nil
return p.VersionPattern.FindString(
versionResult.Result,
), []*base.Response{
versionResult,
}, nil
}

// GetConfig get the configuration of a source datastore from the device.
Expand Down Expand Up @@ -198,7 +202,7 @@ func (p *IOSXRCfg) LoadConfig(
configResult, stdConfigErr := p.conn.SendConfig(
stdConfig, base.WithDesiredPrivilegeLevel(p.configPrivLevel),
)
if stdConfigErr != nil || configResult.Failed {
if stdConfigErr != nil || configResult.Failed != nil {
return scrapliResponses, stdConfigErr
}

Expand All @@ -209,9 +213,12 @@ func (p *IOSXRCfg) LoadConfig(
base.WithSendEager(true),
base.WithDesiredPrivilegeLevel(p.configPrivLevel),
)

if eagerConfigErr != nil {
return scrapliResponses, eagerConfigErr
} else if eagerResult.Failed {
}

if eagerResult.Failed != nil {
return scrapliResponses, eagerConfigErr
}

Expand All @@ -224,7 +231,7 @@ func (p *IOSXRCfg) LoadConfig(
func (p *IOSXRCfg) AbortConfig() ([]*base.Response, error) {
var scrapliResponses []*base.Response

_, err := p.conn.Channel.SendInput("abort", false, false, p.conn.TimeoutOps)
_, err := p.conn.Channel.SendInput("abort", false, false, p.conn.Channel.TimeoutOps)
if err != nil {
return scrapliResponses, err
}
Expand Down Expand Up @@ -321,7 +328,7 @@ func (p *IOSXRCfg) DiffConfig(

scrapliResponses = append(scrapliResponses, diffResult)

if diffResult.Failed {
if diffResult.Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand All @@ -342,7 +349,7 @@ func (p *IOSXRCfg) DiffConfig(

scrapliResponses = append(scrapliResponses, getConfigR[0])

if getConfigR[0].Failed {
if getConfigR[0].Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand Down
10 changes: 7 additions & 3 deletions cfg/cisconxos.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ func (p *NXOSCfg) GetVersion() (string, []*base.Response, error) {
return "", nil, err
}

return p.VersionPattern.FindString(versionResult.Result), []*base.Response{versionResult}, nil
return p.VersionPattern.FindString(
versionResult.Result,
), []*base.Response{
versionResult,
}, nil
}

// GetConfig get the configuration of a source datastore from the device.
Expand Down Expand Up @@ -349,7 +353,7 @@ func (p *NXOSCfg) DiffConfig(

scrapliResponses = append(scrapliResponses, diffResult)

if diffResult.Failed {
if diffResult.Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand All @@ -371,7 +375,7 @@ func (p *NXOSCfg) DiffConfig(

scrapliResponses = append(scrapliResponses, getConfigR[0])

if getConfigR[0].Failed {
if getConfigR[0].Failed != nil {
logging.LogError(
FormatLogMessage(
p.conn,
Expand Down
29 changes: 29 additions & 0 deletions cfg/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cfg_test

import (
"testing"

"github.com/scrapli/scrapligo/cfg"
"github.com/scrapli/scrapligo/driver/network"
)

func createCfgDriver(t *testing.T, d *network.Driver, platform string) *cfg.Cfg {
openErr := d.Open()
if openErr != nil {
t.Fatalf("failed opening driver: %v", openErr)
}

c, cfgErr := cfg.NewCfgDriver(d, platform)

if cfgErr != nil {
t.Fatalf("failed creating cfg test device: %v", cfgErr)
}

prepareErr := c.Prepare()

if prepareErr != nil {
t.Fatalf("failed running prepare method: %v", prepareErr)
}

return c
}
Loading

0 comments on commit c6f2804

Please sign in to comment.