-
Notifications
You must be signed in to change notification settings - Fork 66
/
client_update.go
207 lines (194 loc) · 7.11 KB
/
client_update.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
203
204
205
206
207
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package repository
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/uber/zanzibar/codegen"
reqerr "github.com/uber/zanzibar/codegen/errors"
)
// ClientJSONConfig defines the JSON content of the configuration file.
type ClientJSONConfig struct {
Name string `json:"name"`
Type string `json:"type"`
Config *configField `json:"config"`
}
type configField struct {
ThriftFile string `json:"thriftFile"`
ThriftFileSha string `json:"thriftFileSha"`
ExposedMethods map[string]string `json:"exposedMethods,omitempty"`
}
// NewClientConfigJSON converts ClientConfig to ClientJSONConfig.
func NewClientConfigJSON(cfg *ClientConfig) *ClientJSONConfig {
cfgJSON := &ClientJSONConfig{
Name: cfg.Name,
Type: string(cfg.Type),
Config: &configField{
ThriftFile: cfg.ThriftFile,
ExposedMethods: cfg.ExposedMethods,
},
}
return cfgJSON
}
// UpdateClientConfigs updates JSON configuration files for a client update request.
func (r *Repository) UpdateClientConfigs(req *ClientConfig, clientCfgDir, thriftFileSha string) error {
if err := validateClientUpdateRequest(req); err != nil {
return err
}
cfgJSON := NewClientConfigJSON(req)
// Expose all methods in the thrift file by default.
if len(cfgJSON.Config.ExposedMethods) == 0 {
cfg, err := r.LatestGatewayConfig()
if err != nil {
return errors.Wrap(err, "invalid configuration before updating client")
}
exposedMethods, err := allExposedMethods(cfg.ThriftServices, req.ThriftFile)
if err != nil {
return errors.Wrapf(err, "failed to generate all methods in thrift file %s", req.ThriftFile)
}
cfgJSON.Config.ExposedMethods = exposedMethods
}
cfgJSON.Config.ThriftFileSha = thriftFileSha
clientPath := filepath.Join(r.absPath(clientCfgDir), cfgJSON.Name)
r.Lock()
defer r.Unlock()
if err := os.MkdirAll(clientPath, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create client config dir")
}
if err := writeToJSONFile(filepath.Join(clientPath, clientConfigFileName), cfgJSON); err != nil {
return errors.Wrapf(err, "failed to write config for the client %q", cfgJSON.Name)
}
if err := WriteClientModuleJSON(r.absPath(clientCfgDir)); err != nil {
return errors.Wrap(err, "failed to write module config for all clients")
}
if err := UpdateProductionConfigJSON(req, r.absPath(productionCfgJSONPath)); err != nil {
return errors.Wrap(err, "failed to update gateway production config")
}
return nil
}
func allExposedMethods(thriftServices map[string]map[string]*ThriftService, thriftFile string) (map[string]string, error) {
serviceMap, ok := thriftServices[thriftFile]
if !ok {
return nil, reqerr.NewRequestError(
reqerr.ClientsThriftFile, errors.Errorf("endpoint thrift file %q not found", thriftFile))
}
exposedMethods := map[string]string{}
for _, tservice := range serviceMap {
for _, method := range tservice.Methods {
exposedName := tservice.Name + "::" + method.Name
if pre, ok := exposedMethods[method.Name]; ok {
return nil, reqerr.NewRequestError(
reqerr.ClientsExposedMethods, errors.Errorf("duplicated method name for %q and %q", pre, exposedName))
}
exposedMethods[strings.Title(method.Name)] = exposedName
}
}
return exposedMethods, nil
}
func validateClientUpdateRequest(req *ClientConfig) error {
if req.Type == "tchannel" && req.ServiceName == "" {
return reqerr.NewRequestError(
reqerr.ClientsServiceName, errors.New("invalid request: muttley name is required for tchannel client"))
}
if req.IP == "" || req.Port == 0 {
return reqerr.NewRequestError(
reqerr.ClientsIP, errors.New("invalid request: ip is required"))
}
if req.Port == 0 {
return reqerr.NewRequestError(
reqerr.ClientsPort, errors.New("invalid request: port is required"))
}
if req.Type == "tchannel" && req.Timeout == 0 {
req.Timeout = 10000
}
if req.Type == "tchannel" && req.TimeoutPerAttempt == 0 {
req.TimeoutPerAttempt = 10000
}
return nil
}
// WriteClientModuleJSON writes the JSON file for the module to contain all clients.
func WriteClientModuleJSON(clientCfgDir string) error {
files, err := ioutil.ReadDir(clientCfgDir)
if err != nil {
return err
}
subDirs := []string{}
for _, file := range files {
if file.IsDir() {
subDirs = append(subDirs, file.Name())
}
}
sort.Strings(subDirs)
content := &codegen.ClientClassConfig{
Name: "clients",
Type: "init",
Config: map[string]interface{}{},
Dependencies: codegen.ClientDependencies{
Client: subDirs,
},
}
return writeToJSONFile(filepath.Join(clientCfgDir, clientModuleFileName), content)
}
// UpdateProductionConfigJSON updates the production JSON config with client updates.
func UpdateProductionConfigJSON(req *ClientConfig, productionCfgJSONPath string) error {
content := map[string]interface{}{}
if err := readJSONFile(productionCfgJSONPath, &content); err != nil {
return err
}
// Update fields related to a client.
prefix := "clients." + req.Name + "."
if req.Type == "tchannel" {
content[prefix+"serviceName"] = req.ServiceName
content[prefix+"timeout"] = req.Timeout
content[prefix+"timeoutPerAttempt"] = req.TimeoutPerAttempt,
content[prefix+"routingKey"] = req.routingKey
}
content[prefix+"port"] = req.Port
content[prefix+"ip"] = req.IP
return writeToJSONFile(productionCfgJSONPath, content)
}
// writeToJSONFile writes content into a json file.
func writeToJSONFile(file string, content interface{}) error {
b, err := json.MarshalIndent(content, "", "\t")
if err != nil {
return errors.Wrapf(err, "failed to marshal the content for file %q", file)
}
b = append(b, []byte("\n")...)
if err = ioutil.WriteFile(file, b, os.ModePerm); err != nil {
return errors.Wrapf(err, "failed to write to file %q", file)
}
return nil
}
func readJSONFile(file string, content interface{}) error {
b, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "failed to read file %q", file)
}
err = json.Unmarshal(b, &content)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal file %q", file)
}
return nil
}