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

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources
     5  
     6  func (s *Schema) Add(other *Schema) *Schema {
     7  	if other == nil {
     8  		return s
     9  	}
    10  
    11  	for k, v := range other.Resources {
    12  		if existing, ok := s.Resources[k]; ok {
    13  			// We will merge resources into it until we find one that is not extending.
    14  			// Technically, this should only happen with one resource and one only,
    15  			// i.e. the root resource. This is more of a protection.
    16  			if existing.IsExtension {
    17  				existing.IsExtension = v.IsExtension
    18  				existing.Provider = v.Provider
    19  				existing.Init = v.Init
    20  			}
    21  			// TODO: clean up any resource that clashes right now. There are a few
    22  			//       implicit extensions that cause this behavior at the moment.
    23  			//       log.Warn().Str("resource", k).Msg("found a resource that is not flagged as `extends` properly")
    24  			// else if !v.IsExtension {}
    25  
    26  			if v.Title != "" {
    27  				existing.Title = v.Title
    28  			}
    29  			if v.Name != "" {
    30  				existing.Name = v.Name
    31  			}
    32  			if v.MinMondooVersion != "" {
    33  				existing.MinMondooVersion = v.MinMondooVersion
    34  			}
    35  			if v.Desc != "" {
    36  				existing.Desc = v.Desc
    37  			}
    38  			if !v.Private {
    39  				existing.Private = false
    40  			}
    41  			if v.Defaults != "" {
    42  				existing.Defaults = v.Defaults
    43  			}
    44  
    45  			if existing.Fields == nil {
    46  				existing.Fields = v.Fields
    47  			} else {
    48  				for fk, fv := range v.Fields {
    49  					existing.Fields[fk] = fv
    50  				}
    51  			}
    52  		} else {
    53  			s.Resources[k] = v
    54  		}
    55  	}
    56  
    57  	return s
    58  }
    59  
    60  func (s *Schema) Lookup(name string) *ResourceInfo {
    61  	return s.Resources[name]
    62  }
    63  
    64  func (s *Schema) LookupField(resource string, field string) (*ResourceInfo, *Field) {
    65  	res := s.Lookup(resource)
    66  	if res == nil || res.Fields == nil {
    67  		return res, nil
    68  	}
    69  	return res, res.Fields[field]
    70  }
    71  
    72  func (s *Schema) AllResources() map[string]*ResourceInfo {
    73  	return s.Resources
    74  }