Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Node struct {
autoRefresh bool
enablePasswd string
versionNumber string
eAPIVersion string
}

// GetConnection returns the EapiConnectionEntity
Expand Down Expand Up @@ -154,15 +155,18 @@ func (n *Node) Refresh() {
//
// Returns:
// Pointer to an EapiReqHandle or error on failure
func GetHandle(n *Node, encoding string) (*EapiReqHandle, error) {
func GetHandle(n *Node, encoding string, version StringOrInt) (*EapiReqHandle, error) {
if strings.ToLower(encoding) != "json" &&
strings.ToLower(encoding) != "text" {
return nil, fmt.Errorf("Invalid encoding specified: %s", encoding)
}
if version == nil {
version = 1
}
if n == nil {
return nil, fmt.Errorf("Invalid node")
}
return &EapiReqHandle{node: n, encoding: encoding}, nil
return &EapiReqHandle{node: n, encoding: encoding, version:version}, nil
}

// GetHandle returns the EapiReqHandle for the connection.
Expand All @@ -172,8 +176,11 @@ func GetHandle(n *Node, encoding string) (*EapiReqHandle, error) {
//
// Returns:
// Pointer to an EapiReqHandle or error on failure
func (n *Node) GetHandle(encoding string) (*EapiReqHandle, error) {
return GetHandle(n, encoding)
func (n *Node) GetHandle(encoding string, version ...StringOrInt) (*EapiReqHandle, error) {
if version == nil {
return GetHandle(n, encoding, 1)
}
return GetHandle(n, encoding, version[0])
}

// GetConfig retrieves the config from the node.
Expand Down Expand Up @@ -373,7 +380,7 @@ func (n *Node) RunCommands(commands []string,
cmds = cmdsToInterface(commands)
}

result, err := n.conn.Execute(cmds, encoding)
result, err := n.conn.Execute(cmds, encoding, n.eAPIVersion)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions eapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ type Request struct {
}

// Parameters ...
type StringOrInt interface{}

type Parameters struct {
Version int `json:"version"`
Version StringOrInt `json:"version"`
Cmds []interface{} `json:"cmds"`
Format string `json:"format"`
}
Expand Down Expand Up @@ -107,6 +109,7 @@ type EapiReqHandle struct {
encoding string
eapiCommands []commandBlock
err error
version StringOrInt
}

// debugJSON prints out []byte JSON data Indented
Expand Down Expand Up @@ -250,7 +253,7 @@ func (handle *EapiReqHandle) Call() error {

commands := handle.getAllCommands()

jsonrsp, err := handle.node.conn.Execute(commands, handle.encoding)
jsonrsp, err := handle.node.conn.Execute(commands, handle.encoding, handle.version)

if err != nil {
return err
Expand Down
24 changes: 12 additions & 12 deletions eapilib.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
// EapiConnectionEntity is an interface representing the ability to execute a
// single json transaction, obtaining the Response for a given Request.
type EapiConnectionEntity interface {
Execute(commands []interface{}, encoding string) (*JSONRPCResponse, error)
Execute(commands []interface{}, encoding string, version StringOrInt) (*JSONRPCResponse, error)
SetTimeout(to uint32)
SetDisableKeepAlive(disableKeepAlive bool)
Error() error
Expand Down Expand Up @@ -81,7 +81,7 @@ type EapiConnection struct {
// Returns:
// pointer to JSONRPCResponse or error on failure
func (conn *EapiConnection) Execute(commands []interface{},
encoding string) (*JSONRPCResponse, error) {
encoding string, version StringOrInt) (*JSONRPCResponse, error) {
if conn == nil {
return &JSONRPCResponse{}, fmt.Errorf("No connection")
}
Expand Down Expand Up @@ -167,8 +167,8 @@ func (conn *EapiConnection) SetDisableKeepAlive(disableKeepAlive bool) {
// entries both can be used. Returns []byte of the built JSON request.
// Successful call returns err == nil.
func buildJSONRequest(commands []interface{},
encoding string, reqid string) ([]byte, error) {
p := Parameters{1, commands, encoding}
encoding string, reqid string, version StringOrInt) ([]byte, error) {
p := Parameters{version, commands, encoding}

req := Request{"2.0", "runCmds", p, reqid}
data, err := json.Marshal(req)
Expand Down Expand Up @@ -273,12 +273,12 @@ func (conn *SocketEapiConnection) send(data []byte) (*JSONRPCResponse, error) {
// Returns:
// pointer to JSONRPCResponse or error on failure
func (conn *SocketEapiConnection) Execute(commands []interface{},
encoding string) (*JSONRPCResponse, error) {
encoding string, version StringOrInt) (*JSONRPCResponse, error) {
if conn == nil {
return &JSONRPCResponse{}, fmt.Errorf("No connection")
}
conn.ClearError()
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()))
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()), version)
if err != nil {
conn.SetError(err)
return &JSONRPCResponse{}, err
Expand Down Expand Up @@ -356,12 +356,12 @@ func (conn *HTTPLocalEapiConnection) send(data []byte) (*JSONRPCResponse, error)
// Returns:
// pointer to JSONRPCResponse or error on failure
func (conn *HTTPLocalEapiConnection) Execute(commands []interface{},
encoding string) (*JSONRPCResponse, error) {
encoding string, version StringOrInt) (*JSONRPCResponse, error) {
if conn == nil {
return &JSONRPCResponse{}, fmt.Errorf("No connection")
}
conn.ClearError()
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()))
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()), version)
if err != nil {
conn.SetError(err)
return &JSONRPCResponse{}, err
Expand Down Expand Up @@ -465,12 +465,12 @@ func (conn *HTTPEapiConnection) send(data []byte) (*JSONRPCResponse, error) {
// Returns:
// pointer to JSONRPCResponse or error on failure
func (conn *HTTPEapiConnection) Execute(commands []interface{},
encoding string) (*JSONRPCResponse, error) {
encoding string, version StringOrInt) (*JSONRPCResponse, error) {
if conn == nil {
return &JSONRPCResponse{}, fmt.Errorf("No connection")
}
conn.ClearError()
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()))
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()), version)
if err != nil {
conn.SetError(err)
return &JSONRPCResponse{}, err
Expand Down Expand Up @@ -582,12 +582,12 @@ func (conn *HTTPSEapiConnection) send(data []byte) (*JSONRPCResponse, error) {
// Returns:
// pointer to JSONRPCResponse or error on failure
func (conn *HTTPSEapiConnection) Execute(commands []interface{},
encoding string) (*JSONRPCResponse, error) {
encoding string, version StringOrInt) (*JSONRPCResponse, error) {
if conn == nil {
return &JSONRPCResponse{}, fmt.Errorf("No connection")
}
conn.ClearError()
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()))
data, err := buildJSONRequest(commands, encoding, strconv.Itoa(os.Getpid()), version)
if err != nil {
conn.SetError(err)
return &JSONRPCResponse{}, err
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/aristanetworks/goeapi
module github.com/kalebris/goeapi

go 1.17

require (
github.com/aristanetworks/goeapi v1.0.0
github.com/mitchellh/mapstructure v1.5.0
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/aristanetworks/goeapi v1.0.0 h1:FjckkjOY32SkmKrqDyBqYu6hN7DaIJuxcii9LLdZqtQ=
github.com/aristanetworks/goeapi v1.0.0/go.mod h1:DcgIvssM+qcRRVICDky/ecT/Gqpx40UQDTYY8Lu/iJ0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec h1:DGmKwyZwEB8dI7tbLt/I/gQuP559o/0FrAkHKlQM/Ks=
Expand Down