summaryrefslogtreecommitdiffstats
path: root/msb2pilot/src
diff options
context:
space:
mode:
authorLvbo163 <lv.bo163@zte.com.cn>2018-08-02 10:39:43 +0800
committerLvbo163 <lv.bo163@zte.com.cn>2018-08-02 10:39:43 +0800
commit8bbc7adaeab5df7c9e9a5e3ad2fa94f6d3a41c2a (patch)
tree9f2f13753df385262726290fe1aa06cd0c587f45 /msb2pilot/src
parentf68cc86f0733965297348ce0b7cf1f772da802f0 (diff)
data convertor of consul and msb
Issue-ID: MSB-250 Change-Id: I8c0446af0e1954b68d5af1331bd25426ec4e6ea1 Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'msb2pilot/src')
-rw-r--r--msb2pilot/src/msb2pilot/models/conversion.go117
1 files changed, 117 insertions, 0 deletions
diff --git a/msb2pilot/src/msb2pilot/models/conversion.go b/msb2pilot/src/msb2pilot/models/conversion.go
new file mode 100644
index 0000000..144d05b
--- /dev/null
+++ b/msb2pilot/src/msb2pilot/models/conversion.go
@@ -0,0 +1,117 @@
+/**
+ * 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 models
+
+import (
+ "encoding/json"
+ "msb2pilot/log"
+ "strings"
+
+ "github.com/hashicorp/consul/api"
+)
+
+func ConvertService(endpoints []*api.CatalogService) *MsbService {
+ if len(endpoints) == 0 {
+ return nil
+ }
+
+ endpoint := endpoints[0]
+ service := &MsbService{
+ ServiceName: endpoint.ServiceName,
+ ModifyIndex: endpoint.ModifyIndex,
+ ConsulLabels: &ConsulLabels{},
+ }
+
+ convertMsbLabels(service.ConsulLabels, endpoint.ServiceTags)
+ return service
+}
+
+func convertBaseInfo(baseString string) (baseInfo *BaseInfo, err error) {
+ baseInfo = new(BaseInfo)
+ err = json.Unmarshal([]byte(baseString), baseInfo)
+
+ return
+}
+
+func convertNameSpace(ns string) (nameSpace *NameSpace, err error) {
+ nameSpace = new(NameSpace)
+ err = json.Unmarshal([]byte(ns), nameSpace)
+ return
+}
+
+func LoadMsbServiceFromMap(services map[string][]string) []*MsbService {
+ result := make([]*MsbService, 0, len(services))
+
+ for k, v := range services {
+ service := &MsbService{
+ ServiceName: k,
+ ConsulLabels: new(ConsulLabels),
+ }
+ convertMsbLabels(service.ConsulLabels, v)
+ result = append(result, service)
+ }
+ return result
+}
+
+func ConsulService2MsbService(consulService *api.CatalogService) *MsbService {
+ msbService := &MsbService{
+ ServiceName: consulService.ServiceName,
+ ServiceAddress: consulService.ServiceAddress,
+ ServicePort: consulService.ServicePort,
+ ConsulLabels: new(ConsulLabels),
+ }
+
+ convertMsbLabels(msbService.ConsulLabels, consulService.ServiceTags)
+
+ return msbService
+}
+
+func convertMsbLabel(label, labelstr string) interface{} {
+ var result interface{}
+ var err error
+
+ labelPrefix := "\"" + label + "\":"
+
+ if strings.HasPrefix(labelstr, labelPrefix) {
+ kvp := strings.Split(labelstr, labelPrefix)
+ value := kvp[1]
+
+ switch label {
+ case "base":
+ result, err = convertBaseInfo(value)
+ case "ns":
+ result, err = convertNameSpace(value)
+ }
+
+ if err != nil {
+ log.Log.Error("parse msb label error", err)
+ return nil
+ }
+
+ return result
+ }
+
+ return nil
+}
+func convertMsbLabels(consulLabels *ConsulLabels, labels []string) {
+ for _, label := range labels {
+ baseInfo := convertMsbLabel("base", label)
+ if baseInfo != nil {
+ consulLabels.BaseInfo = baseInfo.(*BaseInfo)
+ }
+
+ ns := convertMsbLabel("ns", label)
+ if ns != nil {
+ consulLabels.NameSpace = ns.(*NameSpace)
+ }
+ }
+}