blob: ed4a9b36d7beffafe1c29519ebadbf561db935a8 [file] [log] [blame]
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +01001package main
2
3import (
4 "flag"
5 "fmt"
6
Serge Bazanskidd3d40f2018-10-25 12:14:18 +01007 "code.hackerspace.pl/hscloud/go/mirko"
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +01008 "github.com/golang/glog"
Sergiusz Bazanski47b7e852019-09-26 18:32:39 +02009 "github.com/q3k/cursedjsonrpc"
Serge Bazanski9cf869b2018-10-14 08:25:43 -070010
Sergiusz Bazanski61594bb2019-07-21 15:20:51 +020011 pb "code.hackerspace.pl/hscloud/dc/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 {
Sergiusz Bazanski47b7e852019-09-26 18:32:39 +020019 rpc cursedjsonrpc.RPCClient
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010020}
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{
Sergiusz Bazanski47b7e852019-09-26 18:32:39 +020049 rpc: cursedjsonrpc.NewClient(flagAristaAPI),
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010050 }
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}