This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
apollo_client.go
202 lines (178 loc) · 6.84 KB
/
apollo_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package apollo
import (
"errors"
"io/ioutil"
"net/http"
"strings"
"github.com/go-chassis/foundation/httpclient"
"github.com/go-chassis/go-chassis-config"
"github.com/go-chassis/go-chassis-config/serializers"
"github.com/go-mesh/openlogging"
)
// Client contains the implementation of Client
type Client struct {
name string
client *httpclient.URLClient
serviceName string
cluster string
namespace string
URI string
}
const (
apolloServerAPI = ":ServerURL/configs/:appID/:clusterName/:nameSpace"
defaultContentType = "application/json"
//Name of the Plugin
Name = "apollo"
)
// NewApolloClient init's the necessary objects needed for seamless communication to apollo Server
func (apolloClient *Client) NewApolloClient() {
options := &httpclient.URLClientOption{
SSLEnabled: false,
TLSConfig: nil, //TODO Analyse the TLS configuration of Apollo Server
Compressed: false,
Verbose: false,
}
var err error
apolloClient.client, err = httpclient.GetURLClient(options)
if err != nil {
openlogging.GetLogger().Error("Client Initialization Failed: " + err.Error())
}
openlogging.GetLogger().Debugf("Client Initialized successfully")
}
// HTTPDo Use http-client package for rest communication
func (apolloClient *Client) HTTPDo(method string, rawURL string, headers http.Header, body []byte) (resp *http.Response, err error) {
return apolloClient.client.HTTPDo(method, rawURL, headers, body)
}
// PullConfigs is the implementation of Client and pulls all the configuration for a given serviceName
func (apolloClient *Client) PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error) {
/*
1. Compose the URL
2. Make a Http Request to Apollo Server
3. Unmarshal the response
4. Return back the configuration/error
Note: Currently the input to this function in not used, need to check it's feasibility of using it, as the serviceName/version can be different in Apollo
*/
// Compose the URL
pullConfigurationURL := apolloClient.composeURL()
// Make a Http Request to Apollo Server
resp, err := apolloClient.HTTPDo("GET", pullConfigurationURL, nil, nil)
if err != nil {
openlogging.GetLogger().Error("Error in Querying the Response from Apollo: " + err.Error())
return nil, err
}
if resp.StatusCode != 200 {
openlogging.GetLogger().Error("Bad Response : " + "Response from Apollo Server " + resp.Status)
return nil, errors.New("Bad Response from Apollo Server " + resp.Status)
}
/*
Sample Response from Apollo Server
{
"appId": "SampleApp",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"timeout": "500"
},
"releaseKey": "20180327130726-1dc5027439679153"
}
*/
//Unmarshal the response
var body []byte
body, err = ioutil.ReadAll(resp.Body)
var configurations map[string]interface{}
error := serializers.Decode(defaultContentType, body, &configurations)
if error != nil {
openlogging.GetLogger().Error("Error in Unmarshalling the Response from Apollo: " + error.Error())
return nil, error
}
openlogging.GetLogger().Debugf("The Marshaled response of the body is : ", configurations["configurations"])
var configValues map[string]interface{}
configValues = configurations["configurations"].(map[string]interface{})
return configValues, nil
}
// PullConfig is the implementation of the Client
func (apolloClient *Client) PullConfig(serviceName, version, app, env, key, contentType string) (interface{}, error) {
/*
1. Compose the URL
2. Make a Http Request to Apollo Server
3. Unmarshal the response
4. Get the particular key/value
4. Return back the value/error
//TODO Use the contentType to send the response
*/
// Compose the URL
pullConfigurationURL := apolloClient.composeURL()
// Make a Http Request to Apollo Server
resp, err := apolloClient.HTTPDo("GET", pullConfigurationURL, nil, nil)
if err != nil {
openlogging.GetLogger().Error("Error in Querying the Response from Apollo: " + err.Error())
return nil, err
}
if resp.StatusCode != 200 {
openlogging.GetLogger().Error("Bad Response : " + "Response from Apollo Server " + resp.Status)
return nil, errors.New("Bad Response from Apollo Server " + resp.Status)
}
//Unmarshal the response
var body []byte
body, err = ioutil.ReadAll(resp.Body)
var configurations map[string]interface{}
error := serializers.Decode(defaultContentType, body, &configurations)
if error != nil {
openlogging.GetLogger().Error("Error in Unmarshalling the Response from Apollo: " + err.Error())
return nil, err
}
//Find the particular Key
configList := configurations["configurations"]
configurationsValue := ""
isFound := false
for configKey, configValue := range configList.(map[string]interface{}) {
if configKey == key {
configurationsValue = configValue.(string)
isFound = true
}
}
if !isFound {
openlogging.GetLogger().Error("Error in fetching the configurations for particular value" + "No Key found : " + key)
return nil, errors.New("No Key found : " + key)
}
openlogging.GetLogger().Debugf("The Key Value of : ", configurationsValue)
return configurationsValue, nil
}
// composeURL composes the URL based on the configurations given in chassis.yaml
func (apolloClient *Client) composeURL() string {
pullConfigurationURL := strings.Replace(apolloServerAPI, ":ServerURL", apolloClient.URI, 1)
pullConfigurationURL = strings.Replace(pullConfigurationURL, ":appID", apolloClient.serviceName, 1)
pullConfigurationURL = strings.Replace(pullConfigurationURL, ":clusterName", apolloClient.cluster, 1)
pullConfigurationURL = strings.Replace(pullConfigurationURL, ":nameSpace", apolloClient.namespace, 1)
return pullConfigurationURL
}
//PullConfigsByDI returns the configuration for additional Projects in Apollo
func (apolloClient *Client) PullConfigsByDI(dimensionInfo, diInfo string) (map[string]map[string]interface{}, error) {
// TODO Return the configurations for customized Projects in Apollo Configs
return nil, nil
}
// PushConfigs not implemented
func (apolloClient *Client) PushConfigs(data map[string]interface{}, dimensionInfo string) (map[string]interface{}, error) {
return map[string]interface{}{"Result": "not implemented"}, nil
}
// DeleteConfigsByKeys not implemented
func (apolloClient *Client) DeleteConfigsByKeys(keys []string, dimensionInfo string) (map[string]interface{}, error) {
return map[string]interface{}{"Result": "not implemented"}, nil
}
//InitConfigApollo initialize the Apollo Client
func InitConfigApollo(options config.Options) config.Client {
apolloClient := &Client{
serviceName: options.ApolloServiceName,
cluster: options.Cluster,
URI: options.ServerURI,
namespace: options.Namespace,
}
apolloClient.NewApolloClient()
return apolloClient
}
func (apolloClient *Client) Watch(f func(map[string]interface{}), errHandler func(err error)) error {
return nil
}
func init() {
config.InstallConfigClientPlugin(Name, InitConfigApollo)
}