hscloud/dc/arista-proxy/main.go
Serge Bazanski 97b5cd7b58 go: re-do the entire thing
This is a mega-change, but attempting to split this up further is
probably not worth the effort.

Summary:

1. Bump up bazel, rules_go, and others.
2. Switch to new go target naming (bye bye go_default_library)
3. Move go deps to go.mod/go.sum, use make gazelle generate from that
4. Bump up Python deps a bit

And also whatever was required to actually get things to work - loads of
small useless changes.

Tested to work on NixOS and Ubuntu 20.04:

   $ bazel build //...
   $ bazel test //...

Change-Id: I8364bdaa1406b9ae4d0385a6b607f3e7989f98a9
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1583
Reviewed-by: q3k <q3k@hackerspace.pl>
2023-09-22 21:50:19 +00:00

67 lines
1.2 KiB
Go

package main
import (
"flag"
"fmt"
"code.hackerspace.pl/hscloud/go/mirko"
"github.com/golang/glog"
cursedjsonrpc "github.com/q3k/cursedjsonrpc"
pb "code.hackerspace.pl/hscloud/dc/arista-proxy/proto"
)
var (
flagAristaAPI string
)
type aristaClient struct {
rpc cursedjsonrpc.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
}
type server struct {
arista *aristaClient
}
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: cursedjsonrpc.NewClient(flagAristaAPI),
}
m := mirko.New()
if err := m.Listen(); err != nil {
glog.Exitf("Listen(): %v", err)
}
s := &server{
arista: arista,
}
pb.RegisterAristaProxyServer(m.GRPC(), s)
if err := m.Serve(); err != nil {
glog.Exitf("Serve(): %v", err)
}
select {}
}