convert the JSON into a google protobuf

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-12 19:19:15 +00:00
parent 0b6d5fbac9
commit 7bcbe607a0
2 changed files with 129 additions and 2 deletions

View File

@ -19,7 +19,7 @@ var upgrader = websocket.Upgrader{
},
}
func sendProtobuf(conn *websocket.Conn) {
func sendSampleProtobuf(conn *websocket.Conn) {
msg := pb.CreateSampleEvent()
msg.Name = "test echo over gorilla websocket"
data, _ := proto.Marshal(msg)
@ -29,6 +29,14 @@ func sendProtobuf(conn *websocket.Conn) {
}
}
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
}
func eventHandler(w http.ResponseWriter, r *http.Request) {
log.Println("echoHandler() START")
@ -59,7 +67,8 @@ func eventHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("recv binary: %s", pdata)
if pdata.Type == pb.Event_MIGRATE {
log.Printf("GOT MIGRATE")
sendProtobuf(conn)
e := oldmain()
sendProtobuf(conn, e)
}
}
}

View File

@ -0,0 +1,118 @@
package main
import "net/http"
import "encoding/json"
import "io/ioutil"
import "log"
import "github.com/davecgh/go-spew/spew"
// import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
const (
JWT = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjoyLCJyIjoiIiwiY3NyZiI6Ilk3MkZvM1RpQXZVRUl0RHhaRnpsZTZDVCIsImV4cCI6MTU1Nzc3NDM0OCwiaXNzIjoid2l0Iiwic3ViIjoiYm1hdGhAd2l0LmNvbSJ9.AOnsc8XiE9WemSfY2LU4ll_0wZJtLJvlf1Jjy_Cxt89uxc6Mts_dva4w1xQNQAYUaYqc9HPtHIimWxvbApRs45bYq6vNFbOFwieu3XQ9374f0u6g5NZoplURE7WbZvSJ"
API = "http://stackapi:4000/"
MAX_NETS = 10
MAX_VMS = 10
)
type network struct {
Subdomain string `json:"subdomain"`
}
func fetchNetworks() []network {
var networks []network
req, err := http.NewRequest("GET", API + "clusters", nil)
if err != nil { log.Fatal("can't make new req") }
req.Header.Set("X-Wit-Auth", JWT)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { log.Printf("Can't exec the req to list networks: %s", err) }
body, err := ioutil.ReadAll(resp.Body)
if err != nil { log.Fatal("can't read resp") }
err = json.Unmarshal(body, &networks)
if err != nil { log.Fatal("can't unmarshal resp for list networks: %s", err) }
spew.Dump(networks)
return networks
}
/*
{
"name": "vm10",
"cpu": 4,
"ram": 2048,
"created_on": "2019-03-18T19:22:10.755428Z",
"scope": "private",
"role": "k8s-node",
"stage": "Creating...",
"seconds_left": 300,
"base_image": "debcore_guest_latest",
"address_list": [
"192.168.0.1/32"
]
},
*/
type address struct {
Address string
}
type vm struct {
Name string `json:"name"`
Role string `json:"role"`
BaseImage string `json:"base_image"`
}
func fetchVMs(network string) []vm {
var vms []vm
req, err := http.NewRequest("GET", API + "vms/" + network, nil)
if err != nil { log.Fatal("can't make new req") }
req.Header.Set("X-Wit-Auth", JWT)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { log.Printf("Can't exec the req to fetch vms for %s: %s", network, err) }
body, err := ioutil.ReadAll(resp.Body)
if err != nil { log.Fatal("can't read resp") }
err = json.Unmarshal(body, &vms)
if err != nil { log.Fatal("can't unmarshal resp for fetch vms: ", err, "\n\n", network, string(body)) }
spew.Dump(vms)
return vms
}
func oldmain() *pb.Event {
e := pb.Event{}
e.Name = "stablesid.org"
networks := fetchNetworks()
for i, network := range(networks) {
if i >= MAX_NETS {
break
}
log.Println("network.Subdomain =",network.Subdomain)
log.Println("network=",network)
enetwork := pb.Event_Network{}
enetwork.Name = network.Subdomain
e.Networks = append(e.Networks, &enetwork)
vms := fetchVMs(network.Subdomain)
for j, vm := range(vms) {
if j >= MAX_VMS {
break
}
evm := pb.Event_VM{}
evm.Name = vm.Name
evm.Cpus = 1
evm.Hostname = vm.Name + "." + enetwork.Name + ".lab.wit.com"
e.Vms = append(e.Vms, &evm)
log.Println("\tvm.Name =",vm.Name)
log.Println("\tvm =",vm)
}
}
return &e
}