Explorar o código

1: change uuidgen to Post (form-data-urlEncoded)

torabkheslat %!s(int64=4) %!d(string=hai) anos
pai
achega
1233c79b07
Modificáronse 2 ficheiros con 64 adicións e 16 borrados
  1. 2 2
      main.go
  2. 62 14
      ovirt.go

+ 2 - 2
main.go

@@ -122,7 +122,7 @@ func main() {
 
 	h := &handler{}
 	echoHandler.POST("/login", h.login)
-	echoHandler.GET("/uuidgen", h.uuidgen)
+	echoHandler.POST("/uuidgen", h.uuidgen)
 
 	//echoHandler.GET("/admin", h.uuidgen, isLoggedIn, isAdmin)
 	echoHandler.POST("/addUser", h.addUser, isLoggedIn, isAdmin)
@@ -147,7 +147,7 @@ func main() {
 	echoHandler.POST("/ovirtEditVM", iaas.EditVM, isLoggedIn)
 	echoHandler.POST("/ovirtResetPassword", iaas.ResetPassword, isLoggedIn)
 	echoHandler.POST("/vmDetails", iaas.vmDetails, isLoggedIn)
-	echoHandler.POST("/ovirtPayment", iaas.ovirtPayment, isLoggedIn, isAdmin)
+	echoHandler.POST("/ovirtPayment", iaas.ovirtPayment, isLoggedIn )
 
 	echoHandler.Logger.Fatal(echoHandler.Start(os.Args[1] + ":" + os.Args[2]))
 }

+ 62 - 14
ovirt.go

@@ -315,6 +315,21 @@ func (o ovirt) vmStatus(uuid string) string {
 	return _vmstatus.Status
 }
 func (o ovirt) addvm(c echo.Context) error {
+	type AddVMPayment struct {
+		Data struct {
+			Price struct {
+				CPU    string `json:"cpu"`
+				Mem    string `json:"mem"`
+				Disk   string `json:"disk"`
+				Nic    string `json:"nic"`
+				Period string `json:"period"`
+			} `json:"price"`
+			InvoiceUUID string `json:"invoiceUUID"`
+		} `json:"data"`
+		Message string `json:"message"`
+		Origin  string `json:"origin"`
+		Code    int    `json:"code"`
+	}
 	user := c.Get("user").(*jwt.Token)
 	claims := user.Claims.(jwt.MapClaims)
 	_sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
@@ -323,12 +338,15 @@ func (o ovirt) addvm(c echo.Context) error {
 	token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
 	_BA := strings.Split(token, ";")
 	BA := _BA[len(_BA)-2]
-	var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem string
+	var vmname, vmdescr, vmcomment, templatename, cpuSock, cpuCore, cpuThread, mem, Disk, nic, period string
 	vmname = c.FormValue("VmName")
 	vmdescr = c.FormValue("VmDescr")
 	vmcomment = c.FormValue("VmComment")
 	templatename = c.FormValue("VmTempl")
 	cpuThread = c.FormValue("VmCPU")
+	Disk = c.FormValue("VmDisk")
+	period = c.FormValue("VmPeriod")
+	nic = c.FormValue("VmNIC")
 	sshKey := c.FormValue("sshKey")
 	rootpass := c.FormValue("rootpass")
 	cpuCore = "1"
@@ -489,6 +507,36 @@ func (o ovirt) addvm(c echo.Context) error {
 	})
 	uuid, _ := uuidgen("APIGW-Ovirt-addVMTask")
 	invoiceUUID, _ := uuidgen("APIGW-FakeInvoice")
+	responseInvoiece:=AddVMPayment{
+		Data: struct {
+			Price struct {
+				CPU    string `json:"cpu"`
+				Mem    string `json:"mem"`
+				Disk   string `json:"disk"`
+				Nic    string `json:"nic"`
+				Period string `json:"period"`
+			} `json:"price"`
+			InvoiceUUID string `json:"invoiceUUID"`
+		}{
+			Price: struct {
+				CPU    string `json:"cpu"`
+				Mem    string `json:"mem"`
+				Disk   string `json:"disk"`
+				Nic    string `json:"nic"`
+				Period string `json:"period"`
+			}{
+				CPU:    cpuThread,
+				Mem:    mem,
+				Disk:   Disk,
+				Nic:    nic,
+				Period: period,
+			},
+			InvoiceUUID: invoiceUUID,
+		},
+		Message: "Done",
+		Origin:  "AddVM",
+		Code:    1000,
+	}
 	__createVM, _ := json.MarshalIndent(newvm, "", "  ")
 	// Add VM Task
 	addTask(uuid, string(__createVM), "", "APIGW", "VM Creation for "+claims["name"].(string), invoiceUUID, "0", "0")
@@ -579,12 +627,12 @@ func (o ovirt) addvm(c echo.Context) error {
 		//fmt.Println("StartVMHeaders: ", string(__startVMHeaders))
 		addTask(uuid, string(__startVM), "", "APIGW", "VM Initialization", invoiceUUID, "1", "0")
 	}
-	resp := _response{
-		Origin:  "ovirt-addvm",
-		Message: invoiceUUID,
-		Code:    1000,
-	}
-	return c.JSON(http.StatusOK, resp)
+	//resp := _response{
+	//	Origin:  "ovirt-addvm",
+	//	Message: resposeInvoice
+	//	Code:    1000,
+	//}
+	return c.JSON(http.StatusOK, responseInvoiece)
 }
 func (o ovirt) listVM(c echo.Context) error {
 	user := c.Get("user").(*jwt.Token)
@@ -712,7 +760,7 @@ func (o ovirt) StopVM(c echo.Context) error {
 	addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
 
 	resp := _response{
-		Origin:  "ovirt-StartVM",
+		Origin:  "ovirt-StopVM",
 		Message: "Done",
 		Code:    1000,
 	}
@@ -762,7 +810,7 @@ func (o ovirt) RebootVM(c echo.Context) error {
 	addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
 
 	resp := _response{
-		Origin:  "ovirt-StartVM",
+		Origin:  "ovirt-RebootVM",
 		Message: "Done",
 		Code:    1000,
 	}
@@ -811,7 +859,7 @@ func (o ovirt) PowerOffVM(c echo.Context) error {
 	addTask(uuid, string(__startVM), "", "APIGW", "VM Start", vmuuid, "1", "1")
 
 	resp := _response{
-		Origin:  "ovirt-StartVM",
+		Origin:  "ovirt-PowerOffVM",
 		Message: "Done",
 		Code:    1000,
 	}
@@ -1033,7 +1081,7 @@ func createVM(invoice_uuid string, apiJson string, uuid string) {
 		method := _createVM.Method
 		__json := _createVM.JSON
 		client := &http.Client{}
-		_json, _ :=json.Marshal(__json)
+		_json, _ := json.Marshal(__json)
 		payload := strings.NewReader(string(_json))
 		//payload := __json
 		req, err := http.NewRequest(method, url, payload)
@@ -1089,7 +1137,7 @@ func createVM(invoice_uuid string, apiJson string, uuid string) {
 
 }
 
-func VMInitialization (relatedUuid string, apiCall string, uuid string) {
+func VMInitialization(relatedUuid string, apiCall string, uuid string) {
 	iaas := &ovirt{}
 	status := iaas.vmStatus(relatedUuid)
 	//fmt.Println("VM :", relatedUuid, " is now: ", status)
@@ -1598,7 +1646,7 @@ func (o ovirt) vmDetails(c echo.Context) error {
 	//return _vmstatus.Status, _vmstatus.Name, disk, cpu, ram
 }
 func (o ovirt) ovirtPayment(c echo.Context) error {
-	invoiceUuid :=c.FormValue("invoiceUuid")
+	invoiceUuid := c.FormValue("invoiceUuid")
 	db, err := sql.Open("mysql", MySQLUSER+":"+MySQLPASS+"@tcp(127.0.0.1:3306)/zicloud")
 	if err != nil {
 	}
@@ -1615,4 +1663,4 @@ func (o ovirt) ovirtPayment(c echo.Context) error {
 	//b, _ := json.MarshalIndent(resp, "", "  ")
 	//return c.String(http.StatusOK, string(b))
 	return c.JSON(http.StatusOK, resp)
-}
+}