-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
206 lines (170 loc) · 5.03 KB
/
services.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
package integra
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"os"
"path"
"strings"
"gopkg.in/yaml.v2"
"tractor.dev/integra/internal/jsonaccess"
)
//go:embed services
var services embed.FS
// SplitSelectorVersion takes a selector with an optional version
// at the end and returns the selector and the version if any
func SplitSelectorVersion(s string) (selector string, version string) {
parts := strings.SplitN(s, "@", 2)
if len(parts) > 1 {
return parts[0], parts[1]
}
return parts[0], ""
}
func AvailableServices() (names []string) {
fs.WalkDir(services, "services", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
if strings.HasSuffix(path, "/meta.yaml") {
path = strings.TrimPrefix(path, "services/")
path = strings.TrimSuffix(path, "/meta.yaml")
path = strings.ReplaceAll(path, "/", "-")
names = append(names, path)
}
return nil
})
return
}
func ServiceToken(service string) string {
service = strings.ReplaceAll(strings.ToUpper(service), "-", "_")
return os.Getenv(fmt.Sprintf("%s_TOKEN", service))
}
func ServiceClientCredentials(service string) (string, string) {
service = strings.ReplaceAll(strings.ToUpper(service), "-", "_")
return os.Getenv(fmt.Sprintf("%s_CLIENT_ID", service)), os.Getenv(fmt.Sprintf("%s_CLIENT_SECRET", service))
}
func LoadService(name, version string) (Service, error) {
serviceDir := strings.ReplaceAll(name, "-", "/")
b, err := fs.ReadFile(services, path.Join("services", serviceDir, "meta.yaml"))
if err != nil {
return nil, err
}
var yamlData map[any]any
if err := yaml.Unmarshal(b, &yamlData); err != nil {
return nil, err
}
meta := jsonaccess.New(convertYAMLToStringMap(yamlData))
if version == "" {
version = jsonaccess.MustAs[string](meta.Get("latest"))
}
dir, err := fs.ReadDir(services, path.Join("services", serviceDir, version))
if err != nil {
return nil, err
}
for _, info := range dir {
switch info.Name() {
case "openapi.json":
b, err = fs.ReadFile(services, path.Join("services", serviceDir, version, "openapi.json"))
if err != nil {
return nil, err
}
var data map[string]any
if err := json.Unmarshal(b, &data); err != nil {
return nil, err
}
root := jsonaccess.New(data)
resolver := jsonaccess.NewPointerResolver(root)
root = root.WithResolver(resolver).WithAllOfMerge()
return &openapiService{name: name, schema: root, meta: meta}, nil
case "openapi.yaml":
b, err = fs.ReadFile(services, path.Join("services", serviceDir, version, "openapi.yaml"))
if err != nil {
return nil, err
}
var raw map[any]any
if err := yaml.Unmarshal(b, &raw); err != nil {
return nil, err
}
data := convertYAMLToStringMap(raw)
root := jsonaccess.New(data)
resolver := jsonaccess.NewPointerResolver(root)
root = root.WithResolver(resolver).WithAllOfMerge()
return &openapiService{name: name, schema: root, meta: meta}, nil
case "googleapi.json":
b, err = fs.ReadFile(services, path.Join("services", serviceDir, version, "googleapi.json"))
if err != nil {
return nil, err
}
var data map[string]any
if err := json.Unmarshal(b, &data); err != nil {
return nil, err
}
root := jsonaccess.New(data)
resolver := jsonaccess.NewDefinitionsResolver(root.Get("schemas"))
root = root.WithResolver(resolver).WithAllOfMerge()
return &googleService{name: name, schema: root, meta: meta}, nil
}
}
return nil, fmt.Errorf("no schema found for %s@%s", name, version)
}
func ExpandURL(u string, params map[string]any) (string, error) {
for k, v := range params {
u = strings.Replace(u, fmt.Sprintf("{%s}", k), fmt.Sprint(v), 1)
}
if strings.Contains(u, "{") {
return "", fmt.Errorf("parameters not sufficient to expand URL: %s", u)
}
return u, nil
}
func MakeRequest(op Operation, in map[string]any) (*http.Request, error) {
var required []string
for _, p := range op.Parameters() {
if p.Required() {
required = append(required, p.Name())
}
}
for _, name := range required {
_, ok := in[name]
if !ok {
return nil, fmt.Errorf("missing '%s' of required parameters: %v", name, required)
}
}
data := make(map[string]any)
for k, v := range in {
data[k] = v
}
params := make(map[string]any)
for _, p := range op.Parameters() {
v, ok := data[p.Name()]
if ok {
params[p.Name()] = v
delete(data, p.Name())
}
}
u, err := ExpandURL(op.URL(), params)
if err != nil {
return nil, err
}
// var body io.Reader
// if slices.Contains([]string{"create", "set", "update"}, op.Name()) {
// b, err := json.Marshal(params)
// if err != nil {
// log.Fatal(err)
// }
// body = bytes.NewBuffer(b)
// }
// todo: need to populate url since implementing new model
req, err := http.NewRequest(strings.ToUpper(op.Method()), u, nil)
if err != nil {
return nil, err
}
// todo: alternative schemes
token := ServiceToken(op.Resource().Service().Name())
if token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}