@@ -52,6 +52,9 @@ const (
52
52
53
53
var (
54
54
swaggerPartialObjectMetadataDescriptions = metav1beta1.PartialObjectMetadata {}.SwaggerDoc ()
55
+
56
+ nameToken = "{name}"
57
+ namespaceToken = "{namespace}"
55
58
)
56
59
57
60
var definitions map [string ]common.OpenAPIDefinition
@@ -96,33 +99,33 @@ func BuildSwagger(crd *apiextensions.CustomResourceDefinition, version string) (
96
99
97
100
routes := make ([]* restful.RouteBuilder , 0 )
98
101
root := fmt .Sprintf ("/apis/%s/%s/%s" , b .group , b .version , b .plural )
102
+
99
103
if b .namespaced {
100
- routes = append (routes , b .buildRoute (root , "" , "GET" , "list" , sampleList ).
101
- Operation ("list" + b .kind + "ForAllNamespaces" ))
104
+ routes = append (routes , b .buildRoute (root , "" , "GET" , "list" , "list" , sampleList ).Operation ("list" + b .kind + "ForAllNamespaces" ))
102
105
root = fmt .Sprintf ("/apis/%s/%s/namespaces/{namespace}/%s" , b .group , b .version , b .plural )
103
106
}
104
- routes = append (routes , b .buildRoute (root , "" , "GET" , "list" , sampleList ))
105
- routes = append (routes , b .buildRoute (root , "" , "POST" , "create" , sample ).Reads (sample ))
106
- routes = append (routes , b .buildRoute (root , "" , "DELETE" , "deletecollection" , status ))
107
+ routes = append (routes , b .buildRoute (root , "" , "GET" , "list" , "list" , sampleList ))
108
+ routes = append (routes , b .buildRoute (root , "" , "POST" , "post" , " create" , sample ).Reads (sample ))
109
+ routes = append (routes , b .buildRoute (root , "" , "DELETE" , "deletecollection" , "deletecollection" , status ))
107
110
108
- routes = append (routes , b .buildRoute (root , "/{name}" , "GET" , "read" , sample ))
109
- routes = append (routes , b .buildRoute (root , "/{name}" , "PUT" , "replace" , sample ).Reads (sample ))
110
- routes = append (routes , b .buildRoute (root , "/{name}" , "DELETE" , "delete" , status ))
111
- routes = append (routes , b .buildRoute (root , "/{name}" , "PATCH" , "patch" , sample ).Reads (patch ))
111
+ routes = append (routes , b .buildRoute (root , "/{name}" , "GET" , "get" , " read" , sample ))
112
+ routes = append (routes , b .buildRoute (root , "/{name}" , "PUT" , "put" , " replace" , sample ).Reads (sample ))
113
+ routes = append (routes , b .buildRoute (root , "/{name}" , "DELETE" , "delete" , "delete" , status ))
114
+ routes = append (routes , b .buildRoute (root , "/{name}" , "PATCH" , "patch" , "patch" , sample ).Reads (patch ))
112
115
113
116
subresources , err := apiextensions .GetSubresourcesForVersion (crd , version )
114
117
if err != nil {
115
118
return nil , err
116
119
}
117
120
if subresources != nil && subresources .Status != nil {
118
- routes = append (routes , b .buildRoute (root , "/{name}/status" , "GET" , "read" , sample ))
119
- routes = append (routes , b .buildRoute (root , "/{name}/status" , "PUT" , "replace" , sample ).Reads (sample ))
120
- routes = append (routes , b .buildRoute (root , "/{name}/status" , "PATCH" , "patch" , sample ).Reads (patch ))
121
+ routes = append (routes , b .buildRoute (root , "/{name}/status" , "GET" , "get" , " read" , sample ))
122
+ routes = append (routes , b .buildRoute (root , "/{name}/status" , "PUT" , "put" , " replace" , sample ).Reads (sample ))
123
+ routes = append (routes , b .buildRoute (root , "/{name}/status" , "PATCH" , "patch" , "patch" , sample ).Reads (patch ))
121
124
}
122
125
if subresources != nil && subresources .Scale != nil {
123
- routes = append (routes , b .buildRoute (root , "/{name}/scale" , "GET" , "read" , scale ))
124
- routes = append (routes , b .buildRoute (root , "/{name}/scale" , "PUT" , "replace" , scale ).Reads (scale ))
125
- routes = append (routes , b .buildRoute (root , "/{name}/scale" , "PATCH" , "patch" , scale ).Reads (patch ))
126
+ routes = append (routes , b .buildRoute (root , "/{name}/scale" , "GET" , "get" , " read" , scale ))
127
+ routes = append (routes , b .buildRoute (root , "/{name}/scale" , "PUT" , "put" , " replace" , scale ).Reads (scale ))
128
+ routes = append (routes , b .buildRoute (root , "/{name}/scale" , "PATCH" , "patch" , "patch" , scale ).Reads (patch ))
126
129
}
127
130
128
131
for _ , route := range routes {
@@ -189,9 +192,9 @@ func subresource(path string) string {
189
192
panic ("failed to parse subresource; invalid path" )
190
193
}
191
194
192
- func (b * builder ) descriptionFor (path , verb string ) string {
195
+ func (b * builder ) descriptionFor (path , operationVerb string ) string {
193
196
var article string
194
- switch verb {
197
+ switch operationVerb {
195
198
case "list" :
196
199
article = " objects of kind "
197
200
case "read" , "replace" :
@@ -209,7 +212,7 @@ func (b *builder) descriptionFor(path, verb string) string {
209
212
if len (sub ) > 0 {
210
213
sub = " " + sub + " of"
211
214
}
212
- switch verb {
215
+ switch operationVerb {
213
216
case "patch" :
214
217
description = "partially update" + sub + article + b .kind
215
218
case "deletecollection" :
@@ -219,7 +222,7 @@ func (b *builder) descriptionFor(path, verb string) string {
219
222
}
220
223
description = "delete collection of" + sub + " " + b .kind
221
224
default :
222
- description = verb + sub + article + b .kind
225
+ description = operationVerb + sub + article + b .kind
223
226
}
224
227
225
228
return description
@@ -229,29 +232,35 @@ func (b *builder) descriptionFor(path, verb string) string {
229
232
// action can be one of: GET, PUT, PATCH, POST, DELETE;
230
233
// verb can be one of: list, read, replace, patch, create, delete, deletecollection;
231
234
// sample is the sample Go type for response type.
232
- func (b * builder ) buildRoute (root , path , action , verb string , sample interface {}) * restful.RouteBuilder {
235
+ func (b * builder ) buildRoute (root , path , httpMethod , actionVerb , operationVerb string , sample interface {}) * restful.RouteBuilder {
233
236
var namespaced string
234
237
if b .namespaced {
235
238
namespaced = "Namespaced"
236
239
}
237
- route := b .ws .Method (action ).
240
+ route := b .ws .Method (httpMethod ).
238
241
Path (root + path ).
239
242
To (func (req * restful.Request , res * restful.Response ) {}).
240
- Doc (b .descriptionFor (path , verb )).
243
+ Doc (b .descriptionFor (path , operationVerb )).
241
244
Param (b .ws .QueryParameter ("pretty" , "If 'true', then the output is pretty printed." )).
242
- Operation (verb + namespaced + b .kind + strings .Title (subresource (path ))).
245
+ Operation (operationVerb + namespaced + b .kind + strings .Title (subresource (path ))).
243
246
Metadata (endpoints .ROUTE_META_GVK , metav1.GroupVersionKind {
244
247
Group : b .group ,
245
248
Version : b .version ,
246
249
Kind : b .kind ,
247
250
}).
248
- Metadata (endpoints .ROUTE_META_ACTION , strings . ToLower ( action ) ).
251
+ Metadata (endpoints .ROUTE_META_ACTION , actionVerb ).
249
252
Produces ("application/json" , "application/yaml" ).
250
253
Returns (http .StatusOK , "OK" , sample ).
251
254
Writes (sample )
255
+ if strings .Contains (root , namespaceToken ) || strings .Contains (path , namespaceToken ) {
256
+ route .Param (b .ws .PathParameter ("namespace" , "object name and auth scope, such as for teams and projects" ).DataType ("string" ))
257
+ }
258
+ if strings .Contains (root , nameToken ) || strings .Contains (path , nameToken ) {
259
+ route .Param (b .ws .PathParameter ("name" , "name of the " + b .kind ).DataType ("string" ))
260
+ }
252
261
253
262
// Build consume media types
254
- if action == "PATCH" {
263
+ if httpMethod == "PATCH" {
255
264
route .Consumes ("application/json-patch+json" ,
256
265
"application/merge-patch+json" ,
257
266
"application/strategic-merge-patch+json" )
@@ -260,30 +269,30 @@ func (b *builder) buildRoute(root, path, action, verb string, sample interface{}
260
269
}
261
270
262
271
// Build option parameters
263
- switch verb {
272
+ switch actionVerb {
264
273
case "get" :
265
274
// TODO: CRD support for export is still under consideration
266
275
endpoints .AddObjectParams (b .ws , route , & metav1.GetOptions {})
267
276
case "list" , "deletecollection" :
268
277
endpoints .AddObjectParams (b .ws , route , & metav1.ListOptions {})
269
- case "replace " , "patch" :
278
+ case "put " , "patch" :
270
279
// TODO: PatchOption added in feature branch but not in master yet
271
280
endpoints .AddObjectParams (b .ws , route , & metav1.UpdateOptions {})
272
- case "create " :
281
+ case "post " :
273
282
endpoints .AddObjectParams (b .ws , route , & metav1.CreateOptions {})
274
283
case "delete" :
275
284
endpoints .AddObjectParams (b .ws , route , & metav1.DeleteOptions {})
276
285
route .Reads (& metav1.DeleteOptions {}).ParameterNamed ("body" ).Required (false )
277
286
}
278
287
279
288
// Build responses
280
- switch verb {
281
- case "create " :
289
+ switch actionVerb {
290
+ case "post " :
282
291
route .Returns (http .StatusAccepted , "Accepted" , sample )
283
292
route .Returns (http .StatusCreated , "Created" , sample )
284
293
case "delete" :
285
294
route .Returns (http .StatusAccepted , "Accepted" , sample )
286
- case "replace " :
295
+ case "put " :
287
296
route .Returns (http .StatusCreated , "Created" , sample )
288
297
}
289
298
0 commit comments