2018-08-27 19:40:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
2018-10-14 15:25:43 +00:00
|
|
|
"code.hackerspace.pl/q3k/mirko"
|
2018-08-27 19:40:10 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/ybbus/jsonrpc"
|
2018-10-14 15:25:43 +00:00
|
|
|
|
2018-10-25 11:10:17 +00:00
|
|
|
pb "code.hackerspace.pl/hscloud/go/svc/arista-proxy/proto"
|
2018-08-27 19:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-14 15:25:43 +00:00
|
|
|
flagAristaAPI string
|
2018-08-27 19:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type aristaClient struct {
|
|
|
|
rpc jsonrpc.RPCClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *aristaClient) structuredCall(res interface{}, command ...string) error {
|
|
|
|
cmd := struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Cmds []string `json:"cmds"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
}{
|
|
|
|
Version: 1,
|
|
|
|
Cmds: command,
|
|
|
|
Format: "json",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.rpc.CallFor(res, "runCmds", cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not execute structured call: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 15:25:43 +00:00
|
|
|
type server struct {
|
|
|
|
arista *aristaClient
|
|
|
|
}
|
|
|
|
|
2018-08-27 19:40:10 +00:00
|
|
|
func main() {
|
|
|
|
flag.StringVar(&flagAristaAPI, "arista_api", "http://admin:password@1.2.3.4:80/command-api", "Arista remote endpoint")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
arista := &aristaClient{
|
|
|
|
rpc: jsonrpc.NewClient(flagAristaAPI),
|
|
|
|
}
|
|
|
|
|
2018-10-14 15:25:43 +00:00
|
|
|
m := mirko.New()
|
|
|
|
if err := m.Listen(); err != nil {
|
|
|
|
glog.Exitf("Listen(): %v", err)
|
2018-08-27 19:40:10 +00:00
|
|
|
}
|
2018-10-14 15:25:43 +00:00
|
|
|
|
|
|
|
s := &server{
|
|
|
|
arista: arista,
|
|
|
|
}
|
|
|
|
pb.RegisterAristaProxyServer(m.GRPC(), s)
|
|
|
|
|
|
|
|
if err := m.Serve(); err != nil {
|
|
|
|
glog.Exitf("Serve(): %v", err)
|
2018-08-27 19:40:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 15:25:43 +00:00
|
|
|
select {}
|
2018-08-27 19:40:10 +00:00
|
|
|
}
|