go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/testutils/mockprovider/resources/mockprovider.lr.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  // Code generated by resources. DO NOT EDIT.
     5  
     6  package resources
     7  
     8  import (
     9  	"errors"
    10  
    11  	"go.mondoo.com/cnquery/llx"
    12  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    13  	"go.mondoo.com/cnquery/types"
    14  )
    15  
    16  var resourceFactories map[string]plugin.ResourceFactory
    17  
    18  func init() {
    19  	resourceFactories = map[string]plugin.ResourceFactory {
    20  		"muser": {
    21  			// to override args, implement: initMuser(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
    22  			Create: createMuser,
    23  		},
    24  		"mgroup": {
    25  			// to override args, implement: initMgroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error)
    26  			Create: createMgroup,
    27  		},
    28  	}
    29  }
    30  
    31  // NewResource is used by the runtime of this plugin to create new resources.
    32  // Its arguments may be provided by users. This function is generally not
    33  // used by initializing resources from recordings or from lists.
    34  func NewResource(runtime *plugin.Runtime, name string, args map[string]*llx.RawData) (plugin.Resource, error) {
    35  	f, ok := resourceFactories[name]
    36  	if !ok {
    37  		return nil, errors.New("cannot find resource " + name + " in this provider")
    38  	}
    39  
    40  	if f.Init != nil {
    41  		cargs, res, err := f.Init(runtime, args)
    42  		if err != nil {
    43  			return res, err
    44  		}
    45  
    46  		if res != nil {
    47  			id := name+"\x00"+res.MqlID()
    48  			if x, ok := runtime.Resources.Get(id); ok {
    49  				return x, nil
    50  			}
    51  			runtime.Resources.Set(id, res)
    52  			return res, nil
    53  		}
    54  
    55  		args = cargs
    56  	}
    57  
    58  	res, err := f.Create(runtime, args)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	id := name+"\x00"+res.MqlID()
    64  	if x, ok := runtime.Resources.Get(id); ok {
    65  		return x, nil
    66  	}
    67  
    68  	runtime.Resources.Set(id, res)
    69  	return res, nil
    70  }
    71  
    72  // CreateResource is used by the runtime of this plugin to create resources.
    73  // Its arguments must be complete and pre-processed. This method is used
    74  // for initializing resources from recordings or from lists.
    75  func CreateResource(runtime *plugin.Runtime, name string, args map[string]*llx.RawData) (plugin.Resource, error) {
    76  	f, ok := resourceFactories[name]
    77  	if !ok {
    78  		return nil, errors.New("cannot find resource " + name + " in this provider")
    79  	}
    80  
    81  	res, err := f.Create(runtime, args)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	id := name+"\x00"+res.MqlID()
    87  	if x, ok := runtime.Resources.Get(id); ok {
    88  		return x, nil
    89  	}
    90  
    91  	runtime.Resources.Set(id, res)
    92  	return res, nil
    93  }
    94  
    95  var getDataFields = map[string]func(r plugin.Resource) *plugin.DataRes{
    96  	"muser.name": func(r plugin.Resource) *plugin.DataRes {
    97  		return (r.(*mqlMuser).GetName()).ToDataRes(types.String)
    98  	},
    99  	"muser.group": func(r plugin.Resource) *plugin.DataRes {
   100  		return (r.(*mqlMuser).GetGroup()).ToDataRes(types.Resource("mgroup"))
   101  	},
   102  	"muser.nullgroup": func(r plugin.Resource) *plugin.DataRes {
   103  		return (r.(*mqlMuser).GetNullgroup()).ToDataRes(types.Resource("mgroup"))
   104  	},
   105  	"muser.groups": func(r plugin.Resource) *plugin.DataRes {
   106  		return (r.(*mqlMuser).GetGroups()).ToDataRes(types.Array(types.Resource("mgroup")))
   107  	},
   108  	"mgroup.name": func(r plugin.Resource) *plugin.DataRes {
   109  		return (r.(*mqlMgroup).GetName()).ToDataRes(types.String)
   110  	},
   111  }
   112  
   113  func GetData(resource plugin.Resource, field string, args map[string]*llx.RawData) *plugin.DataRes {
   114  	f, ok := getDataFields[resource.MqlName()+"."+field]
   115  	if !ok {
   116  		return &plugin.DataRes{Error: "cannot find '" + field + "' in resource '" + resource.MqlName() + "'"}
   117  	}
   118  
   119  	return f(resource)
   120  }
   121  
   122  var setDataFields = map[string]func(r plugin.Resource, v *llx.RawData) bool {
   123  	"muser.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   124  			r.(*mqlMuser).__id, ok = v.Value.(string)
   125  			return
   126  		},
   127  	"muser.name": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   128  		r.(*mqlMuser).Name, ok = plugin.RawToTValue[string](v.Value, v.Error)
   129  		return
   130  	},
   131  	"muser.group": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   132  		r.(*mqlMuser).Group, ok = plugin.RawToTValue[*mqlMgroup](v.Value, v.Error)
   133  		return
   134  	},
   135  	"muser.nullgroup": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   136  		r.(*mqlMuser).Nullgroup, ok = plugin.RawToTValue[*mqlMgroup](v.Value, v.Error)
   137  		return
   138  	},
   139  	"muser.groups": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   140  		r.(*mqlMuser).Groups, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error)
   141  		return
   142  	},
   143  	"mgroup.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   144  			r.(*mqlMgroup).__id, ok = v.Value.(string)
   145  			return
   146  		},
   147  	"mgroup.name": func(r plugin.Resource, v *llx.RawData) (ok bool) {
   148  		r.(*mqlMgroup).Name, ok = plugin.RawToTValue[string](v.Value, v.Error)
   149  		return
   150  	},
   151  }
   152  
   153  func SetData(resource plugin.Resource, field string, val *llx.RawData) error {
   154  	f, ok := setDataFields[resource.MqlName() + "." + field]
   155  	if !ok {
   156  		return errors.New("[mockprovider] cannot set '"+field+"' in resource '"+resource.MqlName()+"', field not found")
   157  	}
   158  
   159  	if ok := f(resource, val); !ok {
   160  		return errors.New("[mockprovider] cannot set '"+field+"' in resource '"+resource.MqlName()+"', type does not match")
   161  	}
   162  	return nil
   163  }
   164  
   165  func SetAllData(resource plugin.Resource, args map[string]*llx.RawData) error {
   166  	var err error
   167  	for k, v := range args {
   168  		if err = SetData(resource, k, v); err != nil {
   169  			return err
   170  		}
   171  	}
   172  	return nil
   173  }
   174  
   175  // mqlMuser for the muser resource
   176  type mqlMuser struct {
   177  	MqlRuntime *plugin.Runtime
   178  	__id string
   179  	// optional: if you define mqlMuserInternal it will be used here
   180  	Name plugin.TValue[string]
   181  	Group plugin.TValue[*mqlMgroup]
   182  	Nullgroup plugin.TValue[*mqlMgroup]
   183  	Groups plugin.TValue[[]interface{}]
   184  }
   185  
   186  // createMuser creates a new instance of this resource
   187  func createMuser(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
   188  	res := &mqlMuser{
   189  		MqlRuntime: runtime,
   190  	}
   191  
   192  	err := SetAllData(res, args)
   193  	if err != nil {
   194  		return res, err
   195  	}
   196  
   197  	if res.__id == "" {
   198  	res.__id, err = res.id()
   199  		if err != nil {
   200  			return nil, err
   201  		}
   202  	}
   203  
   204  	if runtime.HasRecording {
   205  		args, err = runtime.ResourceFromRecording("muser", res.__id)
   206  		if err != nil || args == nil {
   207  			return res, err
   208  		}
   209  		return res, SetAllData(res, args)
   210  	}
   211  
   212  	return res, nil
   213  }
   214  
   215  func (c *mqlMuser) MqlName() string {
   216  	return "muser"
   217  }
   218  
   219  func (c *mqlMuser) MqlID() string {
   220  	return c.__id
   221  }
   222  
   223  func (c *mqlMuser) GetName() *plugin.TValue[string] {
   224  	return &c.Name
   225  }
   226  
   227  func (c *mqlMuser) GetGroup() *plugin.TValue[*mqlMgroup] {
   228  	return plugin.GetOrCompute[*mqlMgroup](&c.Group, func() (*mqlMgroup, error) {
   229  		if c.MqlRuntime.HasRecording {
   230  			d, err := c.MqlRuntime.FieldResourceFromRecording("muser", c.__id, "group")
   231  			if err != nil {
   232  				return nil, err
   233  			}
   234  			if d != nil {
   235  				return d.Value.(*mqlMgroup), nil
   236  			}
   237  		}
   238  
   239  		return c.group()
   240  	})
   241  }
   242  
   243  func (c *mqlMuser) GetNullgroup() *plugin.TValue[*mqlMgroup] {
   244  	return plugin.GetOrCompute[*mqlMgroup](&c.Nullgroup, func() (*mqlMgroup, error) {
   245  		if c.MqlRuntime.HasRecording {
   246  			d, err := c.MqlRuntime.FieldResourceFromRecording("muser", c.__id, "nullgroup")
   247  			if err != nil {
   248  				return nil, err
   249  			}
   250  			if d != nil {
   251  				return d.Value.(*mqlMgroup), nil
   252  			}
   253  		}
   254  
   255  		return c.nullgroup()
   256  	})
   257  }
   258  
   259  func (c *mqlMuser) GetGroups() *plugin.TValue[[]interface{}] {
   260  	return plugin.GetOrCompute[[]interface{}](&c.Groups, func() ([]interface{}, error) {
   261  		if c.MqlRuntime.HasRecording {
   262  			d, err := c.MqlRuntime.FieldResourceFromRecording("muser", c.__id, "groups")
   263  			if err != nil {
   264  				return nil, err
   265  			}
   266  			if d != nil {
   267  				return d.Value.([]interface{}), nil
   268  			}
   269  		}
   270  
   271  		return c.groups()
   272  	})
   273  }
   274  
   275  // mqlMgroup for the mgroup resource
   276  type mqlMgroup struct {
   277  	MqlRuntime *plugin.Runtime
   278  	__id string
   279  	// optional: if you define mqlMgroupInternal it will be used here
   280  	Name plugin.TValue[string]
   281  }
   282  
   283  // createMgroup creates a new instance of this resource
   284  func createMgroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) {
   285  	res := &mqlMgroup{
   286  		MqlRuntime: runtime,
   287  	}
   288  
   289  	err := SetAllData(res, args)
   290  	if err != nil {
   291  		return res, err
   292  	}
   293  
   294  	if res.__id == "" {
   295  	res.__id, err = res.id()
   296  		if err != nil {
   297  			return nil, err
   298  		}
   299  	}
   300  
   301  	if runtime.HasRecording {
   302  		args, err = runtime.ResourceFromRecording("mgroup", res.__id)
   303  		if err != nil || args == nil {
   304  			return res, err
   305  		}
   306  		return res, SetAllData(res, args)
   307  	}
   308  
   309  	return res, nil
   310  }
   311  
   312  func (c *mqlMgroup) MqlName() string {
   313  	return "mgroup"
   314  }
   315  
   316  func (c *mqlMgroup) MqlID() string {
   317  	return c.__id
   318  }
   319  
   320  func (c *mqlMgroup) GetName() *plugin.TValue[string] {
   321  	return &c.Name
   322  }