mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-11-30 02:38:17 +08:00
[REV] update charges verify code
This commit is contained in:
parent
a01c05f73f
commit
61e84ddd17
@ -19,14 +19,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/pquerna/ffjson/ffjson"
|
||||
"github.com/goodrain/rainbond/pkg/api/handler/publiccloud"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/goodrain/rainbond/pkg/db"
|
||||
"github.com/goodrain/rainbond/pkg/db/model"
|
||||
|
||||
@ -58,8 +56,8 @@ import (
|
||||
func ChargesVerifyController(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if publicCloud := os.Getenv("PUBLIC_CLOUD"); publicCloud != "true" {
|
||||
|
||||
httputil.ReturnSuccess(r, w, nil)
|
||||
return
|
||||
}
|
||||
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*model.Tenants)
|
||||
if tenant.EID == "" {
|
||||
@ -80,35 +78,15 @@ func ChargesVerifyController(w http.ResponseWriter, r *http.Request) {
|
||||
httputil.ReturnError(r, w, 400, "quantity can not found")
|
||||
return
|
||||
}
|
||||
quantityInt, err := strconv.Atoi(quantity)
|
||||
if err != nil {
|
||||
httputil.ReturnError(r, w, 400, "quantity type must be int")
|
||||
return
|
||||
}
|
||||
reason := r.FormValue("reason")
|
||||
api := fmt.Sprintf("%s/openapi/v1/enterprises/%s/memory-apply?quantity=%s&tid=%s&reason=%s", cloudAPI, tenant.EID, quantity, tenant.UUID, reason)
|
||||
req, err := http.NewRequest("GET", api, nil)
|
||||
if err != nil {
|
||||
logrus.Error("create request cloud api error", err.Error())
|
||||
httputil.ReturnError(r, w, 400, "create request cloud api error")
|
||||
if err := publiccloud.ChargeSverify(tenant, quantityInt, reason); err != nil {
|
||||
err.Handle(r, w)
|
||||
return
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logrus.Error("create request cloud api error", err.Error())
|
||||
httputil.ReturnError(r, w, 400, "create request cloud api error")
|
||||
return
|
||||
}
|
||||
if res.StatusCode == 200 {
|
||||
httputil.ReturnSuccess(r, w, nil)
|
||||
return
|
||||
}
|
||||
if res.Body != nil {
|
||||
defer res.Body.Close()
|
||||
rebody, _ := ioutil.ReadAll(res.Body)
|
||||
var re = make(map[string]interface{})
|
||||
if err := ffjson.Unmarshal(rebody, &re); err == nil {
|
||||
if msg, ok := re["msg"]; ok {
|
||||
httputil.ReturnError(r, w, res.StatusCode, msg.(string))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
httputil.ReturnError(r, w, res.StatusCode, "none")
|
||||
return
|
||||
httputil.ReturnSuccess(r, w, nil)
|
||||
}
|
||||
|
@ -213,8 +213,6 @@ func handleStatus(status int, err error, w http.ResponseWriter, r *http.Request)
|
||||
// "$ref": "#/responses/commandResponse"
|
||||
// description: 统一返回格式
|
||||
func (t *TenantStruct) StartService(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
logrus.Debugf("trans start service")
|
||||
rules := validator.MapData{
|
||||
"event_id": []string{},
|
||||
}
|
||||
@ -222,7 +220,15 @@ func (t *TenantStruct) StartService(w http.ResponseWriter, r *http.Request) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// if os.Getenv("PUBLIC_CLOUD") == "true" {
|
||||
// tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
|
||||
// service := r.Context().Value(middleware.ContextKey("service")).(*dbmodel.TenantServices)
|
||||
// if err := publiccloud.ChargeSverify(tenant, service.ContainerMemory*service.Replicas, "start"); err != nil {
|
||||
// err.Handle(r, w)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
|
||||
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
|
||||
|
69
pkg/api/handler/publiccloud/chargesverify.go
Normal file
69
pkg/api/handler/publiccloud/chargesverify.go
Normal file
@ -0,0 +1,69 @@
|
||||
// RAINBOND, Application Management Platform
|
||||
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version. For any non-GPL usage of Rainbond,
|
||||
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
|
||||
// must be obtained first.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package publiccloud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/pquerna/ffjson/ffjson"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/goodrain/rainbond/pkg/api/util"
|
||||
"github.com/goodrain/rainbond/pkg/db/model"
|
||||
)
|
||||
|
||||
//ChargeSverify service Charge Sverify
|
||||
func ChargeSverify(tenant *model.Tenants, quantity int, reason string) *util.APIHandleError {
|
||||
cloudAPI := os.Getenv("CLOUD_API")
|
||||
if cloudAPI == "" {
|
||||
cloudAPI = "http://api.goodrain.com"
|
||||
}
|
||||
regionName := os.Getenv("REGION_NAME")
|
||||
if regionName == "" {
|
||||
return util.CreateAPIHandleError(500, fmt.Errorf("region name must define in api by env REGION_NAME"))
|
||||
}
|
||||
api := fmt.Sprintf("%s/openapi/v1/enterprises/%s/memory-apply?quantity=%d&tid=%s&reason=%s®ion=%s", cloudAPI, tenant.EID, quantity, tenant.UUID, reason, regionName)
|
||||
req, err := http.NewRequest("GET", api, nil)
|
||||
if err != nil {
|
||||
logrus.Error("create request cloud api error", err.Error())
|
||||
return util.CreateAPIHandleError(400, fmt.Errorf("create request cloud api error"))
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logrus.Error("create request cloud api error", err.Error())
|
||||
return util.CreateAPIHandleError(400, fmt.Errorf("create request cloud api error"))
|
||||
}
|
||||
if res.StatusCode == 200 {
|
||||
return nil
|
||||
}
|
||||
if res.Body != nil {
|
||||
defer res.Body.Close()
|
||||
rebody, _ := ioutil.ReadAll(res.Body)
|
||||
var re = make(map[string]interface{})
|
||||
if err := ffjson.Unmarshal(rebody, &re); err == nil {
|
||||
if msg, ok := re["msg"]; ok {
|
||||
return util.CreateAPIHandleError(res.StatusCode, fmt.Errorf("%s", msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
return util.CreateAPIHandleError(res.StatusCode, fmt.Errorf("none"))
|
||||
}
|
@ -87,6 +87,7 @@ func InitService(next http.Handler) http.Handler {
|
||||
serviceID := service.ServiceID
|
||||
ctx := context.WithValue(r.Context(), ContextKey("service_alias"), serviceAlias)
|
||||
ctx = context.WithValue(ctx, ContextKey("service_id"), serviceID)
|
||||
ctx = context.WithValue(ctx, ContextKey("service"), service)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
|
Loading…
Reference in New Issue
Block a user