add a test rest api 35/59335/1
authorLvbo163 <lv.bo163@zte.com.cn>
Tue, 7 Aug 2018 07:12:22 +0000 (15:12 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Tue, 7 Aug 2018 07:12:22 +0000 (15:12 +0800)
Issue-ID: MSB-271

Change-Id: Ic3476a98d66ad285f400e7bbdc6e8021be50e980
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/controllers/routerule.go [new file with mode: 0644]
msb2pilot/src/msb2pilot/main.go
msb2pilot/src/msb2pilot/pilot/controller.go
msb2pilot/src/msb2pilot/pilot/msb.go
msb2pilot/src/msb2pilot/routers/router.go

diff --git a/msb2pilot/src/msb2pilot/controllers/routerule.go b/msb2pilot/src/msb2pilot/controllers/routerule.go
new file mode 100644 (file)
index 0000000..f32708e
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and the Apache License 2.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ *     ZTE - initial Project
+ */
+package controllers
+
+import (
+       "encoding/json"
+       "msb2pilot/log"
+       "msb2pilot/pilot"
+
+       "github.com/astaxie/beego"
+)
+
+type RouteRuleController struct {
+       beego.Controller
+}
+
+func (this *RouteRuleController) Get() {
+       log.Log.Debug("routerule controller get method called")
+
+       name := this.GetString("name")
+       namespace := this.GetString("namespace")
+       log.Log.Debug("name is : " + name)
+       log.Log.Debug("namespace is: " + namespace)
+       if name != "" { // get routerule detail
+               data, _ := pilot.Get("routerules", namespace, name)
+               b, _ := json.Marshal(data)
+               log.Log.Debug(string(b))
+
+               this.Data["json"] = pilot.ConvertConfig(*data)
+
+       } else { // get routerule name list
+               data, _ := pilot.List("routerules", "")
+               b, _ := json.Marshal(data)
+               log.Log.Debug(string(b))
+               rules := make([]interface{}, 0, len(data))
+               for _, config := range data {
+                       rules = append(rules, pilot.ConvertConfig(config))
+               }
+
+               this.Data["json"] = rules
+       }
+       this.ServeJSON()
+}
index 7ed762c..708e88c 100644 (file)
 package main
 
 import (
-       "fmt"
        "msb2pilot/consul"
        "msb2pilot/log"
        "msb2pilot/models"
+       "msb2pilot/pilot"
        _ "msb2pilot/routers"
        "time"
 
@@ -37,5 +37,7 @@ func syncConsulData() {
 }
 
 func syncMsbData(newServices []*models.MsbService) {
-       fmt.Println(len(newServices), "services updated", time.Now())
+       stop := make(chan struct{})
+       monitor := consul.NewConsulMonitor(nil, 20*time.Second, pilot.SyncMsbData)
+       monitor.Start(stop)
 }
index ddb59ec..f0cf87d 100644 (file)
@@ -161,6 +161,7 @@ func Update(config *model.Config) (string, error) {
 }
 
 func operate(operation Operation, config *model.Config) (string, error) {
+       log.Log.Informational("%s %s %s %s", operation, config.Type, config.Name, config.Namespace)
        switch operation {
        case OperationCreate:
                return client.Create(*config)
index d81b2f1..fb87d08 100644 (file)
@@ -47,6 +47,7 @@ func SyncMsbData(newServices []*models.MsbService) {
 
 func saveService(operation Operation, services []*models.MsbService) {
        if len(services) == 0 {
+               log.Log.Debug("0 services need to %s. \n", operation)
                return
        }
        configs, err := parseServiceToConfig(services)
@@ -55,20 +56,21 @@ func saveService(operation Operation, services []*models.MsbService) {
                return
        }
        fails := Save(operation, configs)
-       log.Log.Debug("%d services need to %s, %d fails. \n", len(services), operation, len(fails))
+       log.Log.Debug("%d services %d rules need to %s, %d fails. \n", len(services), len(configs), operation, len(fails))
 }
 
 func deleteAllMsbRules() {
        log.Log.Informational("delete all msb rules")
-       configs, err := List("route-rule", "")
+       configs, err := List("routerules", "")
 
        if err != nil {
+               log.Log.Error("fail to load rule list", err)
                return
        }
 
        deleteList := msbRuleFilter(configs)
-
-       Save(OperationDelete, deleteList)
+       failed := Save(OperationDelete, deleteList)
+       log.Log.Debug("deleteAllMsbRules total %d rules, fail %d", len(configs), len(failed))
 }
 
 func msbRuleFilter(configs []istioModel.Config) []istioModel.Config {
index 1a8d1e5..8768359 100644 (file)
@@ -18,5 +18,6 @@ import (
 )
 
 func init() {
+       beego.Router("/routerule", &controllers.RouteRuleController{})
        beego.Router("/", &controllers.MainController{})
 }