github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/parse/references.go (about)

     1  package parse
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/hashicorp/hcl/v2/hclsyntax"
     6  	"github.com/turbot/pipe-fittings/hclhelpers"
     7  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
     8  )
     9  
    10  // AddReferences populates the 'References' resource field, used for the introspection tables
    11  func AddReferences(resource modconfig.HclResource, block *hcl.Block, parseCtx *ModParseContext) hcl.Diagnostics {
    12  	resourceWithMetadata, ok := resource.(modconfig.ResourceWithMetadata)
    13  	if !ok {
    14  		return nil
    15  	}
    16  
    17  	var diags hcl.Diagnostics
    18  	for _, attr := range block.Body.(*hclsyntax.Body).Attributes {
    19  		for _, v := range attr.Expr.Variables() {
    20  			for _, referenceBlockType := range modconfig.ReferenceBlocks {
    21  				if referenceString, ok := hclhelpers.ResourceNameFromTraversal(referenceBlockType, v); ok {
    22  					var blockName string
    23  					if len(block.Labels) > 0 {
    24  						blockName = block.Labels[0]
    25  					}
    26  					reference := modconfig.NewResourceReference(resource, block, referenceString, blockName, attr)
    27  
    28  					moreDiags := addResourceMetadata(reference, attr.SrcRange, parseCtx)
    29  					diags = append(diags, moreDiags...)
    30  					resourceWithMetadata.AddReference(reference)
    31  					break
    32  				}
    33  			}
    34  		}
    35  	}
    36  	return diags
    37  }