blob: 66e8d946b0d921ea2dbfc3711e3f87a113aec6c2 [file] [log] [blame]
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +01001package main
2
3import (
4 "flag"
5 "fmt"
6
7 "github.com/golang/glog"
8 "github.com/ybbus/jsonrpc"
9)
10
11var (
12 flagAristaAPI string
13 flagListenAddress string
14 flagDebugAddress string
15 flagCAPath string
16 flagCertificatePath string
17 flagKeyPath string
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010018)
19
20type aristaClient struct {
21 rpc jsonrpc.RPCClient
22}
23
24func (c *aristaClient) structuredCall(res interface{}, command ...string) error {
25 cmd := struct {
26 Version int `json:"version"`
27 Cmds []string `json:"cmds"`
28 Format string `json:"format"`
29 }{
30 Version: 1,
31 Cmds: command,
32 Format: "json",
33 }
34
35 err := c.rpc.CallFor(res, "runCmds", cmd)
36 if err != nil {
37 return fmt.Errorf("could not execute structured call: %v", err)
38 }
39 return nil
40}
41
42func main() {
43 flag.StringVar(&flagAristaAPI, "arista_api", "http://admin:password@1.2.3.4:80/command-api", "Arista remote endpoint")
Sergiusz Bazanskic3caeff2018-08-27 21:03:03 +010044 flag.StringVar(&flagListenAddress, "listen_address", "127.0.0.1:42000", "gRPC listen address")
45 flag.StringVar(&flagDebugAddress, "debug_address", "127.0.0.1:42001", "Debug HTTP listen address, or empty to disable")
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010046 flag.StringVar(&flagCAPath, "tls_ca_path", "pki/ca.pem", "Path to PKI CA certificate")
47 flag.StringVar(&flagCertificatePath, "tls_certificate_path", "pki/service.pem", "Path to PKI service certificate")
48 flag.StringVar(&flagKeyPath, "tls_key_path", "pki/service-key.pem", "Path to PKI service private key")
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010049 flag.Set("logtostderr", "true")
50 flag.Parse()
51
52 arista := &aristaClient{
53 rpc: jsonrpc.NewClient(flagAristaAPI),
54 }
55
56 opts := &serverOpts{
57 listenAddress: flagListenAddress,
58 debugAddress: flagDebugAddress,
59 tlsCAPath: flagCAPath,
60 tlsCertificatePath: flagCertificatePath,
61 tlsKeyPath: flagKeyPath,
Sergiusz Bazanskie08e6da2018-08-27 20:40:10 +010062 }
63 server, err := newServer(opts, arista)
64 if err != nil {
65 glog.Errorf("Could not create server: %v", err)
66 }
67
68 glog.Info("Starting up...")
69 server.serveForever()
70}