From 61e84ddd17bf20a7be59f7239591508b9702f4d7 Mon Sep 17 00:00:00 2001 From: goodrain Date: Fri, 16 Mar 2018 12:16:57 +0800 Subject: [PATCH] [REV] update charges verify code --- pkg/api/controller/chargesVerify.go | 44 ++++--------- pkg/api/controller/serviceAction.go | 12 +++- pkg/api/handler/publiccloud/chargesverify.go | 69 ++++++++++++++++++++ pkg/api/middleware/mideware.go | 1 + 4 files changed, 90 insertions(+), 36 deletions(-) create mode 100644 pkg/api/handler/publiccloud/chargesverify.go diff --git a/pkg/api/controller/chargesVerify.go b/pkg/api/controller/chargesVerify.go index 0e156bd79..894dcd94c 100644 --- a/pkg/api/controller/chargesVerify.go +++ b/pkg/api/controller/chargesVerify.go @@ -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) } diff --git a/pkg/api/controller/serviceAction.go b/pkg/api/controller/serviceAction.go index 62f37e464..7bac8029a 100644 --- a/pkg/api/controller/serviceAction.go +++ b/pkg/api/controller/serviceAction.go @@ -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) diff --git a/pkg/api/handler/publiccloud/chargesverify.go b/pkg/api/handler/publiccloud/chargesverify.go new file mode 100644 index 000000000..4f0567e47 --- /dev/null +++ b/pkg/api/handler/publiccloud/chargesverify.go @@ -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 . + +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")) +} diff --git a/pkg/api/middleware/mideware.go b/pkg/api/middleware/mideware.go index 9a4c21e36..f4448bcb8 100644 --- a/pkg/api/middleware/mideware.go +++ b/pkg/api/middleware/mideware.go @@ -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)