Browse Source

1: resetPassword method
2: price calculator (preview)
3: fix uuidGen bug

torabkheslat 4 years ago
parent
commit
f7a99d431d
3 changed files with 39 additions and 17 deletions
  1. 33 3
      handler.go
  2. 2 1
      main.go
  3. 4 13
      ovirt.go

+ 33 - 3
handler.go

@@ -110,7 +110,6 @@ type user_findResult struct {
 	} `json:"result"`
 	Version string `json:"version"`
 }
-
 var User = userInfo{}
 
 func (h *handler) login(c echo.Context) error {
@@ -695,7 +694,6 @@ func (h *handler) verifyUser(c echo.Context) error {
 
 	return c.JSON(http.StatusNotFound, resp)
 }
-
 func (h *handler) forgetpassword(c echo.Context) error {
 
 	user := c.Get("user").(*jwt.Token)
@@ -829,7 +827,6 @@ func (h *handler) forgetpassword(c echo.Context) error {
 	}
 	return c.JSON(http.StatusNotFound, resp)
 }
-
 func setResetKey(key string, username string) string {
 	short := uniuri.NewLen(32)
 	db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
@@ -955,3 +952,36 @@ func (h *handler) ListServices(c echo.Context) error {
 	}
 	return c.JSON(http.StatusOK, resp)
 }
+func (h *handler) PriceCalc(c echo.Context) error {
+	user := c.Get("user").(*jwt.Token)
+	db, _:= sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
+	_, _ = db.Query("SELECT uuid as UUID ,task_apiCall as TaskAPICall , cron_expression as CronExpression , related_uuid as Ruuid, type FROM scheduler where active=1")
+	_, _ = db.Query("SELECT uuid as UUID ,task_apiCall as TaskAPICall , cron_expression as CronExpression , related_uuid as Ruuid, type FROM scheduler where active=1")
+	_, _ = db.Query("SELECT uuid as UUID ,task_apiCall as TaskAPICall , cron_expression as CronExpression , related_uuid as Ruuid, type FROM scheduler where active=1")
+	db.Close()
+	claims := user.Claims.(jwt.MapClaims)
+	_sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
+	var hashChannel_ = make(chan []byte, 1)
+	hashChannel_ <- _sha256[:]
+	//token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
+	cpu, _ := strconv.Atoi(c.FormValue("cpu"))
+	mem, _ := strconv.Atoi(c.FormValue("mem"))
+	hdd, _ := strconv.Atoi(c.FormValue("hdd"))
+	nic, _ := strconv.Atoi(c.FormValue("nic"))
+	period, _ := strconv.Atoi(c.FormValue("period"))
+	type PriceCalculator struct {
+		Data struct {
+			TotalPrice string `json:"TotalPrice"`
+		} `json:"message"`
+		Origin string `json:"origin"`
+		Code   int    `json:"code"`
+	}
+	resp := PriceCalculator{
+		Data: struct {
+			TotalPrice string `json:"TotalPrice"`
+		}{TotalPrice: strconv.Itoa((cpu*500 + mem + 10 + hdd*19 + nic*10000)*period)},
+		Origin: "PriceCalc",
+		Code:   1000,
+	}
+	return c.JSON(http.StatusOK, resp)
+}

+ 2 - 1
main.go

@@ -133,6 +133,7 @@ func main() {
 	echoHandler.POST("/forgetpassword", h.forgetpassword, isLoggedIn, isAdmin)
 	echoHandler.POST("/token", h.token, isLoggedIn)
 	echoHandler.POST("/ListServices", h.ListServices, isLoggedIn)
+	echoHandler.POST("/PriceCalc", h.PriceCalc, isLoggedIn)
 
 	iaas := &ovirt{}
 	echoHandler.GET("/ovirtListVMs", iaas.listVM, isLoggedIn)
@@ -322,4 +323,4 @@ func VMPowerMng(relatedUuid string, apiCall string, uuid string) {
 	runAPICall(startVM)
 	toggleTask(uuid, 0)
 
-}
+}

+ 4 - 13
ovirt.go

@@ -507,7 +507,7 @@ func (o ovirt) addvm(c echo.Context) error {
 	})
 	uuid, _ := uuidgen("APIGW-Ovirt-addVMTask")
 	invoiceUUID, _ := uuidgen("APIGW-FakeInvoice")
