ok. ADD works from the GUI
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
1cb13782e2
commit
7c5b906f5f
|
@ -28,8 +28,8 @@ func main() {
|
||||||
log.Println("processAddEvent() recieved json=", tmp)
|
log.Println("processAddEvent() recieved json=", tmp)
|
||||||
|
|
||||||
url := ge.Account.URL + "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"
|
||||||
tmp = string(POSTbody(ge, url))
|
newVM := string(POSTbody(ge, url))
|
||||||
log.Println("processAddEvent() recieved json=", tmp)
|
log.Println("processAddEvent() recieved newVM=", newVM)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if a user can login here
|
// check if a user can login here
|
||||||
|
|
|
@ -1,52 +1,79 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
import "net/http"
|
import "net/http"
|
||||||
import "io/ioutil"
|
import "io/ioutil"
|
||||||
import "log"
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
import "github.com/tidwall/gjson"
|
import "github.com/tidwall/gjson"
|
||||||
import pb "git.wit.com/wit/witProtobuf"
|
import pb "git.wit.com/wit/witProtobuf"
|
||||||
|
|
||||||
func fetchGET(token string, a string) []byte {
|
/*
|
||||||
return fetchJSON("GET", token, a)
|
func main() {
|
||||||
|
ge := pb.MakeOkResponse()
|
||||||
|
c := pb.MakeDefaultConfig()
|
||||||
|
ge.Account = c.Accounts[0]
|
||||||
|
ge.Account.Email = "jcarr@wit.com"
|
||||||
|
ge.Account.Password = "yowzayowza"
|
||||||
|
ge.Account.URL = "http://stackapi:4000/"
|
||||||
|
// ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6Ijloa0VYa2M0YURxTlVqSElGU2VJYUJoOCIsImV4cCI6MTU1OTY3OTAxMywiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.eCfhg3_VYBZh1ve69p0Op4U9L2T9CAZY5qPtx7vNswe3TB-y1nz2X2vw4AzPwfy4fACKeQGkJmWGedt1NKQP-WaZNVtGpgX9g0yvloCob2OMQwsLhq3X7e83weiK_Jlk"
|
||||||
|
ge.Account.Token = "badtoken"
|
||||||
|
|
||||||
|
junkEvent := processLoginEvent(ge)
|
||||||
|
log.Println("processAddEvent() junkEvent =", junkEvent)
|
||||||
|
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)
|
||||||
|
|
||||||
|
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
||||||
|
newVM := string(POSTbody(ge, url))
|
||||||
|
log.Println("processAddEvent() recieved newVM=", newVM)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// check if a user can login here
|
||||||
|
// tries generates a new TOKEN if the old one doesn't work
|
||||||
|
// fails with the appropriate protobuf response to send
|
||||||
|
// to the GUI client
|
||||||
|
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 fetchPOST(token string, a string) []byte {
|
func checkLogin(ge *pb.Event) bool {
|
||||||
return fetchJSON("POST", token, a)
|
if (ge.Account == nil) {
|
||||||
}
|
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
||||||
|
return false
|
||||||
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
|
url := ge.Account.URL + "me"
|
||||||
if (token == "") {
|
json := string(GETbody(ge, url))
|
||||||
token = "badtoken"
|
if (json != "") {
|
||||||
|
email := gjson.Get(json, "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
|
||||||
}
|
}
|
||||||
req.Header.Set("X-Wit-Auth", token)
|
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
||||||
|
return updateToken(ge)
|
||||||
// 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 {
|
func updateToken(ge *pb.Event) bool {
|
||||||
|
@ -55,7 +82,8 @@ func updateToken(ge *pb.Event) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
||||||
json := newFetchBody(ge, "POST", url)
|
// json := newFetchBody(ge, "POST", url)
|
||||||
|
json := POSTbody(ge, url)
|
||||||
|
|
||||||
jwt := gjson.Get(json, "jwt")
|
jwt := gjson.Get(json, "jwt")
|
||||||
if (jwt.String() == "") {
|
if (jwt.String() == "") {
|
||||||
|
@ -71,54 +99,6 @@ func updateToken(ge *pb.Event) bool {
|
||||||
return true
|
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 {
|
func GETbody(ge *pb.Event, URL string) string {
|
||||||
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
||||||
req, err := http.NewRequest("GET", URL, nil)
|
req, err := http.NewRequest("GET", URL, nil)
|
||||||
|
@ -143,15 +123,33 @@ func GETbody(ge *pb.Event, URL string) string {
|
||||||
log.Println("networks is nil")
|
log.Println("networks is nil")
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
/*
|
return string(body)
|
||||||
len := len(body)
|
}
|
||||||
err = json.Unmarshal(body, &networks)
|
|
||||||
if err != nil {
|
func POSTbody(ge *pb.Event, URL string) string {
|
||||||
log.Println("body = ", body)
|
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
||||||
log.Println("body len = ", len)
|
log.Println("POSTbody() url =", URL)
|
||||||
log.Println("can't unmarshal resp for list networks: %s", err)
|
req, err := http.NewRequest("POST", URL, nil)
|
||||||
}
|
if err != nil {
|
||||||
*/
|
log.Println("POSTbody() can't make new req")
|
||||||
// spew.Dump(networks)
|
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 ""
|
||||||
|
}
|
||||||
return string(body)
|
return string(body)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ type network struct {
|
||||||
Subdomain string `json:"subdomain"`
|
Subdomain string `json:"subdomain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func checkLogin(ge *pb.Event) bool {
|
func checkLogin(ge *pb.Event) bool {
|
||||||
if (ge.Account == nil) {
|
if (ge.Account == nil) {
|
||||||
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
||||||
|
@ -31,6 +32,7 @@ func checkLogin(ge *pb.Event) bool {
|
||||||
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
||||||
return updateToken(ge)
|
return updateToken(ge)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func fetchNetworks(ge *pb.Event) []network {
|
func fetchNetworks(ge *pb.Event) []network {
|
||||||
var networks []network
|
var networks []network
|
||||||
|
@ -181,7 +183,7 @@ func eventHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
e.Comment = "websocket.BinaryMessage LOGIN FAILED"
|
e.Comment = "websocket.BinaryMessage LOGIN FAILED"
|
||||||
}
|
}
|
||||||
e.Account = pdata.Account
|
e.Account = pdata.Account
|
||||||
log.Println("\t", e.Comment)
|
log.Println("\teventHandler() e.Comment =", e.Comment)
|
||||||
sendProtobuf(conn, e)
|
sendProtobuf(conn, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -255,6 +257,7 @@ func processGetEvent(ge *pb.Event) *pb.Event {
|
||||||
return &e
|
return &e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// should check a user can login here
|
// should check a user can login here
|
||||||
func processLoginEvent(ge *pb.Event) *pb.Event {
|
func processLoginEvent(ge *pb.Event) *pb.Event {
|
||||||
log.Println("processLoginEvent() LOGIN START")
|
log.Println("processLoginEvent() LOGIN START")
|
||||||
|
@ -271,6 +274,7 @@ func processLoginEvent(ge *pb.Event) *pb.Event {
|
||||||
log.Println(e.Comment)
|
log.Println(e.Comment)
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func JSONtoVM(ge *pb.Event, json string) {
|
func JSONtoVM(ge *pb.Event, json string) {
|
||||||
tmp := "network_addr"
|
tmp := "network_addr"
|
||||||
|
@ -295,105 +299,13 @@ func processAddEvent(ge *pb.Event) *pb.Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
||||||
json := newFetchBody(ge, "POST", url)
|
newVM := string(POSTbody(ge, url))
|
||||||
log.Println("processAddEvent() recieved json=", json)
|
log.Println("processAddEvent() recieved newVM=", newVM)
|
||||||
|
|
||||||
/*
|
|
||||||
m, ok := gjson.Parse(tmp).Value().(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
// not a map
|
|
||||||
log.Println("processAddEvent() not a map m=", m)
|
|
||||||
}
|
|
||||||
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"
|
|
||||||
json := newFetchBody(ge, "GET", url)
|
|
||||||
log.Println("processAddEvent() recieved json=", json)
|
|
||||||
|
|
||||||
json = string(fetchGET(ge.Account.Token, "http://stackapi:4000/clusters"))
|
|
||||||
log.Println("processAddEvent() recieved json=", json)
|
|
||||||
|
|
||||||
url = ge.Account.URL + "vms/testing.com?count=1&cpu=2&ram=512&disk=25"
|
|
||||||
json = newFetchBody(ge, "POST", url)
|
|
||||||
log.Println("processAddEvent() recieved json=", json)
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
e := pb.MakeOkResponse()
|
e := pb.MakeOkResponse()
|
||||||
e.Account = ge.Account
|
e.Account = ge.Account
|
||||||
e.Comment = "processAddEvent() LOGIN WORKED"
|
e.Comment = newVM
|
||||||
log.Println(e.Comment)
|
log.Println(e.Comment)
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue