-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmockprovider.lr.go
More file actions
603 lines (520 loc) · 16.1 KB
/
mockprovider.lr.go
File metadata and controls
603 lines (520 loc) · 16.1 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright Mondoo, Inc. 2024, 2026
// SPDX-License-Identifier: BUSL-1.1
// Code generated by resources. DO NOT EDIT.
package resources
import (
"errors"
"go.mondoo.com/mql/v13/llx"
"go.mondoo.com/mql/v13/providers-sdk/v1/plugin"
"go.mondoo.com/mql/v13/types"
)
// The MQL type names exposed as public consts for ease of reference.
const (
ResourceMuser string = "muser"
ResourceMgroup string = "mgroup"
ResourceMos string = "mos"
ResourceCustomGroups string = "customGroups"
ResourceEmptyGroups string = "emptyGroups"
)
var resourceFactories map[string]plugin.ResourceFactory
func init() {
resourceFactories = map[string]plugin.ResourceFactory{
"muser": {
// to override args, implement: initMuser(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
Create: createMuser,
},
"mgroup": {
// to override args, implement: initMgroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
Create: createMgroup,
},
"mos": {
// to override args, implement: initMos(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
Create: createMos,
},
"customGroups": {
// to override args, implement: initCustomGroups(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
Create: createCustomGroups,
},
"emptyGroups": {
// to override args, implement: initEmptyGroups(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
Create: createEmptyGroups,
},
}
}
// NewResource is used by the runtime of this plugin to create new resources.
// Its arguments may be provided by users. This function is generally not
// used by initializing resources from recordings or from lists.
func NewResource(runtime *plugin.Runtime, name string, args map[string]*llx.RawData) (plugin.Resource, error) {
f, ok := resourceFactories[name]
if !ok {
return nil, errors.New("cannot find resource " + name + " in this provider")
}
if f.Init != nil {
cargs, res, err := f.Init(runtime, args)
if err != nil {
return res, err
}
if res != nil {
mqlId := res.MqlID()
id := name + "\x00" + mqlId
if x, ok := runtime.Resources.Get(id); ok {
return x, nil
}
runtime.Resources.Set(id, res)
return res, nil
}
args = cargs
}
res, err := f.Create(runtime, args)
if err != nil {
return nil, err
}
mqlId := res.MqlID()
id := name + "\x00" + mqlId
if x, ok := runtime.Resources.Get(id); ok {
return x, nil
}
runtime.Resources.Set(id, res)
return res, nil
}
// CreateResource is used by the runtime of this plugin to create resources.
// Its arguments must be complete and pre-processed. This method is used
// for initializing resources from recordings or from lists.
func CreateResource(runtime *plugin.Runtime, name string, args map[string]*llx.RawData) (plugin.Resource, error) {
f, ok := resourceFactories[name]
if !ok {
return nil, errors.New("cannot find resource " + name + " in this provider")
}
res, err := f.Create(runtime, args)
if err != nil {
return nil, err
}
mqlId := res.MqlID()
id := name + "\x00" + mqlId
if x, ok := runtime.Resources.Get(id); ok {
return x, nil
}
runtime.Resources.Set(id, res)
return res, nil
}
var getDataFields = map[string]func(r plugin.Resource) *plugin.DataRes{
"muser.name": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetName()).ToDataRes(types.String)
},
"muser.group": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetGroup()).ToDataRes(types.Resource("mgroup"))
},
"muser.nullgroup": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetNullgroup()).ToDataRes(types.Resource("mgroup"))
},
"muser.nullstring": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetNullstring()).ToDataRes(types.String)
},
"muser.groups": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetGroups()).ToDataRes(types.Array(types.Resource("mgroup")))
},
"muser.dict": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetDict()).ToDataRes(types.Dict)
},
"muser.error": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMuser).GetError()).ToDataRes(types.String)
},
"mgroup.name": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMgroup).GetName()).ToDataRes(types.String)
},
"mos.groups": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlMos).GetGroups()).ToDataRes(types.Resource("customGroups"))
},
"customGroups.length": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlCustomGroups).GetLength()).ToDataRes(types.Int)
},
"customGroups.list": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlCustomGroups).GetList()).ToDataRes(types.Array(types.Resource("mgroup")))
},
"emptyGroups.list": func(r plugin.Resource) *plugin.DataRes {
return (r.(*mqlEmptyGroups).GetList()).ToDataRes(types.Array(types.Resource("mgroup")))
},
}
func GetData(resource plugin.Resource, field string, args map[string]*llx.RawData) *plugin.DataRes {
f, ok := getDataFields[resource.MqlName()+"."+field]
if !ok {
return &plugin.DataRes{Error: "cannot find '" + field + "' in resource '" + resource.MqlName() + "'"}
}
return f(resource)
}
var setDataFields = map[string]func(r plugin.Resource, v *llx.RawData) bool{
"muser.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).__id, ok = v.Value.(string)
return
},
"muser.name": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Name, ok = plugin.RawToTValue[string](v.Value, v.Error)
return
},
"muser.group": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Group, ok = plugin.RawToTValue[*mqlMgroup](v.Value, v.Error)
return
},
"muser.nullgroup": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Nullgroup, ok = plugin.RawToTValue[*mqlMgroup](v.Value, v.Error)
return
},
"muser.nullstring": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Nullstring, ok = plugin.RawToTValue[string](v.Value, v.Error)
return
},
"muser.groups": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Groups, ok = plugin.RawToTValue[[]any](v.Value, v.Error)
return
},
"muser.dict": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Dict, ok = plugin.RawToTValue[any](v.Value, v.Error)
return
},
"muser.error": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMuser).Error, ok = plugin.RawToTValue[string](v.Value, v.Error)
return
},
"mgroup.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMgroup).__id, ok = v.Value.(string)
return
},
"mgroup.name": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMgroup).Name, ok = plugin.RawToTValue[string](v.Value, v.Error)
return
},
"mos.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMos).__id, ok = v.Value.(string)
return
},
"mos.groups": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlMos).Groups, ok = plugin.RawToTValue[*mqlCustomGroups](v.Value, v.Error)
return
},
"customGroups.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlCustomGroups).__id, ok = v.Value.(string)
return
},
"customGroups.length": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlCustomGroups).Length, ok = plugin.RawToTValue[int64](v.Value, v.Error)
return
},
"customGroups.list": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlCustomGroups).List, ok = plugin.RawToTValue[[]any](v.Value, v.Error)
return
},
"emptyGroups.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlEmptyGroups).__id, ok = v.Value.(string)
return
},
"emptyGroups.list": func(r plugin.Resource, v *llx.RawData) (ok bool) {
r.(*mqlEmptyGroups).List, ok = plugin.RawToTValue[[]any](v.Value, v.Error)
return
},
}
func SetData(resource plugin.Resource, field string, val *llx.RawData) error {
f, ok := setDataFields[resource.MqlName()+"."+field]
if !ok {
return errors.New("[mockprovider] cannot set '" + field + "' in resource '" + resource.MqlName() + "', field not found")
}
if ok := f(resource, val); !ok {
return errors.New("[mockprovider] cannot set '" + field + "' in resource '" + resource.MqlName() + "', type does not match")
}
return nil
}
func SetAllData(resource plugin.Resource, args map[string]*llx.RawData) error {
var err error
for k, v := range args {
if err = SetData(resource, k, v); err != nil {
return err
}
}
return nil
}
// mqlMuser for the muser resource
type mqlMuser struct {
MqlRuntime *plugin.Runtime
__id string
// optional: if you define mqlMuserInternal it will be used here
Name plugin.TValue[string]
Group plugin.TValue[*mqlMgroup]
Nullgroup plugin.TValue[*mqlMgroup]
Nullstring plugin.TValue[string]
Groups plugin.TValue[[]any]
Dict plugin.TValue[any]
Error plugin.TValue[string]
}
// createMuser creates a new instance of this resource
func createMuser(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
res := &mqlMuser{
MqlRuntime: runtime,
}
err := SetAllData(res, args)
if err != nil {
return res, err
}
if res.__id == "" {
res.__id, err = res.id()
if err != nil {
return nil, err
}
}
if runtime.HasRecording {
args, err = runtime.ResourceFromRecording("muser", res.__id)
if err != nil || args == nil {
return res, err
}
return res, SetAllData(res, args)
}
return res, nil
}
func (c *mqlMuser) MqlName() string {
return "muser"
}
func (c *mqlMuser) MqlID() string {
return c.__id
}
func (c *mqlMuser) GetName() *plugin.TValue[string] {
return &c.Name
}
func (c *mqlMuser) GetGroup() *plugin.TValue[*mqlMgroup] {
return plugin.GetOrCompute[*mqlMgroup](&c.Group, func() (*mqlMgroup, error) {
if c.MqlRuntime.HasRecording {
d, err := c.MqlRuntime.FieldResourceFromRecording("muser", c.__id, "group")
if err != nil {
return nil, err
}
if d != nil {
return d.Value.(*mqlMgroup), nil
}
}
return c.group()
})
}
func (c *mqlMuser) GetNullgroup() *plugin.TValue[*mqlMgroup] {
return plugin.GetOrCompute[*mqlMgroup](&c.Nullgroup, func() (*mqlMgroup, error) {
if c.MqlRuntime.HasRecording {
d, err := c.MqlRuntime.FieldResourceFromRecording("muser", c.__id, "nullgroup")
if err != nil {
return nil, err
}
if d != nil {
return d.Value.(*mqlMgroup), nil
}
}
return c.nullgroup()
})
}
func (c *mqlMuser) GetNullstring() *plugin.TValue[string] {
return plugin.GetOrCompute[string](&c.Nullstring, func() (string, error) {
return c.nullstring()
})
}
func (c *mqlMuser) GetGroups() *plugin.TValue[[]any] {
return plugin.GetOrCompute[[]any](&c.Groups, func() ([]any, error) {
if c.MqlRuntime.HasRecording {
d, err := c.MqlRuntime.FieldResourceFromRecording("muser", c.__id, "groups")
if err != nil {
return nil, err
}
if d != nil {
return d.Value.([]any), nil
}
}
return c.groups()
})
}
func (c *mqlMuser) GetDict() *plugin.TValue[any] {
return plugin.GetOrCompute[any](&c.Dict, func() (any, error) {
return c.dict()
})
}
func (c *mqlMuser) GetError() *plugin.TValue[string] {
return plugin.GetOrCompute[string](&c.Error, func() (string, error) {
return c.error()
})
}
// mqlMgroup for the mgroup resource
type mqlMgroup struct {
MqlRuntime *plugin.Runtime
__id string
// optional: if you define mqlMgroupInternal it will be used here
Name plugin.TValue[string]
}
// createMgroup creates a new instance of this resource
func createMgroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
res := &mqlMgroup{
MqlRuntime: runtime,
}
err := SetAllData(res, args)
if err != nil {
return res, err
}
if res.__id == "" {
res.__id, err = res.id()
if err != nil {
return nil, err
}
}
if runtime.HasRecording {
args, err = runtime.ResourceFromRecording("mgroup", res.__id)
if err != nil || args == nil {
return res, err
}
return res, SetAllData(res, args)
}
return res, nil
}
func (c *mqlMgroup) MqlName() string {
return "mgroup"
}
func (c *mqlMgroup) MqlID() string {
return c.__id
}
func (c *mqlMgroup) GetName() *plugin.TValue[string] {
return &c.Name
}
// mqlMos for the mos resource
type mqlMos struct {
MqlRuntime *plugin.Runtime
__id string
// optional: if you define mqlMosInternal it will be used here
Groups plugin.TValue[*mqlCustomGroups]
}
// createMos creates a new instance of this resource
func createMos(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
res := &mqlMos{
MqlRuntime: runtime,
}
err := SetAllData(res, args)
if err != nil {
return res, err
}
// to override __id implement: id() (string, error)
if runtime.HasRecording {
args, err = runtime.ResourceFromRecording("mos", res.__id)
if err != nil || args == nil {
return res, err
}
return res, SetAllData(res, args)
}
return res, nil
}
func (c *mqlMos) MqlName() string {
return "mos"
}
func (c *mqlMos) MqlID() string {
return c.__id
}
func (c *mqlMos) GetGroups() *plugin.TValue[*mqlCustomGroups] {
return plugin.GetOrCompute[*mqlCustomGroups](&c.Groups, func() (*mqlCustomGroups, error) {
if c.MqlRuntime.HasRecording {
d, err := c.MqlRuntime.FieldResourceFromRecording("mos", c.__id, "groups")
if err != nil {
return nil, err
}
if d != nil {
return d.Value.(*mqlCustomGroups), nil
}
}
return c.groups()
})
}
// mqlCustomGroups for the customGroups resource
type mqlCustomGroups struct {
MqlRuntime *plugin.Runtime
__id string
// optional: if you define mqlCustomGroupsInternal it will be used here
Length plugin.TValue[int64]
List plugin.TValue[[]any]
}
// createCustomGroups creates a new instance of this resource
func createCustomGroups(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
res := &mqlCustomGroups{
MqlRuntime: runtime,
}
err := SetAllData(res, args)
if err != nil {
return res, err
}
// to override __id implement: id() (string, error)
if runtime.HasRecording {
args, err = runtime.ResourceFromRecording("customGroups", res.__id)
if err != nil || args == nil {
return res, err
}
return res, SetAllData(res, args)
}
return res, nil
}
func (c *mqlCustomGroups) MqlName() string {
return "customGroups"
}
func (c *mqlCustomGroups) MqlID() string {
return c.__id
}
func (c *mqlCustomGroups) GetLength() *plugin.TValue[int64] {
return plugin.GetOrCompute[int64](&c.Length, func() (int64, error) {
return c.length()
})
}
func (c *mqlCustomGroups) GetList() *plugin.TValue[[]any] {
return plugin.GetOrCompute[[]any](&c.List, func() ([]any, error) {
if c.MqlRuntime.HasRecording {
d, err := c.MqlRuntime.FieldResourceFromRecording("customGroups", c.__id, "list")
if err != nil {
return nil, err
}
if d != nil {
return d.Value.([]any), nil
}
}
return c.list()
})
}
// mqlEmptyGroups for the emptyGroups resource
type mqlEmptyGroups struct {
MqlRuntime *plugin.Runtime
__id string
// optional: if you define mqlEmptyGroupsInternal it will be used here
List plugin.TValue[[]any]
}
// createEmptyGroups creates a new instance of this resource
func createEmptyGroups(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
res := &mqlEmptyGroups{
MqlRuntime: runtime,
}
err := SetAllData(res, args)
if err != nil {
return res, err
}
// to override __id implement: id() (string, error)
if runtime.HasRecording {
args, err = runtime.ResourceFromRecording("emptyGroups", res.__id)
if err != nil || args == nil {
return res, err
}
return res, SetAllData(res, args)
}
return res, nil
}
func (c *mqlEmptyGroups) MqlName() string {
return "emptyGroups"
}
func (c *mqlEmptyGroups) MqlID() string {
return c.__id
}
func (c *mqlEmptyGroups) GetList() *plugin.TValue[[]any] {
return plugin.GetOrCompute[[]any](&c.List, func() ([]any, error) {
if c.MqlRuntime.HasRecording {
d, err := c.MqlRuntime.FieldResourceFromRecording("emptyGroups", c.__id, "list")
if err != nil {
return nil, err
}
if d != nil {
return d.Value.([]any), nil
}
}
return c.list()
})
}