-	responseInvoiece:=AddVMPayment{
+	responseInvoiece := AddVMPayment{
 		Data: struct {
 			Price struct {
 				CPU    string `json:"cpu"`
@@ -668,7 +668,6 @@ func (o ovirt) listVM(c echo.Context) error {
 	return c.JSON(http.StatusOK, resp)
 	//return c.String(http.StatusOK, string(b))
 }
-
 func (o ovirt) StartVM(c echo.Context) error {
 	vmuuid := c.FormValue("VmUUID")
 	user := c.Get("user").(*jwt.Token)
@@ -718,7 +717,6 @@ func (o ovirt) StartVM(c echo.Context) error {
 	return c.JSON(http.StatusOK, resp)
 	//return c.String(http.StatusOK, string(b))
 }
-
 func (o ovirt) StopVM(c echo.Context) error {
 	vmuuid := c.FormValue("VmUUID")
 	user := c.Get("user").(*jwt.Token)
@@ -768,7 +766,6 @@ func (o ovirt) StopVM(c echo.Context) error {
 	return c.JSON(http.StatusOK, resp)
 	//return c.String(http.StatusOK, string(b))
 }
-
 func (o ovirt) RebootVM(c echo.Context) error {
 	vmuuid := c.FormValue("VmUUID")
 	user := c.Get("user").(*jwt.Token)
@@ -867,7 +864,6 @@ func (o ovirt) PowerOffVM(c echo.Context) error {
 	//return c.String(http.StatusOK, string(b))
 	return c.JSON(http.StatusOK, resp)
 }
-
 func (o ovirt) ResetVM(c echo.Context) error {
 	resp := _response{
 		Origin:  "ovirt-ResetVM",
@@ -879,7 +875,6 @@ func (o ovirt) ResetVM(c echo.Context) error {
 	//return c.String(http.StatusOK, string(b))
 
 }
-
 func (o ovirt) AddNIC(c echo.Context) error {
 	resp := _response{
 		Origin:  "ovirt-AddNIC",
@@ -890,7 +885,6 @@ func (o ovirt) AddNIC(c echo.Context) error {
 	//return c.String(http.StatusOK, string(b))
 	return c.JSON(http.StatusOK, resp)
 }
-
 func (o ovirt) AddDisk(c echo.Context) error {
 	resp := _response{
 		Origin:  "ovirt-AddDisk",
@@ -901,7 +895,6 @@ func (o ovirt) AddDisk(c echo.Context) error {
 	//return c.String(http.StatusOK, string(b))
 	return c.JSON(http.StatusOK, resp)
 }
-
 func (o ovirt) EditVM(c echo.Context) error {
 	resp := _response{
 		Origin:  "ovirt-EditVM",
@@ -912,7 +905,6 @@ func (o ovirt) EditVM(c echo.Context) error {
 	//return c.String(http.StatusOK, string(b))
 	return c.JSON(http.StatusOK, resp)
 }
-
 func (o ovirt) ResetPassword(c echo.Context) error {
 	resp := _response{
 		Origin:  "ovirt-ResetPassword",
@@ -923,7 +915,6 @@ func (o ovirt) ResetPassword(c echo.Context) error {
 	//return c.String(http.StatusOK, string(b))
 	return c.JSON(http.StatusOK, resp)
 }
-
 func createVM(invoice_uuid string, apiJson string, uuid string) {
 	type AddVMResponse struct {
 		NextRunConfigurationExists string `json:"next_run_configuration_exists"`
@@ -1136,7 +1127,6 @@ func createVM(invoice_uuid string, apiJson string, uuid string) {
 	}
 
 }
-
 func VMInitialization(relatedUuid string, apiCall string, uuid string) {
 	iaas := &ovirt{}
 	status := iaas.vmStatus(relatedUuid)
@@ -1165,7 +1155,6 @@ func VMInitialization(relatedUuid string, apiCall string, uuid string) {
 		toggleTask(uuid, 0)
 	}
 }
-
 func vmStatistics(BA string, VMUUID string) (string, float64, float64) {
 	//ram,cpu,storage
 	var _disk []string
@@ -1648,9 +1637,11 @@ func (o ovirt) vmDetails(c echo.Context) error {
 func (o ovirt) ovirtPayment(c echo.Context) error {
 	invoiceUuid := c.FormValue("invoiceUuid")
 	db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
+	//results, err := db.Query("SELECT uuid as UUID ,task_apiCall as TaskAPICall , cron_expression as CronExpression , related_uuid as Ruuid, type FROM scheduler where active=1")
+	///TODO: check duplicate payment confirmation by Invoice table
 	if err != nil {
 	}
-	update, err := db.Query("update scheduler set active=' 1' where related_uuid='" + invoiceUuid + "' and type=0")
+	update, err := db.Query("update scheduler set active='1' where related_uuid='" + invoiceUuid + "' and type=0")
 	defer db.Close()
 	if err != nil {
 	}