-
Notifications
You must be signed in to change notification settings - Fork 250
/
openapi.go
95 lines (89 loc) · 2.86 KB
/
openapi.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
package openapi
import (
"github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
"github.com/go-openapi/spec"
"k8s.io/kube-openapi/pkg/builder"
"k8s.io/kube-openapi/pkg/common"
)
// code stolen/adapted from https://github.com/kubevirt/kubevirt/blob/master/pkg/util/openapi/openapi.go
func createOpenAPIConfig(webServices []*restful.WebService) restfulspec.Config {
return restfulspec.Config{
WebServices: webServices,
WebServicesURL: "",
APIPath: "/swaggerapi",
PostBuildSwaggerObjectHandler: addInfoToSwaggerObject,
}
}
func addInfoToSwaggerObject(swo *spec.Swagger) {
swo.Info = &spec.Info{
InfoProps: spec.InfoProps{
Title: "KubeVirt Containerized Data Importer API",
Description: "Containerized Data Importer for KubeVirt.",
Contact: &spec.ContactInfo{
Name: "kubevirt-dev",
Email: "kubevirt-dev@googlegroups.com",
URL: "https://github.com/kubevirt/containerized-data-importer",
},
License: &spec.License{
Name: "Apache 2.0",
URL: "https://www.apache.org/licenses/LICENSE-2.0",
},
},
}
swo.SecurityDefinitions = spec.SecurityDefinitions{
"BearerToken": &spec.SecurityScheme{
SecuritySchemeProps: spec.SecuritySchemeProps{
Type: "apiKey",
Name: "authorization",
In: "header",
Description: "Bearer Token authentication",
},
},
}
swo.Swagger = "2.0"
swo.Security = make([]map[string][]string, 1)
swo.Security[0] = map[string][]string{"BearerToken": {}}
}
func createConfig(getDefinitions common.GetOpenAPIDefinitions) *common.Config {
return &common.Config{
CommonResponses: map[int]spec.Response{
401: {
ResponseProps: spec.ResponseProps{
Description: "Unauthorized",
},
},
},
Info: &spec.Info{
InfoProps: spec.InfoProps{
Title: "KubeVirt Containerized Data Importer API",
Description: "Containerized Data Importer for KubeVirt.",
Contact: &spec.ContactInfo{
Name: "kubevirt-dev",
Email: "kubevirt-dev@googlegroups.com",
URL: "https://github.com/kubevirt/containerized-data-importer",
},
License: &spec.License{
Name: "Apache 2.0",
URL: "https://www.apache.org/licenses/LICENSE-2.0",
},
},
},
SecurityDefinitions: &spec.SecurityDefinitions{
"BearerToken": &spec.SecurityScheme{
SecuritySchemeProps: spec.SecuritySchemeProps{
Type: "apiKey",
Name: "authorization",
In: "header",
Description: "Bearer Token authentication",
},
},
},
GetDefinitions: getDefinitions,
}
}
// LoadOpenAPISpec creates a swagger doc for given webservice(s)
func LoadOpenAPISpec(webServices []*restful.WebService, getDefinitions common.GetOpenAPIDefinitions) (*spec.Swagger, error) {
config := createConfig(getDefinitions)
return builder.BuildOpenAPISpec(webServices, config)
}