-
Notifications
You must be signed in to change notification settings - Fork 266
/
template.go
391 lines (360 loc) · 14 KB
/
template.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package generator
import "html/template"
type ClientGenConfig struct {
Verbs []string `marker:",optional"`
Resource string
SubResource string `marker:"subResource,optional"`
PackageName string
PackageAlias string
ClientName string
Expand bool `marker:"expand,optional"`
RateLimitKey string `marker:"rateLimitKey,optional"`
CrossSubFactory bool `marker:"crossSubFactory,optional"`
}
var ClientTemplate = template.Must(template.New("object-scaffolding-client-struct").Parse(`
type Client struct{
*{{.PackageAlias}}.{{.ClientName}}
subscriptionID string
tracer tracing.Tracer
}
`))
var ClientFactoryTemplate = template.Must(template.New("object-scaffolding-factory").Parse(`
func New(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (Interface, error) {
if options == nil {
options = utils.GetDefaultOption()
}
tr := options.TracingProvider.NewTracer(utils.ModuleName, utils.ModuleVersion)
client, err := {{.PackageAlias}}.New{{.ClientName}}(subscriptionID, credential, options)
if err != nil {
return nil, err
}
return &Client{
{{.ClientName}}: client,
subscriptionID: subscriptionID,
tracer: tr,
}, nil
}
`))
var CreateOrUpdateFuncTemplate = template.Must(template.New("object-scaffolding-create-func").Parse(`
{{- $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{- $resource = .SubResource}}
{{- end }}
const CreateOrUpdateOperationName = "{{.ClientName}}.Create"
// CreateOrUpdate creates or updates a {{$resource}}.
func (client *Client) CreateOrUpdate(ctx context.Context, resourceGroupName string, resourceName string,{{with .SubResource}}parentResourceName string, {{end}} resource {{.PackageAlias}}.{{$resource}}) (result *{{.PackageAlias}}.{{$resource}},err error) {
ctx = utils.ContextWithClientName(ctx, "{{.ClientName}}")
ctx = utils.ContextWithRequestMethod(ctx, "CreateOrUpdate")
ctx = utils.ContextWithResourceGroupName(ctx, resourceGroupName)
ctx = utils.ContextWithSubscriptionID(ctx, client.subscriptionID)
ctx, endSpan := runtime.StartSpan(ctx, CreateOrUpdateOperationName, client.tracer, nil)
defer endSpan(err)
resp, err := utils.NewPollerWrapper(client.{{.ClientName}}.BeginCreateOrUpdate(ctx, resourceGroupName, resourceName,{{with .SubResource}}parentResourceName,{{end}} resource, nil)).WaitforPollerResp(ctx)
if err != nil {
return nil, err
}
if resp != nil {
return &resp.{{$resource}}, nil
}
return nil, nil
}
`))
var ListByRGFuncTemplate = template.Must(template.New("object-scaffolding-list-func").Parse(`
{{- $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{- $resource = .SubResource}}
{{- end }}
const ListOperationName = "{{.ClientName}}.List"
// List gets a list of {{$resource}} in the resource group.
func (client *Client) List(ctx context.Context, resourceGroupName string{{with .SubResource}}, parentResourceName string{{end}}) (result []*{{.PackageAlias}}.{{$resource}}, rerr error) {
ctx = utils.ContextWithClientName(ctx, "{{.ClientName}}")
ctx = utils.ContextWithRequestMethod(ctx, "List")
ctx = utils.ContextWithResourceGroupName(ctx, resourceGroupName)
ctx = utils.ContextWithSubscriptionID(ctx, client.subscriptionID)
ctx, endSpan := runtime.StartSpan(ctx, ListOperationName, client.tracer, nil)
defer endSpan(rerr)
pager := client.{{.ClientName}}.NewListByResourceGroupPager(resourceGroupName, nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
result = append(result, nextResult.Value...)
}
return result, nil
}
`))
var ListFuncTemplate = template.Must(template.New("object-scaffolding-list-func").Parse(`
{{- $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{- $resource = .SubResource}}
{{- end }}
const ListOperationName = "{{.ClientName}}.List"
// List gets a list of {{$resource}} in the resource group.
func (client *Client) List(ctx context.Context, resourceGroupName string{{with .SubResource}}, parentResourceName string{{end}}) (result []*{{.PackageAlias}}.{{$resource}}, rerr error) {
ctx = utils.ContextWithClientName(ctx, "{{.ClientName}}")
ctx = utils.ContextWithRequestMethod(ctx, "List")
ctx = utils.ContextWithResourceGroupName(ctx, resourceGroupName)
ctx = utils.ContextWithSubscriptionID(ctx, client.subscriptionID)
ctx, endSpan := runtime.StartSpan(ctx, ListOperationName, client.tracer, nil)
defer endSpan(rerr)
pager := client.{{.ClientName}}.NewListPager(resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
result = append(result, nextResult.Value...)
}
return result, nil
}
`))
var DeleteFuncTemplate = template.Must(template.New("object-scaffolding-delete-func").Parse(`
{{- $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{- $resource = .SubResource}}
{{- end }}
const DeleteOperationName = "{{.ClientName}}.Delete"
// Delete deletes a {{$resource}} by name.
func (client *Client) Delete(ctx context.Context, resourceGroupName string, {{with .SubResource}} parentResourceName string, {{end}}resourceName string) (err error) {
ctx = utils.ContextWithClientName(ctx, "{{.ClientName}}")
ctx = utils.ContextWithRequestMethod(ctx, "Delete")
ctx = utils.ContextWithResourceGroupName(ctx, resourceGroupName)
ctx = utils.ContextWithSubscriptionID(ctx, client.subscriptionID)
ctx, endSpan := runtime.StartSpan(ctx, DeleteOperationName, client.tracer, nil)
defer endSpan(err)
_, err = utils.NewPollerWrapper(client.BeginDelete(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName, nil)).WaitforPollerResp(ctx)
return err
}
`))
var GetFuncTemplate = template.Must(template.New("object-scaffolding-get-func").Parse(`
{{- $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{- $resource = .SubResource}}
{{- end }}
const GetOperationName = "{{.ClientName}}.Get"
// Get gets the {{$resource}}
func (client *Client) Get(ctx context.Context, resourceGroupName string, {{with .SubResource}}parentResourceName string,{{end}} resourceName string{{if .Expand}}, expand *string{{end}}) (result *{{.PackageAlias}}.{{$resource}}, rerr error) {
{{ if .Expand}}var ops *{{.PackageAlias}}.{{.ClientName}}GetOptions
if expand != nil {
ops = &{{.PackageAlias}}.{{.ClientName}}GetOptions{ Expand: expand }
}{{- end}}
ctx = utils.ContextWithClientName(ctx, "{{.ClientName}}")
ctx = utils.ContextWithRequestMethod(ctx, "Get")
ctx = utils.ContextWithResourceGroupName(ctx, resourceGroupName)
ctx = utils.ContextWithSubscriptionID(ctx, client.subscriptionID)
ctx, endSpan := runtime.StartSpan(ctx, GetOperationName, client.tracer, nil)
defer endSpan(rerr)
resp, err := client.{{.ClientName}}.Get(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName,{{if .Expand}}ops{{else}}nil{{end}} )
if err != nil {
return nil, err
}
//handle statuscode
return &resp.{{$resource}}, nil
}
`))
var ImportTemplate = template.Must(template.New("import").Parse(
`
import (
{{ range $package, $Entry := . }}
{{- range $alias, $flag := $Entry}}{{$alias}} {{end}}"{{$package}}"
{{ end -}}
)
`))
type ImportStatement struct {
Alias string
Package string
}
var TestSuiteTemplate = template.Must(template.New("object-scaffolding-test-suite").Parse(
`
{{- $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{- $resource = .SubResource}}
{{- end }}
func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Client Suite")
}
var resourceGroupName = "aks-cit-{{$resource}}"
var resourceName = "testResource"
{{if .SubResource}}var parentResourceName = "testParentResource"{{- end }}
var subscriptionID string
var location = "eastus"
var resourceGroupClient *armresources.ResourceGroupsClient
var err error
var recorder *recording.Recorder
var realClient Interface
var _ = BeforeSuite(func(ctx context.Context) {
recorder, err = recording.NewRecorder("testdata/{{$resource}}")
Expect(err).ToNot(HaveOccurred())
subscriptionID = recorder.SubscriptionID()
Expect(err).NotTo(HaveOccurred())
cred := recorder.TokenCredential()
resourceGroupClient, err = armresources.NewResourceGroupsClient(subscriptionID, cred, &arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Transport: recorder.HTTPClient(),
TracingProvider: utils.TracingProvider,
},
})
Expect(err).NotTo(HaveOccurred())
realClient, err = New(subscriptionID, recorder.TokenCredential(), &arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Transport: recorder.HTTPClient(),
},
})
Expect(err).NotTo(HaveOccurred())
_, err = resourceGroupClient.CreateOrUpdate(
ctx,
resourceGroupName,
armresources.ResourceGroup{
Location: to.Ptr(location),
},
nil)
Expect(err).NotTo(HaveOccurred())
})
var _ = AfterSuite(func(ctx context.Context) {
_, err := resourceGroupClient.BeginDelete(ctx, resourceGroupName, nil)
Expect(err).NotTo(HaveOccurred())
err = recorder.Stop()
Expect(err).ToNot(HaveOccurred())
})
`))
var TestCaseTemplate = template.Must(template.New("object-scaffolding-test-case").Parse(
`
{{ $resource := .Resource}}
{{- if (gt (len .SubResource) 0) }}
{{ $resource = .SubResource}}
{{- end -}}
{{- $HasCreateOrUpdate := false }}
{{- $HasGet := false }}
{{- $HasDelete := false }}
{{- $HasListByRG := false }}
{{- $HasList := false }}
{{- range .Verbs}}
{{- if eq . "createorupdate"}}{{$HasCreateOrUpdate = true}}{{end}}
{{- if eq . "get"}}{{$HasGet = true}}{{end}}
{{- if eq . "delete"}}{{$HasDelete = true}}{{end}}
{{- if eq . "listbyrg"}}{{$HasListByRG = true}}{{end}}
{{- if eq . "list"}}{{$HasList = true}}{{end}}
{{- end -}}
var beforeAllFunc func(context.Context)
var afterAllFunc func(context.Context)
var additionalTestCases func()
{{if or $HasCreateOrUpdate}}var newResource *{{.PackageAlias}}.{{$resource}} = &{{.PackageAlias}}.{{$resource}}{} {{- end }}
var _ = Describe("{{.ClientName}}", Ordered, func() {
if beforeAllFunc != nil {
BeforeAll(beforeAllFunc)
}
if additionalTestCases != nil {
additionalTestCases()
}
{{if $HasCreateOrUpdate}}
When("creation requests are raised", func() {
It("should not return error", func(ctx context.Context) {
newResource, err := realClient.CreateOrUpdate(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName, *newResource)
Expect(err).NotTo(HaveOccurred())
Expect(newResource).NotTo(BeNil())
Expect(strings.EqualFold(*newResource.Name,resourceName)).To(BeTrue())
})
})
{{end -}}
{{if $HasGet}}
When("get requests are raised", func() {
It("should not return error", func(ctx context.Context) {
newResource, err := realClient.Get(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName{{if .Expand}}, nil{{end}})
Expect(err).NotTo(HaveOccurred())
Expect(newResource).NotTo(BeNil())
})
})
When("invalid get requests are raised", func() {
It("should return 404 error", func(ctx context.Context) {
newResource, err := realClient.Get(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName+"notfound"{{if .Expand}}, nil{{end}})
Expect(err).To(HaveOccurred())
Expect(newResource).To(BeNil())
})
})
{{end -}}
{{if $HasCreateOrUpdate}}
When("update requests are raised", func() {
It("should not return error", func(ctx context.Context) {
newResource, err := realClient.CreateOrUpdate(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName, *newResource)
Expect(err).NotTo(HaveOccurred())
Expect(newResource).NotTo(BeNil())
})
})
{{end -}}
{{if or $HasListByRG $HasList}}
When("list requests are raised", func() {
It("should not return error", func(ctx context.Context) {
resourceList, err := realClient.List(ctx, resourceGroupName,{{with .SubResource}}parentResourceName{{end}})
Expect(err).NotTo(HaveOccurred())
Expect(resourceList).NotTo(BeNil())
Expect(len(resourceList)).To(Equal(1))
Expect(*resourceList[0].Name).To(Equal(resourceName))
})
})
When("invalid list requests are raised", func() {
It("should return error", func(ctx context.Context) {
resourceList, err := realClient.List(ctx, resourceGroupName+"notfound",{{with .SubResource}}parentResourceName{{end}})
Expect(err).To(HaveOccurred())
Expect(resourceList).To(BeNil())
})
})
{{end -}}
{{if $HasDelete}}
When("deletion requests are raised", func() {
It("should not return error", func(ctx context.Context) {
err = realClient.Delete(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName)
Expect(err).NotTo(HaveOccurred())
})
})
{{end -}}
if afterAllFunc != nil {
AfterAll(afterAllFunc)
}
})
`))
var TestCaseCustomTemplate = template.Must(template.New("object-scaffolding-test-case-custom").Parse(
`
{{ $resource := .Resource}}
{{ if (gt (len .SubResource) 0) }}
{{ $resource = .SubResource}}
{{- end -}}
{{- $HasCreateOrUpdate := false }}
{{- $HasGet := false }}
{{- $HasDelete := false }}
{{- $HasListByRG := false }}
{{- $HasList := false }}
{{- range .Verbs}}
{{- if eq . "createorupdate"}}{{$HasCreateOrUpdate = true}}{{end}}
{{- if eq . "get"}}{{$HasGet = true}}{{end}}
{{- if eq . "delete"}}{{$HasDelete = true}}{{end}}
{{- if eq . "listbyrg"}}{{$HasListByRG = true}}{{end}}
{{- if eq . "list"}}{{$HasList = true}}{{end}}
{{- end -}}
func init() {
additionalTestCases = func() {
}
beforeAllFunc = func(ctx context.Context) {
{{if not $HasCreateOrUpdate}}
newResource = &{{.PackageAlias}}.{{$resource}}{
{{if not .SubResource}}Location: to.Ptr(location), {{- end -}}
}
{{- end -}}
}
afterAllFunc = func(ctx context.Context) {
}
}
`))