From 79b87d498c00afb328661b89282f024ad73c7f30 Mon Sep 17 00:00:00 2001 From: Bela Toros Date: Mon, 28 Aug 2023 12:09:54 +0000 Subject: [PATCH 1/4] hacking in a way the capability of using eapi version 1 or "latest" --- client.go | 17 ++++++++++++----- eapi.go | 7 +++++-- eapilib.go | 24 ++++++++++++------------ go.mod | 2 +- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/client.go b/client.go index f91cfc6..fc723ef 100644 --- a/client.go +++ b/client.go @@ -63,6 +63,7 @@ type Node struct { autoRefresh bool enablePasswd string versionNumber string + eAPIVersion string } // GetConnection returns the EapiConnectionEntity @@ -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. @@ -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. @@ -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 } diff --git a/eapi.go b/eapi.go index 066546e..2b0ae65 100644 --- a/eapi.go +++ b/eapi.go @@ -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"` } @@ -107,6 +109,7 @@ type EapiReqHandle struct { encoding string eapiCommands []commandBlock err error + version StringOrInt } // debugJSON prints out []byte JSON data Indented @@ -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 diff --git a/eapilib.go b/eapilib.go index d3d1423..6736481 100644 --- a/eapilib.go +++ b/eapilib.go @@ -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 @@ -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") } @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/go.mod b/go.mod index 253a19e..19d84de 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/aristanetworks/goeapi +module github.com/kalebris/goeapi-version-parm go 1.17 From 87230f3b8f6062ba17e5f21c7796e18daa56cf9a Mon Sep 17 00:00:00 2001 From: Bela Toros Date: Mon, 28 Aug 2023 12:13:38 +0000 Subject: [PATCH 2/4] fixing a tab vs spaces issue --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index fc723ef..8acfdf8 100644 --- a/client.go +++ b/client.go @@ -63,7 +63,7 @@ type Node struct { autoRefresh bool enablePasswd string versionNumber string - eAPIVersion string + eAPIVersion string } // GetConnection returns the EapiConnectionEntity From 50ce9ec91666b142f7d579c153b82a1bebb77740 Mon Sep 17 00:00:00 2001 From: Bela Toros Date: Fri, 6 Oct 2023 08:07:16 +0000 Subject: [PATCH 3/4] fixing module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 19d84de..127e942 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kalebris/goeapi-version-parm +module github.com/kalebris/goeapi go 1.17 From 4f0a408e0208e19962ae8348323f11d32228fc18 Mon Sep 17 00:00:00 2001 From: Bela Toros Date: Fri, 6 Oct 2023 08:28:40 +0000 Subject: [PATCH 4/4] renaming module --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 127e942..05b8bdb 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ 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 ) diff --git a/go.sum b/go.sum index 73405ce..0f95fc4 100644 --- a/go.sum +++ b/go.sum @@ -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=