From ce890da0e79c26ad6f31f86956f690efee0106da Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Mon, 6 Aug 2018 15:21:24 +0800 Subject: get published services from msb Issue-ID: MSB-263 Change-Id: I3e155fec6e1e68190ad95da064d4d46b921a70b1 Signed-off-by: Lvbo163 --- msb2pilot/src/msb2pilot/models/config.go | 5 +-- msb2pilot/src/msb2pilot/msb/msb.go | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 msb2pilot/src/msb2pilot/msb/msb.go diff --git a/msb2pilot/src/msb2pilot/models/config.go b/msb2pilot/src/msb2pilot/models/config.go index 8dd2d3c..5773cf4 100644 --- a/msb2pilot/src/msb2pilot/models/config.go +++ b/msb2pilot/src/msb2pilot/models/config.go @@ -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 index 0000000..9d5754a --- /dev/null +++ b/msb2pilot/src/msb2pilot/msb/msb.go @@ -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 +} -- cgit 1.2.3-korg