/
endpoint_update.go
180 lines (170 loc) · 6.2 KB
/
endpoint_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
// 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 (
"os"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/uber/zanzibar/codegen"
reqerr "github.com/uber/zanzibar/codegen/errors"
)
// WriteEndpointConfig writes endpoint configs and its test cases into a runtime repository and
// also updates the meta json file for all endpoints.
func (r *Repository) WriteEndpointConfig(
endpointCfgDir string,
config *EndpointConfig,
thriftFileSha string,
) error {
if err := r.validateEndpointCfg(config); err != nil {
return errors.Wrap(err, "invalid endpoint config")
}
r.Lock()
defer r.Unlock()
endpointDir := codegen.CamelToSnake(strings.TrimSuffix(config.ID, "."+config.HandleID))
dir := filepath.Join(r.absPath(endpointCfgDir), endpointDir)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return errors.Wrap(err, "failed to create endpoint config dir")
}
fileName := codegen.CamelToSnake(config.HandleID) + ".json"
config.ThriftFileSha = thriftFileSha
err = writeToJSONFile(filepath.Join(dir, fileName), config)
if err != nil {
return errors.Wrap(err, "failed to write to endpoint config file")
}
err = updateEndpointMetaJSON(dir, endpointConfigFileName, fileName, config)
if err != nil {
return errors.Wrap(err, "failed to write endpoint group configuration")
}
serviceConfigDir := filepath.Join(r.absPath(endpointCfgDir), "../services/", r.gatewayConfig.ID)
err = updateServiceMetaJSON(serviceConfigDir, serviceConfigFileName, config)
if err != nil {
return errors.Wrap(err, "failed to write service group configuration")
}
return nil
}
func (r *Repository) validateEndpointCfg(req *EndpointConfig) error {
gatewayConfig, err := r.LatestGatewayConfig()
if err != nil {
return errors.Wrap(err, "invalid configuration before updating endpoint")
}
clientCfg, ok := gatewayConfig.Clients[req.ClientID]
if !ok {
return reqerr.NewRequestError(
reqerr.EndpointsClientID, errors.Errorf("can't find client %q", req.ClientID))
}
if clientCfg.Type == HTTP {
req.WorkflowType = "httpClient"
} else if clientCfg.Type == TCHANNEL {
req.WorkflowType = "tchannelClient"
} else {
return reqerr.NewRequestError(reqerr.ClientsType,
errors.Errorf("client type %q is not supported", clientCfg.Type))
}
for _, mid := range req.Middlewares {
for _, midCfg := range r.gatewayConfig.RawMiddlewares {
if mid.Name == midCfg.Name {
absFile := "file://" + r.absPath(midCfg.SchemaFile)
err := codegen.SchemaValidateGo(absFile, mid.Options)
if err != nil {
return err
}
}
}
}
return nil
}
// updateServiceMetaJSON adds an endpoint group in the meta json file or updates the config for an existing endpoint.
func updateServiceMetaJSON(configDir, serviceConfigJSONPath string, cfg *EndpointConfig) error {
metaFilePath := filepath.Join(configDir, serviceConfigJSONPath)
fileContent := new(codegen.EndpointClassConfig)
if _, err := os.Stat(metaFilePath); !os.IsNotExist(err) {
err := readJSONFile(metaFilePath, fileContent)
if err != nil {
return err
}
}
endpoints := fileContent.Dependencies["endpoint"]
sort.Strings(endpoints)
i := sort.SearchStrings(endpoints, cfg.ID)
// not update if client id already exist
if i < len(endpoints) && endpoints[i] == cfg.ID {
return nil
}
// update endpoint list with the new client id
fileContent.Dependencies["endpoint"] = append(fileContent.Dependencies["endpoint"], cfg.ID)
return writeToJSONFile(metaFilePath, fileContent)
}
// updateEndpointMetaJSON adds an endpoint in the meta json file or updates the config for an existing endpoint.
func updateEndpointMetaJSON(configDir, metaFile, newFile string, cfg *EndpointConfig) error {
metaFilePath := filepath.Join(configDir, metaFile)
fileContent := new(codegen.EndpointClassConfig)
if _, err := os.Stat(metaFilePath); !os.IsNotExist(err) {
err := readJSONFile(metaFilePath, fileContent)
if err != nil {
return err
}
}
var err error
fileContent.Config.Endpoints, err = addToEndpointList(fileContent.Config.Endpoints, newFile, configDir)
if err != nil {
return err
}
if fileContent.Dependencies == nil {
fileContent.Dependencies = make(map[string][]string)
}
if c := fileContent.Dependencies["client"]; !findString(cfg.ClientID, c) {
fileContent.Dependencies["client"] = append(c, cfg.ClientID)
}
fileContent.Name = cfg.ID
if fileContent.Type == "" {
fileContent.Type = string(cfg.Type)
}
return writeToJSONFile(metaFilePath, fileContent)
}
// addToEndpointList adds 'newFile' to the endpoint list if it doesn't exist.
func addToEndpointList(curEndpoints []string, newFile string, configDir string) ([]string, error) {
newFilePath, err := filepath.Abs(filepath.Join(configDir, newFile))
if err != nil {
return nil, err
}
oldFilePaths := make([]string, len(curEndpoints))
for i, path := range curEndpoints {
file, err := filepath.Abs(filepath.Join(configDir, path))
if err != nil {
return nil, err
}
oldFilePaths[i] = file
}
if !findString(newFilePath, oldFilePaths) {
curEndpoints = append(curEndpoints, newFile)
}
return curEndpoints, nil
}
func findString(target string, array []string) bool {
for _, str := range array {
if str == target {
return true
}
}
return false
}