Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | |
Serge Bazanski | dd3d40f | 2018-10-25 12:14:18 +0100 | [diff] [blame] | 7 | "code.hackerspace.pl/hscloud/go/mirko" |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 8 | "github.com/golang/glog" |
Sergiusz Bazanski | 47b7e85 | 2019-09-26 18:32:39 +0200 | [diff] [blame] | 9 | "github.com/q3k/cursedjsonrpc" |
Serge Bazanski | 9cf869b | 2018-10-14 08:25:43 -0700 | [diff] [blame] | 10 | |
Sergiusz Bazanski | 61594bb | 2019-07-21 15:20:51 +0200 | [diff] [blame] | 11 | pb "code.hackerspace.pl/hscloud/dc/arista-proxy/proto" |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | var ( |
Serge Bazanski | 9cf869b | 2018-10-14 08:25:43 -0700 | [diff] [blame] | 15 | flagAristaAPI string |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | type aristaClient struct { |
Sergiusz Bazanski | 47b7e85 | 2019-09-26 18:32:39 +0200 | [diff] [blame] | 19 | rpc cursedjsonrpc.RPCClient |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | func (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 Bazanski | 9cf869b | 2018-10-14 08:25:43 -0700 | [diff] [blame] | 40 | type server struct { |
| 41 | arista *aristaClient |
| 42 | } |
| 43 | |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 44 | func main() { |
| 45 | flag.StringVar(&flagAristaAPI, "arista_api", "http://admin:password@1.2.3.4:80/command-api", "Arista remote endpoint") |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 46 | flag.Parse() |
| 47 | |
| 48 | arista := &aristaClient{ |
Sergiusz Bazanski | 47b7e85 | 2019-09-26 18:32:39 +0200 | [diff] [blame] | 49 | rpc: cursedjsonrpc.NewClient(flagAristaAPI), |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Serge Bazanski | 9cf869b | 2018-10-14 08:25:43 -0700 | [diff] [blame] | 52 | m := mirko.New() |
| 53 | if err := m.Listen(); err != nil { |
| 54 | glog.Exitf("Listen(): %v", err) |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Serge Bazanski | 9cf869b | 2018-10-14 08:25:43 -0700 | [diff] [blame] | 57 | 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 Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 67 | } |