dnssecsocket/gorilla-server/main.go

219 lines
5.5 KiB
Go
Raw Permalink Normal View History

package main
import "flag"
import "log"
import "fmt"
import "regexp"
import "net/http"
import "encoding/json"
import "io/ioutil"
import pb "git.wit.com/wit/witProtobuf"
import "github.com/davecgh/go-spew/spew"
import "github.com/tidwall/gjson"
type network struct {
Subdomain string `json:"subdomain"`
}
func fetchNetworks(ge *pb.Event) []network {
var networks []network
req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
if err != nil { log.Fatal("can't make new req") }
req.Header.Set("X-Wit-Auth", ge.Account.Token)
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") }
log.Printf("NETWORKS =", string(body))
if body == nil {
log.Println("networks is nil")
} else {
len := len(body)
err = json.Unmarshal(body, &networks)
if err != nil {
log.Println("body = ", body)
log.Println("body len = ", len)
log.Println("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(ge *pb.Event, network string) []vm {
var vms []vm
req, err := http.NewRequest("GET", ge.Account.URL + "vms/" + network, nil)
if err != nil {
log.Println("fetchVMs() can't make new req")
return vms
}
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("fetchVMs() Can't exec the req to fetch vms for %s: %s", network, err)
return vms
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("fetchVMs() can't read resp")
return vms
}
err = json.Unmarshal(body, &vms)
if err != nil {
log.Println("fetchVMs() can't unmarshal resp for fetch vms: ", err, "\n\n", network, string(body))
return vms
}
spew.Dump(vms)
return vms
}
var addr = flag.String("addr", ":9000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
http.HandleFunc("/event", eventHandler)
log.Println("Starting http.ListenAndServe() on port 9000")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// Gets a list of all the customer VM's
func processGetEvent(ge *pb.Event) *pb.Event {
log.Println("processGetEvent() START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "processGetEvent() LOGIN FAILED on checkLogin(ge)"
log.Println(e.Comment)
return e
}
log.Println("processGetEvent() ge.Account.Token =", ge.Account.Token)
log.Println("processGetEvent() ge.Account.Username =", ge.Account.Username)
log.Println("processGetEvent() ge.Account.Email =", ge.Account.Email)
e := pb.Event{}
networks := fetchNetworks(ge)
for _, network := range(networks) {
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(ge, network.Subdomain)
for _, vm := range(vms) {
evm := pb.Event_VM{}
evm.Name = vm.Name
evm.Cpus = 1
evm.Memory = 512
evm.Disk = 20
// TODO: bmath -- let's change the stack-api to remove this hack
evm.Hostname = vm.Name + "." + enetwork.Name + ".customers.wprod.wit.com"
e.Vms = append(e.Vms, &evm)
log.Println("\tvm.Name =",vm.Name)
log.Println("\tvm =",vm)
}
}
return &e
}
/*
// should check a user can login here
func processLoginEvent(ge *pb.Event) *pb.Event {
log.Println("processLoginEvent() LOGIN START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
log.Println(e.Comment)
return e
}
e := pb.MakeOkResponse()
e.Account = ge.Account
e.Comment = "processLoginEvent() LOGIN WORKED"
log.Println(e.Comment)
return e
}
*/
func JSONtoVM(ge *pb.Event, json string) {
tmp := "network_addr"
value := gjson.Get(json, tmp)
println(tmp, "=", value.String())
tmp = "public_mac"
value = gjson.Get(json, tmp)
println(tmp, "=", value.String())
tmp = "public_addr"
value = gjson.Get(json, tmp)
println(tmp, "=", value.String())
}
func processAddEvent(ge *pb.Event) *pb.Event {
log.Println("processAddEvent() START ge.Account =", ge.Account)
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "processAddEvent() LOGIN FAILED. ge.Account = nil"
log.Println(e.Comment)
return e
}
a := regexp.MustCompile(`\.`) // split subdomain on .
stuff := a.Split(ge.Account.Domain, -1)
log.Printf("processAddEvent() stuff = %q\n", stuff)
log.Println("processAddEvent() host =", stuff[0])
log.Println("processAddEvent() memory =", ge.Vms[0].Memory)
memory := fmt.Sprintf("%d", ge.Vms[0].Memory)
url := ge.Account.URL + "vms/" + stuff[0] + "?count=1&cpu=2&ram=" + memory + "&disk=25"
log.Println("processAddEvent() new URL =", url)
newVM := string(POSTbody(ge, url))
log.Println("processAddEvent() recieved newVM=", newVM)
e := pb.MakeOkResponse()
e.Account = ge.Account
e.Comment = newVM
log.Println(e.Comment)
return e
}