center-config/transform/apollo_migrate.go

117 lines
3.2 KiB
Go
Raw Normal View History

2021-11-30 17:50:21 +08:00
// Package transform ...
//
// Description : transform ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-30 4:04 下午
package transform
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/ddliu/go-httpclient"
)
// NewApolloMigrate 从apollo迁移数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:06 下午 2021/11/30
func NewApolloMigrate(getAllApolloConfigURL string, appID string, env string, namespace string, cookie map[string]string, header map[string]string) ITransform {
if len(getAllApolloConfigURL) == 0 {
getAllApolloConfigURL = "http://test-apollo.portal.life.ke.com/apps/{APP_ID}/envs/{ENV}/clusters/default/namespaces/{NAMESPACE}/releases/active?page=0&size=1"
replaceMap := map[string]string{
"{APP_ID}": appID,
"{ENV}": env,
"{NAMESPACE}": namespace,
}
for k, v := range replaceMap {
getAllApolloConfigURL = strings.ReplaceAll(getAllApolloConfigURL, k, v)
}
}
if cookie == nil {
cookie = make(map[string]string)
header = make(map[string]string)
}
return &ApolloMigrate{
getDataURL: getAllApolloConfigURL,
cookie: cookie,
header: header,
appID: appID,
}
}
// ApolloMigrate 从apollo迁移数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:05 下午 2021/11/30
type ApolloMigrate struct {
getDataURL string
cookie map[string]string
header map[string]string
appID string
}
// GetNamespace 迁移之后,存储到哪一个命名空间
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:17 下午 2021/11/30
func (a *ApolloMigrate) GetNamespace() string {
return a.appID
}
// GetSourceData ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:18 下午 2021/11/30
func (a *ApolloMigrate) GetSourceData() (map[string]string, error) {
var (
err error
resp *httpclient.Response
responseData []byte
)
hc := httpclient.NewHttpClient()
cookieList := make([]*http.Cookie, 0)
for name, val := range a.cookie {
cookieList = append(cookieList, &http.Cookie{Name: name, Value: val})
}
resp, err = hc.WithHeaders(a.header).WithCookie(cookieList...).Get(a.getDataURL)
if nil != err {
return make(map[string]string), errors.New("apollo接口请求失败 : " + err.Error())
}
responseData, err = ioutil.ReadAll(resp.Body)
if nil != err {
return make(map[string]string), errors.New("apollo 响应数据读取失败 : " + err.Error())
}
var cfgList []ApolloData
if err = json.Unmarshal(responseData, &cfgList); nil != err {
return make(map[string]string), errors.New("apollo 数据解析失败 : " + err.Error())
}
if len(cfgList) == 0 {
return make(map[string]string), errors.New("当前apollo配置读取到的数据为空")
}
var realCfg map[string]string
if err = json.Unmarshal([]byte(cfgList[0].Configurations), &realCfg); nil != err {
return make(map[string]string), errors.New("真实配置文件解析失败 ")
}
return realCfg, nil
}
// MigrateKeyFailCallback ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:58 下午 2021/11/30
func (a *ApolloMigrate) MigrateKeyFailCallback(namespace string, key string, val string, err error) {
fmt.Println(namespace, key, val, err)
}