-
Notifications
You must be signed in to change notification settings - Fork 165
/
swagger.go
301 lines (261 loc) · 10 KB
/
swagger.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
package swagger
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"encoding/json"
"net/http"
"os"
"reflect"
"k8s.io/utils/strings/slices"
"github.com/Azure/ARO-RP/pkg/util/stringutils"
)
// resourceName represents the tracked resource, OpenShiftCluster
var resourceName = "OpenShiftCluster"
// proxyResources represent the list of proxy resources - these are resources with operations, but do not exist in the Azure Portal
// https://github.com/Azure/azure-resource-manager-rpc/blob/master/v1.0/proxy-api-reference.md
var proxyResources = []string{
"SyncSet",
"SyncIdentityProvider",
"MachinePool",
"Secret",
"OpenShiftVersion",
}
// resourceNamePattern is a regex pattern to validate resource names
const resourceNamePattern = `^[a-zA-Z0-9]$|^[a-zA-Z0-9][-_a-zA-Z0-9]*[a-zA-Z0-9]$`
func Run(api, outputDir string) error {
g, err := New(api)
if err != nil {
return err
}
s := &Swagger{
Swagger: "2.0",
Info: &Info{
Title: "Azure Red Hat OpenShift Client",
Description: "Rest API for Azure Red Hat OpenShift 4",
Version: stringutils.LastTokenByte(outputDir, '/'),
},
Host: "management.azure.com",
Schemes: []string{"https"},
Consumes: []string{"application/json"},
Produces: []string{"application/json"},
Paths: g.populateTopLevelPaths("Microsoft.RedHatOpenShift", "openShiftCluster", "OpenShift cluster"),
Definitions: Definitions{},
Parameters: ParametersDefinitions{
"api-version": &Parameter{
Name: "api-version",
Description: "The version of the API the caller wants to use.",
Required: true,
Type: "string",
In: "query",
Pattern: "^\\d{2}-\\d{2}-\\d{4}(-preview)?$",
XMSParameterLocation: "client",
},
},
SecurityDefinitions: SecurityDefinitions{
"azure_auth": {
Type: "oauth2",
AuthorizationURL: "https://login.microsoftonline.com/common/oauth2/authorize",
Flow: "implicit",
Description: "Azure Active Directory OAuth2 Flow",
Scopes: map[string]string{
"user_impersonation": "impersonate your user account",
},
},
},
Security: []SecurityRequirement{
{
"azure_auth": []string{"user_impersonation"},
},
},
}
s.Paths["/providers/Microsoft.RedHatOpenShift/operations"] = &PathItem{
Get: &Operation{
Tags: []string{"Operations"},
Summary: "Lists all of the available RP operations.",
Description: "The operation returns the RP operations.",
OperationID: "Operations_List",
Parameters: g.populateParameters(0, "Operation", "Operation"),
Responses: g.populateResponses("OperationList", false, http.StatusOK),
Pageable: &Pageable{
NextLinkName: "nextLink",
},
},
}
s.Paths["/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/openShiftClusters/{resourceName}/listCredentials"] = &PathItem{
Post: &Operation{
Tags: []string{"OpenShiftClusters"},
Summary: "Lists credentials of an OpenShift cluster with the specified subscription, resource group and resource name.",
Description: "The operation returns the credentials.",
OperationID: "OpenShiftClusters_ListCredentials",
Parameters: g.populateParameters(3, "OpenShiftCluster", "OpenShift cluster"),
Responses: g.populateResponses("OpenShiftClusterCredentials", false, http.StatusOK),
},
}
if g.kubeConfig {
s.Paths["/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RedHatOpenShift/openShiftClusters/{resourceName}/listAdminCredentials"] = &PathItem{
Post: &Operation{
Tags: []string{"OpenShiftClusters"},
Summary: "Lists admin kubeconfig of an OpenShift cluster with the specified subscription, resource group and resource name.",
Description: "The operation returns the admin kubeconfig.",
OperationID: "OpenShiftClusters_ListAdminCredentials",
Parameters: g.populateParameters(3, "OpenShiftCluster", "OpenShift cluster"),
Responses: g.populateResponses("OpenShiftClusterAdminKubeconfig", false, http.StatusOK),
},
}
}
if g.installVersionList {
s.Paths["/subscriptions/{subscriptionId}/providers/Microsoft.RedHatOpenShift/locations/{location}/openshiftversions"] = &PathItem{
Get: &Operation{
Tags: []string{"OpenShiftVersions"},
Summary: "Lists all OpenShift versions available to install in the specified location.",
Description: "The operation returns the installable OpenShift versions as strings.",
OperationID: "OpenShiftVersions_List",
Parameters: g.populateParameters(6, "OpenShiftVersionList", "OpenShift Versions"),
Responses: g.populateResponses("OpenShiftVersionList", false, http.StatusOK),
Pageable: &Pageable{
NextLinkName: "nextLink",
},
},
}
}
if g.clusterManager {
g.populateChildResourcePaths(s.Paths, "Microsoft.RedHatOpenShift", "openShiftCluster", "syncSet", "SyncSet")
g.populateChildResourcePaths(s.Paths, "Microsoft.RedHatOpenShift", "openShiftCluster", "machinePool", "MachinePool")
g.populateChildResourcePaths(s.Paths, "Microsoft.RedHatOpenShift", "openShiftCluster", "syncIdentityProvider", "SyncIdentityProvider")
g.populateChildResourcePaths(s.Paths, "Microsoft.RedHatOpenShift", "openShiftCluster", "secret", "Secret")
}
populateExamples(s.Paths)
// This begins to define definitions required for the paths, parameters, and responses defined above
names := []string{"OpenShiftClusterList", "OpenShiftClusterCredentials"}
if g.kubeConfig {
names = append(names, "OpenShiftClusterAdminKubeconfig")
}
if g.installVersionList {
names = append(names, "OpenShiftVersionList")
}
if g.clusterManager {
// This needs to be the top level struct
// in most cases, the "list" struct (a collection of resources)
names = append(names, "SyncSetList", "MachinePoolList", "SyncIdentityProviderList", "SecretList")
}
err = define(s.Definitions, api, g.xmsEnum, g.xmsSecretList, g.xmsIdentifiers, names...)
if err != nil {
return err
}
names = []string{"CloudError", "OperationList"}
err = define(s.Definitions, "github.com/Azure/ARO-RP/pkg/api", g.xmsEnum, g.xmsSecretList, g.xmsIdentifiers, names...)
if err != nil {
return err
}
// This begins the ARM / Azure Resources definition generation
azureResources := []string{
resourceName,
}
if g.clusterManager {
azureResources = append(azureResources, "SyncSet", "MachinePool", "SyncIdentityProvider", "Secret")
}
if g.installVersionList {
azureResources = append(azureResources, "OpenShiftVersion")
}
for _, azureResource := range azureResources {
def, err := deepCopy(s.Definitions[azureResource])
if err != nil {
return err
}
update := def.(*Schema)
var properties []NameSchema
for _, property := range s.Definitions[azureResource].Properties {
switch property.Name {
case "id", "name", "type", "location":
case "properties":
property.Schema.ClientFlatten = true
fallthrough
default:
properties = append(properties, property)
}
}
update.Properties = properties
// If this resource is not a proxy resource mark as tracked resource
// otherwise, its a proxy resource and we remove the proxyResource .Allof ref for the Update definition
// in order to make azure-rest-specs-api validation happy
if !slices.Contains(proxyResources, azureResource) {
s.Definitions[azureResource].AllOf = []Schema{
{
Ref: "../../../../../common-types/resource-management/" + g.commonTypesVersion + "/types.json#/definitions/TrackedResource",
},
}
} else {
update.AllOf = []Schema{}
}
properties = nil
for _, property := range s.Definitions[azureResource].Properties {
if property.Name == "properties" {
property.Schema.ClientFlatten = true
properties = append(properties, property)
}
}
s.Definitions[azureResource].Properties = properties
// Don't include an update object for "OpenShiftVersion" as it is not updatable via the API
azureResources := []string{azureResource}
if azureResource != "OpenShiftVersion" {
s.Definitions[azureResource+"Update"] = update
azureResources = append(azureResources, azureResource+"Update")
}
if g.systemData {
s.defineSystemData(azureResources, g.commonTypesVersion)
}
}
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
b = append(b, '\n')
err = g.generateExamples(outputDir, s)
if err != nil {
return err
}
return os.WriteFile(outputDir+"/redhatopenshift.json", b, 0666)
}
func deepCopy(v interface{}) (interface{}, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
w := reflect.New(reflect.TypeOf(v)).Interface()
err = json.Unmarshal(b, w)
if err != nil {
return nil, err
}
return reflect.ValueOf(w).Elem().Interface(), nil
}
// defineSystemData will configure systemData fields for required definitions.
// SystemData is not user consumable, so we remove definitions from auto-generated code
// In addition to this we use common-types definition so we replace one we generate with common-types
func (s *Swagger) defineSystemData(resources []string, commonVersion string) {
for _, resource := range resources {
s.Definitions[resource].Properties = removeNamedSchemas(s.Definitions[resource].Properties, "systemData")
// SystemData is not user side consumable type. It is being returned as Read-Only,
// but should not be generated into API or swagger as API/SDK type
delete(s.Definitions, "SystemData")
delete(s.Definitions, "CreatedByType")
s.Definitions[resource].Properties = append(s.Definitions[resource].Properties,
NameSchema{
Name: "systemData",
Schema: &Schema{
ReadOnly: true,
Description: "The system meta data relating to this resource.",
Ref: "../../../../../common-types/resource-management/" + commonVersion + "/types.json#/definitions/systemData",
},
})
}
}
func removeNamedSchemas(list NameSchemas, remove string) NameSchemas {
var result NameSchemas
for _, schema := range list {
if schema.Name == remove {
continue
}
result = append(result, schema)
}
return result
}