cleaning up the code further

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-26 07:27:51 +00:00
parent deac243443
commit b16b4c84bb
2 changed files with 155 additions and 131 deletions

View File

@ -3,10 +3,11 @@ package main
import "net/http"
import "io/ioutil"
import "log"
import "fmt"
import "bytes"
// import "github.com/davecgh/go-spew/spew"
// const API = "http://stackapi:4000/"
import "github.com/tidwall/gjson"
import pb "git.wit.com/wit/witProtobuf"
func fetchGET(tok string, a string) []byte {
return fetchJSON("GET", tok, a)
@ -41,14 +42,33 @@ func fetchJSON(GETorPOST string, token string, a string) []byte {
return body
}
/*
func main() {
var raw []byte
func getToken(ge *pb.Event) string {
if ge == nil {return ""}
var login string
login = "auth/login?email=jcarr@wit.com&password=yowzayowza"
raw = fetchPOST("badtoken", login)
log.Println("raw =", string(raw))
spew.Dump(raw)
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Email + "&password=" + ge.Password
log.Println("URL =", url)
var jsonStr = []byte(`{}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Wit-Auth", "tokenrequest")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return ""
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
json := string(body)
jwt := gjson.Get(json, "jwt")
// log.Println("TESTST SATDEA jwt =", jwt)
return jwt.String()
}
*/

View File

@ -1,13 +1,11 @@
package main
import "flag"
import "fmt"
import "io"
import "log"
import "net/http"
import "encoding/json"
import "io/ioutil"
import "bytes"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
@ -25,93 +23,20 @@ type network struct {
Subdomain string `json:"subdomain"`
}
func getToken(user string, pass string, APIurl string) string {
url := APIurl + "?email=" + user + "&password=" + pass
log.Println("URL =", url)
var jsonStr = []byte(`{}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Wit-Auth", "alksdjfasdf")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
json := string(body)
jwt := gjson.Get(json, "jwt")
// log.Println("TESTST SATDEA jwt =", jwt)
return jwt.String()
}
func checkLogin(ge *pb.Event) string {
token := getToken("jcarr@wit.com", "yowzayowza", "http://stackapi:4000/auth/login")
if (origLoginCheck(ge) == true) {
log.Println("origLoginCheck() LOGIN WORKED")
return ge.Token
}
log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)")
log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)")
log.Println("origLoginCheck() LOGIN FAILED. ATTEMPT getToken(ge)")
ge.Account.URL = "http://stackapi:4000"
token := getToken(ge)
return token
}
func jsonAddEvent(ge *pb.Event) bool {
var url string
var httpType string
httpType = "GET"
if ge.Type == pb.Event_LOGIN {
url = API + "me"
} else if ge.Type == pb.Event_ADD {
// stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25
url = API + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
httpType = "POST"
} else if ge.Type == pb.Event_MIGRATE {
url = API + "clusters"
} else {
// if unknown, just check login
url = API + "me"
}
log.Println("jsonAddEvent() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}
func fetchNetworks(token string) []network {
var networks []network
req, err := http.NewRequest("GET", API + "clusters", nil)
@ -346,50 +271,129 @@ func processLoginEvent(ge *pb.Event) *pb.Event {
log.Println("processLoginEvent() NEED TO LOGIN HERE OR CHECK TOKEN WORKS")
token := checkLogin(ge)
if token != "" {
log.Println("processLoginEvent() LOGIN WORKED")
log.Println("processLoginEvent() LOGIN WORKED")
log.Println("processLoginEvent() LOGIN WORKED")
e := pb.MakeOkResponse()
e.Token = token
e.Comment = "LOGIN WORKED"
return e
} else {
log.Println("processLoginEvent() LOGIN FAILED")
log.Println("processLoginEvent() LOGIN FAILED")
log.Println("processLoginEvent() LOGIN FAILED")
if (token == "") {
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
return e
}
log.Println("processLoginEvent() END")
e := &pb.Event{}
log.Println("processLoginEvent() LOGIN WORKED")
log.Println("processLoginEvent() LOGIN WORKED")
log.Println("processLoginEvent() LOGIN WORKED")
e := pb.MakeOkResponse()
e.Token = token
e.Comment = "LOGIN WORKED"
return e
}
func processAddEvent(ge *pb.Event) *pb.Event {
log.Println("processAddEvent() START")
log.Println("processAddLoginEvent() ge.Token =", ge.Token)
log.Println("processAddEvent() NEED TO LOGIN HERE OR CHECK TOKEN WORKS")
token := checkLogin(ge)
if token != "" {
log.Println("processAddEvent() LOGIN WORKED")
log.Println("processAddEvent() LOGIN WORKED")
log.Println("processAddEvent() LOGIN WORKED")
} else {
log.Println("processAddEvent() LOGIN FAILED")
log.Println("processAddEvent() LOGIN FAILED")
log.Println("processAddEvent() LOGIN FAILED")
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
return e
}
jsonAddEvent(ge)
log.Println("processAddEvent() END")
return nil
}
/*
var login string
login = "auth/login?email=jcarr@wit.com&password=yowzayowza"
raw = fetchPOST("badtoken", login)
log.Println("raw =", string(raw))
*/
func jsonAddEvent(ge *pb.Event) bool {
var url string
var httpType string
httpType = "GET"
if ge.Type == pb.Event_LOGIN {
url = API + "me"
} else if ge.Type == pb.Event_ADD {
// stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25
url = API + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
httpType = "POST"
} else if ge.Type == pb.Event_MIGRATE {
url = API + "clusters"
} else {
// if unknown, just check login
url = API + "me"
}
log.Println("jsonAddEvent() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}
// 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 = API + "me"
log.Println("origLoginCheck() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}