add a simple client to test ADD
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
12ef7e0dcf
commit
7883c0d16d
|
@ -3,3 +3,4 @@ client/client
|
|||
example-protobuf/example-protobuf
|
||||
gorilla-server/gorilla-server
|
||||
emaild/emaild
|
||||
goclient/goclient
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
build:
|
||||
go build
|
||||
./goclient
|
||||
|
||||
run:
|
||||
go run *.go
|
|
@ -0,0 +1,443 @@
|
|||
package main
|
||||
|
||||
import "io"
|
||||
import "log"
|
||||
import "net/http"
|
||||
import "encoding/json"
|
||||
import "io/ioutil"
|
||||
import "bytes"
|
||||
|
||||
import "github.com/golang/protobuf/proto"
|
||||
import "github.com/gorilla/websocket"
|
||||
import "github.com/davecgh/go-spew/spew"
|
||||
|
||||
|
||||
import "github.com/tidwall/gjson"
|
||||
import pb "git.wit.com/wit/witProtobuf"
|
||||
|
||||
// const API = "http://stackapi:4000/"
|
||||
|
||||
type network struct {
|
||||
Subdomain string `json:"subdomain"`
|
||||
}
|
||||
|
||||
func checkLogin(ge *pb.Event) bool {
|
||||
if (ge.Account == nil) {
|
||||
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
||||
return false
|
||||
}
|
||||
if (origLoginCheck(ge) == true) {
|
||||
log.Println("checkLogin() LOGIN WORKED")
|
||||
return true
|
||||
}
|
||||
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
||||
return updateToken(ge)
|
||||
}
|
||||
|
||||
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 =", 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.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 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
|
||||
}
|
||||
|
||||
// Also potentially look at:
|
||||
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 4096,
|
||||
WriteBufferSize: 4096,
|
||||
EnableCompression: true,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
|
||||
log.Println("sendProtobuf() START")
|
||||
data, _ := proto.Marshal(msg)
|
||||
err := conn.WriteMessage(websocket.BinaryMessage, data)
|
||||
if err != nil {
|
||||
log.Println("sendProtobuf() write:", err)
|
||||
}
|
||||
log.Println("sendProtobuf() END")
|
||||
}
|
||||
|
||||
func eventHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("echoHandler() START")
|
||||
}
|
||||
|
||||
func serveHome(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.Error(w, "Not found.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
io.WriteString(w, "<html><body>Echo Server</body></html>")
|
||||
}
|
||||
|
||||
// var addr = flag.String("addr", ":9000", "http service address")
|
||||
|
||||
func main() {
|
||||
ge := pb.MakeOkResponse()
|
||||
c := pb.MakeDefaultConfig()
|
||||
ge.Account = c.Accounts[0]
|
||||
ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6InRBY1p2eXVJbk1YdWUxV0RSbDFIeDI5YSIsImV4cCI6MTU1OTI3MDQwMCwiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.bqXX_6yrUHQGYh3SEmW8ydSa9Xfqx-HIKutTN_GirwhC_VrVX1xJBcgYfjdKGegvwY7Td1vO3rs40Iz7ifcptrtdzJnDX62d_1JJPKBHUQUfnTLr2qoTgaljElFM0Q_e"
|
||||
|
||||
log.Println("processAddEvent() START ge.Account =", ge.Account)
|
||||
|
||||
tmp := string(GETbody(ge, "http://stackapi:4000/me"))
|
||||
log.Println("processAddEvent() recieved json=", tmp)
|
||||
|
||||
tmp = string(GETbody(ge, "http://stackapi:4000/clusters"))
|
||||
log.Println("processAddEvent() recieved json=", tmp)
|
||||
|
||||
// stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25
|
||||
|
||||
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
||||
json := newFetchBody(ge, "POST", url)
|
||||
log.Println("processAddEvent() recieved json=", json)
|
||||
}
|
||||
|
||||
// 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
|
||||
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 {
|
||||
|
||||
// This is the 'orig' code
|
||||
// TODO: phase this out for more common code
|
||||
func origLoginCheck(ge *pb.Event) bool {
|
||||
var url string
|
||||
var httpType string
|
||||
httpType = "GET"
|
||||
url = ge.Account.URL + "me"
|
||||
|
||||
log.Println("origLoginCheck() ", httpType, url)
|
||||
req, err := http.NewRequest(httpType, url, nil)
|
||||
if err != nil {
|
||||
ge.Comment = "origLoginCheck() FAILED can't make new req"
|
||||
log.Println(ge.Comment)
|
||||
return false
|
||||
}
|
||||
req.Header.Set("X-Wit-Auth", ge.Account.Token)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
ge.Comment = "origLoginCheck() FAILED Can't exec the req to list networks " + err.Error()
|
||||
log.Println(ge.Comment)
|
||||
return false
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
ge.Comment = "origLoginCheck() FAILED can't read resp"
|
||||
log.Println(ge.Comment)
|
||||
return false
|
||||
}
|
||||
log.Printf("origLoginCheck() ", "RESPONSE =", string(body))
|
||||
if body == nil {
|
||||
ge.Comment = "origLoginCheck() FAILED RESPONSE is nil"
|
||||
log.Println(ge.Comment)
|
||||
return false
|
||||
} else {
|
||||
len := len(body)
|
||||
log.Println("origLoginCheck() ", "body = ", string(body))
|
||||
log.Println("origLoginCheck() ", "body len = ", len)
|
||||
email := gjson.Get(string(body), "email")
|
||||
log.Println("origLoginCheck() ", "email = ", email.String())
|
||||
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
|
||||
if (email.String() == ge.Account.Username) {
|
||||
ge.Comment = "origLoginCheck() THIS TOTALLY WORKED MAN"
|
||||
log.Println(ge.Comment)
|
||||
return true
|
||||
}
|
||||
}
|
||||
ge.Comment = "origLoginCheck() FAILED "
|
||||
log.Println(ge.Comment)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
func fetchGET(token string, a string) []byte {
|
||||
return fetchJSON("GET", token, a)
|
||||
}
|
||||
|
||||
func fetchPOST(token string, a string) []byte {
|
||||
return fetchJSON("POST", token, a)
|
||||
}
|
||||
|
||||
func fetchJSON(GETorPOST string, token string, a string) []byte {
|
||||
log.Println("GOT HERE: fetchJSON(): ", GETorPOST + ": " + a)
|
||||
req, err := http.NewRequest(token, a, nil)
|
||||
if err != nil {
|
||||
log.Fatal("can't make new req")
|
||||
}
|
||||
|
||||
// always set X-Wit-Auth even on token request
|
||||
if (token == "") {
|
||||
token = "badtoken"
|
||||
}
|
||||
req.Header.Set("X-Wit-Auth", token)
|
||||
|
||||
// needed to allow 'CORS' to work
|
||||
req.Header.Set("Access-Control-Allow-Origin", "*")
|
||||
req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||
|
||||
log.Println("GOT HERE: fetchJSON(): token =", token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("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.Println("GOT HERE: fetchJSON() body =", string(body))
|
||||
return body
|
||||
}
|
||||
|
||||
func updateToken(ge *pb.Event) bool {
|
||||
if ge == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
||||
json := newFetchBody(ge, "POST", url)
|
||||
|
||||
jwt := gjson.Get(json, "jwt")
|
||||
if (jwt.String() == "") {
|
||||
ge.Comment = "updateToken() GOT TOKEN == nil"
|
||||
log.Println("updateToken() ge.Comment =", ge.Comment)
|
||||
return false
|
||||
}
|
||||
if (jwt.String() != ge.Account.Token) {
|
||||
log.Println("updateToken() GOT NEW TOKEN", jwt)
|
||||
ge.Account.Token = jwt.String()
|
||||
}
|
||||
log.Println("updateToken() END")
|
||||
return true
|
||||
}
|
||||
|
||||
// url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
||||
|
||||
func newFetchBody(ge *pb.Event, POSTorGET string, url string) string {
|
||||
if ge == nil {
|
||||
log.Println("newFetchBody() has ge = nil")
|
||||
ge.Comment = "newFetchBody() has ge = nil"
|
||||
return ""
|
||||
}
|
||||
if ge.Account == nil {
|
||||
log.Println("newFetchBody() has ge.Account = nil")
|
||||
ge.Comment = "newFetchBody() has ge.Account = nil"
|
||||
return ""
|
||||
}
|
||||
|
||||
log.Println("newFetchBody()", POSTorGET, "URL =", url)
|
||||
|
||||
var jsonStr = []byte(`{}`)
|
||||
req, err := http.NewRequest(POSTorGET, url, bytes.NewBuffer(jsonStr))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Wit-Auth", "tokenrequest")
|
||||
|
||||
if (POSTorGET == "POST") {
|
||||
log.Println("newFetchBody()", POSTorGET, "ADDING CORS headers")
|
||||
// needed to allow 'CORS' to work
|
||||
req.Header.Set("Access-Control-Allow-Origin", "*")
|
||||
req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
ge.Comment = err.Error()
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
log.Println("newFetchBody() response Body:", string(body))
|
||||
log.Println("newFetchBody() response Status:", resp.Status)
|
||||
log.Println("newFetchBody() response Headers:", resp.Header)
|
||||
|
||||
return string(body)
|
||||
}
|
||||
|
||||
func GETbody(ge *pb.Event, URL string) string {
|
||||
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
||||
req, err := http.NewRequest("GET", URL, nil)
|
||||
if err != nil {
|
||||
log.Println("GETbody() can't make new req")
|
||||
return ""
|
||||
}
|
||||
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)
|
||||
return ""
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Println("can't read resp")
|
||||
return ""
|
||||
}
|
||||
// log.Printf("GETbody() body =", string(body))
|
||||
if body == nil {
|
||||
log.Println("networks is nil")
|
||||
return ""
|
||||
}
|
||||
/*
|
||||
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 string(body)
|
||||
}
|
|
@ -85,13 +85,23 @@ func newFetchBody(ge *pb.Event, POSTorGET string, url string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
log.Println("newFetchBody() URL =", url)
|
||||
log.Println("newFetchBody()", POSTorGET, "URL =", url)
|
||||
|
||||
var jsonStr = []byte(`{}`)
|
||||
req, err := http.NewRequest(POSTorGET, url, bytes.NewBuffer(jsonStr))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Wit-Auth", "tokenrequest")
|
||||
|
||||
if (POSTorGET == "POST") {
|
||||
log.Println("newFetchBody()", POSTorGET, "ADDING CORS headers")
|
||||
// needed to allow 'CORS' to work
|
||||
req.Header.Set("Access-Control-Allow-Origin", "*")
|
||||
req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
|
|
@ -301,6 +301,12 @@ func processAddEvent(ge *pb.Event) *pb.Event {
|
|||
tmp = string(GETbody(ge, "http://stackapi:4000/clusters"))
|
||||
log.Println("processAddEvent() recieved json=", tmp)
|
||||
|
||||
// stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25
|
||||
|
||||
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
||||
json := newFetchBody(ge, "POST", url)
|
||||
log.Println("processAddEvent() recieved json=", json)
|
||||
|
||||
/*
|
||||
m, ok := gjson.Parse(tmp).Value().(map[string]interface{})
|
||||
if !ok {
|
||||
|
@ -310,12 +316,14 @@ func processAddEvent(ge *pb.Event) *pb.Event {
|
|||
log.Println("processAddEvent() m=", m)
|
||||
*/
|
||||
|
||||
/*
|
||||
tmp = string(GETbody(ge, "http://stackapi:4000/vms/jcarr.com"))
|
||||
log.Println("processAddEvent() recieved json=", tmp)
|
||||
|
||||
tmp = string(GETbody(ge, "http://stackapi:4000/vm/v000196"))
|
||||
log.Println("processAddEvent() recieved json=", tmp)
|
||||
JSONtoVM(ge, tmp)
|
||||
*/
|
||||
|
||||
/*
|
||||
url := ge.Account.URL + "me"
|
||||
|
@ -329,9 +337,6 @@ func processAddEvent(ge *pb.Event) *pb.Event {
|
|||
json = newFetchBody(ge, "POST", url)
|
||||
log.Println("processAddEvent() recieved json=", json)
|
||||
|
||||
url = ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
||||
json = newFetchBody(ge, "POST", url)
|
||||
log.Println("processAddEvent() recieved json=", json)
|
||||
*/
|
||||
|
||||
e := pb.MakeOkResponse()
|
||||
|
|
Loading…
Reference in New Issue