forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation.go
308 lines (259 loc) · 7.42 KB
/
operation.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package apidocs
import (
"bytes"
"fmt"
"strings"
"github.com/go-openapi/spec"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type operation struct {
s *spec.Swagger
Path spec.PathItem
PathName string
Operation *spec.Operation
OpName string
gvk schema.GroupVersionKind
}
func (o operation) bodyParameter() *spec.Parameter {
for _, parameter := range o.Operation.Parameters {
if parameter.In == "body" {
return ¶meter
}
}
return nil
}
func (o operation) Description() string {
return ToUpper(o.Operation.Description)
}
func (o operation) Curl() string {
s := make([]string, 0, 7)
s = append(s, "$ curl -k")
if o.OpName != "Get" {
s = append(s, " -X "+strings.ToUpper(o.OpName))
}
bodyParameter := o.bodyParameter()
if bodyParameter != nil {
s = append(s, " -d @-")
}
s = append(s,
` -H "Authorization: Bearer $TOKEN"`,
" -H 'Accept: application/json'")
if bodyParameter != nil {
contentType := "application/json"
if o.Operation.Consumes[0] != "*/*" {
contentType = o.Operation.Consumes[0]
}
s = append(s, fmt.Sprintf(" -H 'Content-Type: %s'", contentType))
}
s = append(s, " https://$ENDPOINT"+EnvStyle(o.PathName))
var postamble string
if bodyParameter != nil {
postamble = " <<'EOF'\n" + o.sampleRequest() + "EOF"
}
return strings.Join(s, " \\\n ") + postamble
}
func (o operation) HTTPReq() string {
s := make([]string, 0, 7)
s = append(s,
strings.ToUpper(o.OpName)+" "+EnvStyle(o.PathName)+" HTTP/1.1",
"Authorization: Bearer $TOKEN",
"Accept: application/json",
"Connection: close")
if o.bodyParameter() != nil {
contentType := "application/json"
if o.Operation.Consumes[0] != "*/*" {
contentType = o.Operation.Consumes[0]
}
s = append(s,
fmt.Sprintf("Content-Type: %s'", contentType),
"",
o.sampleRequest())
}
return strings.Join(s, "\n")
}
func getGVK(s spec.Schema, gv schema.GroupVersion) schema.GroupVersionKind {
for _, gvk := range GroupVersionKinds(s) {
if gvk.Group == gv.Group && gvk.Version == gv.Version {
return gvk
}
}
return GroupVersionKinds(s)[0]
}
func (o operation) sampleRequest() string {
bodyType := RefType(o.bodyParameter().Schema)
switch bodyType {
case "io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions",
"io.k8s.apimachinery.pkg.apis.meta.v1.Patch":
return "{\n ...\n}\n"
}
gvk := getGVK(o.s.Definitions[bodyType], o.gvk.GroupVersion())
return fmt.Sprintf("{\n \"kind\": \"%s\",\n \"apiVersion\": \"%s\",\n ...\n}\n", gvk.Kind, gvk.GroupVersion().String())
}
func (o operation) Parameters(t string) []spec.Parameter {
parameters := make([]spec.Parameter, 0, len(o.Path.Parameters)+len(o.Operation.Parameters))
for _, param := range append(o.Path.Parameters, o.Operation.Parameters...) {
if param.In == t {
parameters = append(parameters, param)
}
}
return parameters
}
func (o operation) Verb() string {
switch o.OpName {
case "Get":
if strings.Contains(o.PathName, "/watch/") {
return "Watch"
}
case "Post":
return "Create"
case "Put":
return "Update"
}
return o.OpName
}
func (o operation) Plural() bool {
return o.OpName != "Post" && !strings.Contains(o.PathName, "{name}")
}
func (o operation) Subresource() string {
if i := strings.LastIndex(o.PathName, "/{name}/"); i != -1 {
return o.PathName[i+8:]
}
return ""
}
func (o operation) Namespaced() bool {
return strings.Contains(o.PathName, "/namespaces/{namespace}")
}
func (o operation) IsProxy() bool {
return o.gvk.Group == "" &&
o.gvk.Version == "v1" &&
(o.gvk.Kind == "Node" || o.gvk.Kind == "Pod" || o.gvk.Kind == "Service") &&
strings.Contains(o.PathName, "/proxy")
}
func (o operation) Anchor() string {
return strings.Map(func(r rune) rune {
switch r {
case '/':
return '-'
case '{', '}':
return -1
default:
return r
}
}, o.OpName+"/"+strings.Trim(o.PathName, "/"))
}
func (o operation) Title() string {
buf := &bytes.Buffer{}
if o.IsProxy() {
fmt.Fprintf(buf, "Proxy %s request to a %s", strings.ToUpper(o.OpName), o.gvk.Kind)
if o.Namespaced() {
fmt.Fprintf(buf, " in a namespace")
}
if strings.Contains(o.PathName, "/{path}") {
fmt.Fprintf(buf, " (with path)")
}
return buf.String()
}
if o.gvk.Group == "" &&
o.gvk.Version == "v1" &&
o.gvk.Kind == "Pod" {
switch {
case strings.HasSuffix(o.PathName, "/attach"):
return fmt.Sprintf("Attach to a v1.Pod in a namespace (%s)", strings.ToUpper(o.OpName))
case strings.HasSuffix(o.PathName, "/exec"):
return fmt.Sprintf("Exec in a v1.Pod in a namespace (%s)", strings.ToUpper(o.OpName))
case strings.HasSuffix(o.PathName, "/portforward"):
return fmt.Sprintf("Port-forward to a v1.Pod in a namespace (%s)", strings.ToUpper(o.OpName))
}
}
fmt.Fprintf(buf, "%s ", o.Verb())
subresource := o.Subresource()
if subresource != "" {
fmt.Fprintf(buf, "%s of ", subresource)
}
if o.Plural() {
fmt.Fprintf(buf, "all %s", Pluralise(o.gvk.Kind))
} else {
fmt.Fprintf(buf, "a %s", o.gvk.Kind)
}
if o.Namespaced() {
fmt.Fprintf(buf, " in a namespace")
}
return buf.String()
}
func (o operation) GVR() (gvr schema.GroupVersionResource, err error) {
parts := strings.Split(strings.Trim(o.PathName, "/"), "/")
if !((parts[0] == "apis" && len(parts) > 3) ||
(parts[0] == "api" && len(parts) > 2) ||
(parts[0] == "oapi" && len(parts) > 2)) {
err = fmt.Errorf("GVR() called on invalid path %s", o.PathName)
return
}
var i int
switch parts[0] {
case "apis":
gvr.Group = parts[1]
gvr.Version = parts[2]
i = 3
case "api", "oapi":
gvr.Version = parts[1]
i = 2
}
if i < len(parts)-1 && parts[i] == "watch" {
i++
}
if i < len(parts)-2 && parts[i] == "namespaces" && parts[i+1] == "{namespace}" {
i += 2
}
gvr.Resource = parts[i]
return
}
func (o operation) GVK() (schema.GroupVersionKind, error) {
parts := strings.Split(strings.Trim(o.PathName, "/"), "/")
if parts[0] != "api" && parts[0] != "oapi" && parts[0] != "apis" {
return schema.GroupVersionKind{}, fmt.Errorf("GVK() called on invalid path %s", o.PathName)
}
if (parts[0] == "apis" && len(parts) <= 3) || len(parts) <= 2 {
// API group root. Categorize by operation return value.
sch := o.s.Definitions[RefType(o.Operation.Responses.StatusCodeResponses[200].Schema)]
return GroupVersionKinds(sch)[0], nil
}
gvr, err := o.GVR()
if err != nil {
return schema.GroupVersionKind{}, err
}
if gvr.Group == "extensions" &&
gvr.Version == "v1beta1" &&
gvr.Resource == "replicationcontrollers" {
return schema.GroupVersionKind{
Version: "v1",
Kind: "ReplicationController",
}, nil
}
var kind string
switch {
case gvr.Resource == "endpoints",
gvr.Resource == "securitycontextconstraints":
// kind is used in the plural form: don't remove 's'
kind = gvr.Resource
case gvr.Group == "template.openshift.io" && gvr.Resource == "processedtemplates":
kind = "template"
case strings.HasSuffix(gvr.Resource, "ies"):
kind = gvr.Resource[:len(gvr.Resource)-3] + "y"
case strings.HasSuffix(gvr.Resource, "ses"):
kind = gvr.Resource[:len(gvr.Resource)-2]
case strings.HasSuffix(gvr.Resource, "es"):
kind = gvr.Resource[:len(gvr.Resource)-1]
case strings.HasSuffix(gvr.Resource, "s"):
kind = gvr.Resource[:len(gvr.Resource)-1]
}
for _, def := range o.s.Definitions {
for _, gvk := range GroupVersionKinds(def) {
if gvk.Group == gvr.Group &&
gvk.Version == gvr.Version &&
strings.ToLower(gvk.Kind) == kind {
return gvk, nil
}
}
}
return schema.GroupVersionKind{}, fmt.Errorf("GVK for %s not found", gvr)
}