get published services from msb 93/59093/1
authorLvbo163 <lv.bo163@zte.com.cn>
Mon, 6 Aug 2018 07:21:24 +0000 (15:21 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Mon, 6 Aug 2018 07:21:24 +0000 (15:21 +0800)
Issue-ID: MSB-263

Change-Id: I3e155fec6e1e68190ad95da064d4d46b921a70b1
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/models/config.go
msb2pilot/src/msb2pilot/msb/msb.go [new file with mode: 0644]

index 8dd2d3c..5773cf4 100644 (file)
@@ -12,6 +12,7 @@
 package models
 
 const (
-       EnvConsulAddress = "ConsulAddress" //http://localhost:8500
-       EnvK8sAddress    = "K8sAddress"
+       EnvConsulAddress  = "ConsulAddress" //http://localhost:8500
+       EnvK8sAddress     = "K8sAddress"
+       EnvMsbAddress     = "MsbAddress"
 )
diff --git a/msb2pilot/src/msb2pilot/msb/msb.go b/msb2pilot/src/msb2pilot/msb/msb.go
new file mode 100644 (file)
index 0000000..9d5754a
--- /dev/null
@@ -0,0 +1,59 @@
+/**
+ * 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 msb
+
+import (
+       "encoding/json"
+       "io/ioutil"
+       "msb2pilot/log"
+       "msb2pilot/models"
+       "net/http"
+       "os"
+)
+
+var (
+       msbAddr = "http://localhost:9081"
+)
+
+func getBaseUrl() string {
+       baseUrl := os.Getenv(models.EnvMsbAddress)
+       if baseUrl == "" {
+               baseUrl = msbAddr
+       }
+
+       return baseUrl
+}
+
+func GetAllPublishServices() []*models.PublishService {
+       url := getBaseUrl() + "/api/msdiscover/v1/publishservicelist"
+       res, err := http.Get(url)
+
+       if err != nil {
+               log.Log.Error("fail to get public address", url, err)
+               return nil
+       }
+
+       b, err := ioutil.ReadAll(res.Body)
+       if err != nil {
+               log.Log.Error("fail to read response", err)
+               return nil
+       }
+
+       result := make([]*models.PublishService, 0)
+       err = json.Unmarshal(b, &result)
+       if err != nil {
+               log.Log.Error("fail to unmarshal publish address", err)
+               return nil
+       }
+
+       return result
+}