blob: f417f1fbede4309f1b1006d891a26bfb9c90d51c [file] [log] [blame]
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +01001package main
2
3import (
4 "flag"
5 "fmt"
6
Serge Bazanski9cf869b2018-10-14 08:25:43 -07007 "code.hackerspace.pl/q3k/mirko"
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +01008 "github.com/golang/glog"
9 "github.com/ybbus/jsonrpc"
Serge Bazanski9cf869b2018-10-14 08:25:43 -070010
11 pb "code.hackerspace.pl/q3k/arista-proxy/proto"
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010012)
13
14var (
Serge Bazanski9cf869b2018-10-14 08:25:43 -070015 flagAristaAPI string
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010016)
17
18type aristaClient struct {
19 rpc jsonrpc.RPCClient
20}
21
22func (c *aristaClient) structuredCall(res interface{}, command ...string) error {
23 cmd := struct {
24 Version int `json:"version"`
25 Cmds []string `json:"cmds"`
26 Format string `json:"format"`
27 }{
28 Version: 1,
29 Cmds: command,
30 Format: "json",
31 }
32
33 err := c.rpc.CallFor(res, "runCmds", cmd)
34 if err != nil {
35 return fmt.Errorf("could not execute structured call: %v", err)
36 }
37 return nil
38}
39
Serge Bazanski9cf869b2018-10-14 08:25:43 -070040type server struct {
41 arista *aristaClient
42}
43
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010044func main() {
45 flag.StringVar(&flagAristaAPI, "arista_api", "http://admin:password@1.2.3.4:80/command-api", "Arista remote endpoint")
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010046 flag.Parse()
47
48 arista := &aristaClient{
49 rpc: jsonrpc.NewClient(flagAristaAPI),
50 }
51
Serge Bazanski9cf869b2018-10-14 08:25:43 -070052 m := mirko.New()
53 if err := m.Listen(); err != nil {
54 glog.Exitf("Listen(): %v", err)
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010055 }
56
Serge Bazanski9cf869b2018-10-14 08:25:43 -070057 s := &server{
58 arista: arista,
59 }
60 pb.RegisterAristaProxyServer(m.GRPC(), s)
61
62 if err := m.Serve(); err != nil {
63 glog.Exitf("Serve(): %v", err)
64 }
65
66 select {}
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010067}